r - Shiny Map - Leaflet: How to select input matrix dynamically? -
i trying create shiny app generate dynamic maps. want use selectinput function select matrix dynamically. integrate these maps in rmarkdown file several apps there.
i using amazon ec2 ubuntu machine hosting shiny apps , rstudio. working apps @ /srv/shiny-server.
i getting following error:
error
error in maptab[, "long"] : incorrect number of dimensions error in data.frame(lng = maptab[, "long"], lat = maptab[, "lat"], category = factor(maptab[, : object 'maptab' not found
code
### top doctors map leaflet library(shiny) library(leaflet) ##```{r, echo=false, warning=false, message=false} r_colors <- rgb(t(col2rgb(colors()) / 255)) names(r_colors) <- colors() shinyapp( ui =shinyui(fluidpage( # sidebar slider input number of bins sidebarpanel( selectinput("var", "1, select variables top doc summary file", choices =c( "top_docs" = 1, "docs" = 2, "orgs" = 3, "phys_summ" = 4, "pm" = 5), selected= 1 ), br(), sliderinput("bins", "2, select number of bins map", min = 40, max = 100000, value=20), br(), # radiobuttons("color", "3, select color of histogram", choices =c("green", "red", "yellow"), selected= "green") ), # show plot of generated distribution mainpanel( tabsetpanel(type = "tabs", tabpanel('leaflet map', leafletoutput("leaflet_map")), tabpanel('circular map', leafletoutput("circular_map")) )))), server = shinyserver(function(input, output) { output$leaflet_map <- renderleaflet({ maptab <- input$var m <- leaflet() %>% addtiles() %>% # add default openstreetmap map tiles addmarkers(lng=maptab[,long], lat=maptab[,lat],popup=maptab[,name]) m # print map }) output$circular_map <- renderleaflet({ ### top doctors circular map m = leaflet() %>% addtiles() df = data.frame( lng=maptab[,'long'], lat=maptab[,'lat'], # size = runif(40, 5, 20), category = factor(maptab[,'state']), color = sample(colors(), 40) ) m = leaflet(df) %>% addtiles() m %>% addcirclemarkers(radius = runif(40, 4, 10), color = c('red','blue','green')) }) }), options = list(height = 480, width = 1050, dpi=200) ) ##
sample data input
library(data.table) top_docs = data.table(long = runif(40, -87, -80), lat = runif(40, 25, 42), name = letters, state= letters) docs = data.table(long = runif(9123, -87, -80), lat = runif(40, 25, 42), name = letters, state= letters) orgs = data.table(long = runif(722, -87, -80), lat = runif(40, 25, 42), name = letters, state= letters) phys_summ = data.table(long = runif(9845, -87, -80), lat = runif(40, 25, 42), name = letters, state= letters) pm = data.table(long = runif(99999, -87, -80), lat = runif(40, 25, 42), name = letters, state= letters)
finally, figured out. @nice input.
the error due not using get
character input , incorrect selectinpud id assignment. revised working code:
{r, echo=false, message=false} options(warn=-1) library(shiny) library(leaflet) # {r, echo=false, warning=false, message=false} r_colors <- rgb(t(col2rgb(colors()) / 255)) names(r_colors) <- colors() var=c("top doctors" = "top_docs", "doctors" = "docs", "provider orgs." = "orgs", "all providers" = "pm" ) shinyapp( ui =shinyui(fluidpage( # sidebar slider input number of bins sidebarpanel( selectinput("pid", "1. select providers ", var, selected= "top_docs", selectize = true ), br(), sliderinput("bins", "2, select number of bins map", min = 40, max = 100000, value=20) # radiobuttons("color", "3, select color of histogram", choices =c("green", "red", "yellow"), selected= "green") ), # show plot of generated distribution mainpanel( tabsetpanel(type = "tabs", tabpanel('leaflet map', leafletoutput("leaflet_map")), tabpanel('circular map', leafletoutput("circular_map")) )))), server = shinyserver(function(input, output) { output$leaflet_map <- renderleaflet({ maptab <- get(input$pid) m <- leaflet() %>% addtiles() %>% # add default openstreetmap map tiles addmarkers(lng=maptab[,'long'][1:input$bins], lat=maptab[,'lat'][1:input$bins],popup=maptab[,'name']) m # print map }) ### top doctors circular map output$circular_map <- renderleaflet({ maptab <- get(input$pid) m = leaflet() %>% addtiles() df = data.frame( lng=maptab[,'long'][1:input$bins], lat=maptab[,'lat'][1:input$bins], size = runif(input$bins, 5, 20), category = factor(maptab[,'state']), color = sample(colors(), input$bins) ) m = leaflet(df) %>% addtiles() m %>% addcirclemarkers(radius = runif(input$bins, 4, 10), color = c('red','blue','green')) })
}) # ,options = list(height = 480, width = 1050, dpi=200)
)
Comments
Post a Comment