Change setView dynamically according to select box in R shiny app -


i'm developing leaflet map in r shiny. in app want focus of map changed whenever lng , lat value in setview() changed. lng , lat values based on country select drop down box. use static value lng , lat in ifelse() function , app works. problem when want make things more generic: lng , lat mean of longitude , latitude subset of data chosen country, app doesn't show map anymore (from point of view calculation seems right)

below simplified , workable r script:

global.r:

library(devtools) library(leaflet) library(htmlwidgets) library(shiny) library(shinydashboard) library(sp) library(rworldmap) library(rcurl) library(ggmap)  df <- read.csv(url("https://docs.google.com/spreadsheets/d/1rrejiuxr4naftquqblppudgwvgegtbjexlpjdday2uw/pub?output=csv"),            header = t,            stringsasfactors = f) df$time <- as.date(df$time, "%d/%m/%y") 

ui.r

header <- dashboardheader(   title = 'shiny memery' ) body <- dashboardbody(   fluidrow(     tabbox(       tabpanel("my map", leafletoutput("mymap",height = 550)),       width = 700     )) ) dashboardpage(   header,   dashboardsidebar(     sliderinput('timeline value','time line',min = min(df$time),                 max = max(df$time),                  value = c(min(df$time), min(df$time)+10)),     selectinput("select_country", label = "select country",                  choices = null,                  selected = null)   ),   body ) 

server.r

  shinyserver(function(input, output, session) {    dfs <- reactive({     tmp <- subset(df, df$time <= input$`timeline value`[2] & df$time >= input$`timeline value`[1])       tmp   })    part_choices <- reactive({     as.list(c("all",  unique(as.character(dfs()$country))))   })    observe({     updateselectinput(session, "select_country", choices=part_choices())   })    output$mymap <- renderleaflet({     lng <- ifelse(input$select_country == "all", mean(dfs()$lon),                   mean(subset(dfs(), country %in% input$select_country)$lon)                          )     lat <- ifelse(input$select_country == "all", mean(dfs()$lat),                   mean(subset(dfs(), country %in% input$select_country)$lat)                          )      m <- leaflet(dfs()) %>%       addtiles(       ) %>%         setview(lng, lat, zoom = 5) %>%       addmarkers(~lon, ~lat,                   clusteroptions = markerclusteroptions())   }) }) 

you see in server.r part use ifelse() change lng , lat value later can used in setview() function. after changed else argument calculation app doesn't work anymore.

really appreciate if can tell me wrong.

thanks in advance.

in ui.r, try changing country input to

selectinput("select_country", label = "select country",              choices = "all",              selected = "all") 

my guess ifelses not return number, given input$select_country initialized @ null, (for reasons unclear me) causes both renderleaflet , updateselectinput not run, preventing country selector being updated.


Comments

Popular posts from this blog

c# SetCompatibleTextRenderingDefault must be called before the first -

c++ - Fill runtime data at compile time with templates -

C#.NET Oracle.ManagedDataAccess ConfigSchema.xsd -