Search code examples
rshinyr-leaflet

Call choices from input panel in other functions?


I have a simple shiny app:

Read the zip file. Select some desired file and adjust the input panel based on the number of read files:

ui <- fluidPage(
  fileInput("File", "Input checkbox"),
  selectInput("inSelect", "Select input",c())
  leafletOutput("mymap")
)

server <- function(input, output, session) {
  cor<- reactive({
    x=input$File
    
    if (is.null(x))
      return(NULL)
    
    report_list <- c("Park result.txt",
                     "Park result minus",
                     "Park result plus")
    temp_files <- unzip(x$datapath)
    temp_files <- temp_files[grepl(paste(report_list, collapse = "|"), temp_files)]
    
    T=length(temp_files)

    A_new=c();for(i in 1:(T/3)){A_new[[i]]=c()}
    for(i in 1:(T/3)){A_new[[i]]=....}
    
    result <- list(T=T,A_new=A_new);
    return(result);
  })

observeEvent(cor(),{
updateSelectInput(session, "Select1",
                  label = paste(((cor()$T)/3),"different layout"),
                  choices = paste0("Layout",c(1:((cor()$T)/3))))
})

output$mymap <- renderLeaflet({
    infile=input$File
    if (is.null(infile))
      return(NULL)
    a2=cor()
    leaflet() %>%
      addProviderTiles("OpenTopoMap", group = "MapQuestOpen.Aerial") %>%
      addMarkers(data =a2$A_new[[1]],~long, ~lat, popup = ~as.character(mag), label = ~as.character(Name))%>% 
      
      addMeasure()
  })
}

shinyApp(ui, server)

At the end, in my output$mymap function, how could I set the parameter A_new, in a way that If user choose layout1, then plot show the map related to A_new[[1]] , if layout2 then map should be A_new[[2]] to display?


Solution

  • You can also use the observeEvent which is a lot less code

     observeEvent(cor(),{
        updateSelectInput(session, "inSelect",label = paste(cor()$t1,"different layout"),choices = paste0("Layout",c(1:cor()$t1)))
      })