Home > Software design >  R - Is that possible to fill in the function arguments that are saved as an object?
R - Is that possible to fill in the function arguments that are saved as an object?

Time:11-01

map_basic <- leaflet::leaflet() %>%
             leaflet::addMarkers(
                data = flow_stations,
                group = "Stream Flow Stations",
                options = leaflet::leafletOptions(pane="marker"),                    
                label = paste0(flow_stations$`Data Source`, ": ", flow_stations$Station, " (", flow_stations$`Station ID`,")"),
                labelOptions = labelOptions(textsize = "15px"),
                popup = ~paste0("<b>", 
                                flow_stations$`Data Source`," Station Name: ",
                                flow_stations$Station,"<br>",
                                "Station ID: ", flow_stations$`Station ID`,
                                sapply(flow_stations$Station, 
                                       popupTable.flow, USE.NAMES = FALSE)),
                popupOptions = leaflet::popupOptions(maxWidth = 650, maxHeight = 300))

In above codes, can I save the arguments in an object and use the object inside the function of leaflet::addMarkers( )? For example,

flowStations <- paste0(
                   'data = flow_stations,',
                   'group = "Stream Flow Stations",',
                   'options = leaflet::leafletOptions(pane="marker"),',                       
                   'label = paste0(flow_stations$`Data Source`, ": ", flow_stations$Station, " (", flow_stations$`Station ID`,")"),',
                   'labelOptions = labelOptions(textsize = "15px"),',
                   'popup = ~paste0("<b>", ',
                                   'flow_stations$`Data Source`," Station Name: ",',
                                   'flow_stations$Station,"<br>",',
                                   '"Station ID: ", flow_stations$`Station ID`,',
                                   'sapply(flow_stations$Station, ',
                                          'popupTable.flow, USE.NAMES = FALSE)),',
                   'popupOptions = leaflet::popupOptions(maxWidth = 650, maxHeight = 300)')

then, simplify the codes to something like below for testing.

map_basic <- leaflet::leaflet() %>%
             leaflet::addMarkers(flowStations)

The error message of the testing codes is

Error in derivePoints(data, lng, lat, missing(lng), missing(lat), "addMarkers") : Point data not found; please provide addMarkers with data and/or lng/lat arguments

Anyone have an idea to make it work, please kindly share. Thanks!

** Update ****************************************************************

Thanks for all your suggestions. I am adding a very simple reproducible script here below.

Station <- c("Station 1","Station 2")
Lat <- c("45.11373","45.07123")
Long <- c("-121.8151","-121.9406")
flow_stations <- data.frame(Station, Lat, Long) %>% 
   sf::st_as_sf(coords = c("Long","Lat"), crs = sf::st_crs(" init=EPSG:4326"))

map_basic <- leaflet::leaflet() %>%
   leaflet::addMarkers(data = flow_stations,
                       label = flow_stations$Station)

I tried to extra the parameters into a list.

flowStations <- list("data = flow_stations",
                     "label = flow_stations$Station")

Then tried below based on @r2evans's suggestion.

map_basic <- leaflet::leaflet() %>%
   do.call(leaflet::addMarkers,flowStations)

I got the error:

Error in do.call(., leaflet::addMarkers, flowStations) : second argument must be a list

I probably didn't use do.call correctly. More suggestions?

CodePudding user response:

Rather than storing cpde as strings, you can write a helper function. For example

myAddMarkers <- function(plot, data) {
  leaflet::addMarkers(
    plot, data = data,
    group = "Stream Flow Stations",
    options = leaflet::leafletOptions(pane = "marker"),
    label = paste0(data$`Data Source`,": ", data$Station," (", data$`Station ID`,")"),
    labelOptions = labelOptions(textsize = "15px"),
    popup = ~ paste0(
      "<b>", data$`Data Source`,
      " Station Name: ", data$Station, "<br>",
      "Station ID: ", data$`Station ID`,
      sapply(data$Station, popupTable.flow, USE.NAMES = FALSE)
    ),
    popupOptions = leaflet::popupOptions(maxWidth = 650, maxHeight = 300)
  )
}

Then for all the plots you want to use these setting, you can use

map_basic <- leaflet::leaflet() %>% myAddMarkers(flow_stations)

CodePudding user response:

If you save the arguments as a list (not a character string), you can use do.call(). Your code is not replicable, but here's an example showing the idea:

args = list(
  x = 1:10,
  y = 2:20
)

do.call(what = t.test, args = args)

which will call the function t.test with the named arguments in args.

  • Related