通过闪亮按钮更改地图上的目的地

问题描述 投票:0回答:1

我有一个闪亮的应用程序,它显示地图,并可以根据 selectinput 功能选择几个国家。 我正在尝试用按钮替换 selectInput,因此用户应该能够单击按钮,然后会出现下一个国家/地区,而不是选择国家/地区。

library(shiny)
library(leaflet)


df <- structure(list(lng = c(101.901875, -95.712891, 108.339537, 37.618423
), lat = c(35.486703, 37.09024, 14.315424, 55.751244), country = structure(c(1L, 
                                                                             3L, 4L, 2L), .Label = c("China", "Russia", "USA", "Vietnam"), class = "factor"), 
number = c(35500L, 6267L, 2947L, 3070L)), .Names = c("lng", 
                                                     "lat", "country", "number"), class = "data.frame", row.names = c(NA, 

                                                                                                                                                                                                                                            -4L))

ui <- (fluidPage(
  titlePanel(title = "countries"),
  sidebarLayout(
    sidebarPanel( uiOutput("countrynames"),
                  shiny::actionButton("dd", "click")
    ),
    mainPanel(leafletOutput("mymap", height = "500")
    ))
)
)

server <- function(input, output){
  output$countrynames <- renderUI({
    selectInput(inputId = "country", label = "Select a country to view it's values (you can choose more than one):",
                c(as.character(df$country)))
  })
  
  
  
  map_data <- reactive({
    data <- data.frame(df[df$country == input$country,])
    data$popup <- paste0(data$country, " ", data$number)
    return(data)
  })
  output$mymap <- renderLeaflet({
    leaflet(data = map_data()) %>%
      setView( lng = -16.882374406249937, lat = -1.7206857960062047, zoom = 0) %>%
      addProviderTiles( provider = "CartoDB.Positron") %>%
      addMarkers(lng = ~lng, lat = ~lat, popup = ~popup)
  })
}
shinyApp(ui, server)

r shiny r-leaflet
1个回答
2
投票

您可以使用

reactiveVal
来表示国家/地区列表的索引来实现此目的。单击按钮时它将更新。

library(shiny)
library(leaflet)

countries = c("China", "Russia", "USA", "Vietnam")

df <- structure(list(lng = c(101.901875, -95.712891, 108.339537, 37.618423
), lat = c(35.486703, 37.09024, 14.315424, 55.751244), country = structure(c(1L, 
                                                                             3L, 4L, 2L), .Label = c("China", "Russia", "USA", "Vietnam"), class = "factor"), 
number = c(35500L, 6267L, 2947L, 3070L)), .Names = c("lng", 
                                                     "lat", "country", "number"), class = "data.frame", row.names = c(NA, 
                                                                                                                      
                                                                                                                      -4L))

ui <- (fluidPage(
  titlePanel(title = "countries"),
  sidebarLayout(
    sidebarPanel( 
      # uiOutput("countrynames"),
      shiny::actionButton("dd", "click")
    ),
    mainPanel(leafletOutput("mymap", height = "500")
    ))
)
)

server <- function(input, output){
  output$countrynames <- renderUI({
    selectInput(inputId = "country", label = "Select a country to view it's values (you can choose more than one):",
                c(as.character(df$country)))
  })
  
  
  index = reactiveVal(1)
  
  selected_country = reactive({
    
    countries[index()]
    
  })
  
  observeEvent(input$dd,{
    i = index() + 1
    if(i > length(countries)){
      i = 1
    }
    index(i)
  })
  
  map_data <- reactive({
    data <- data.frame(df[df$country == selected_country(),])
    data$popup <- paste0(data$country, " ", data$number)
    return(data)
  })
  output$mymap <- renderLeaflet({
    leaflet(data = map_data()) %>%
      setView( lng = -16.882374406249937, lat = -1.7206857960062047, zoom = 0) %>%
      addProviderTiles( provider = "CartoDB.Positron") %>%
      addMarkers(lng = ~lng, lat = ~lat, popup = ~popup)
  })
}
shinyApp(ui, server)

要保持

selectInput
,您可以使用
updateSelectInput

© www.soinside.com 2019 - 2024. All rights reserved.