R闪亮:从selectize中取消选择

3

首先,感谢Shree清理和优化了我的之前的代码(在这里)

无论如何,我有一些代码,使用户能够从selectizeInput中选择一个或多个物种。应用程序会在地图上显示该物种的分布情况。

现在,我困惑了,因为我不能取消选择物种?一旦分布被绘制出来,它们就会保留在地图上,我不能再将其移除... 我已经仔细查找了,但是无法看到它.. 我对Shiny非常陌生.. 所以可能是一个简单的错误?

以下是全部代码, 谢谢!!! JOnas

数据框

df<- data.frame(
  Number_Total = sample(c("5", "6", "1", "3")),
  Species = sample(c("Ilione trifaria", "Pherbellia argyrotarsis", 
"Euthycera seguyi", "Ilione trifaria")),
  X= sample(c("37", "28", "21", "30")),
  Y= sample(c("-5", "-16", "-10", "-15"))
)

UI

ui <- (fluidPage(titlePanel("Species Checker"),  
                 sidebarLayout(
                   sidebarPanel(
                      selectizeInput('species', 'Choose species', 
choices = df$Species, multiple = TRUE, 
options = list(placeholder = 'select species'))
                     ),
                   mainPanel(
                     leafletOutput("CountryMap", width = 600, height = 600))
                 )
))

服务器

server <- function(input, output, session) {
  map_data <- reactive({
    #req(input$species)
    df[df$Species %in% input$species, ]
  })

  output$CountryMap <- renderLeaflet({
    leaflet() %>% addTiles() %>% 
      setView(lng = 20, lat = 40, zoom = 2)
  })

  map_proxy <- leafletProxy("CountryMap")

  observe({
    md <- map_data()
    map_proxy %>%
      addCircles(lng = md$Y, lat = md$X, weight = 10, 
                 radius = sqrt(md$Number_Total)*15000, popup = md$Species)
  })
}

运行应用程序

shinyApp(ui = ui, server = server)

你确定代码能正常工作吗?对我来说,它会崩溃,因为经纬度值似乎是字符类型而不是数字类型。 - Tonio Liebrand
1个回答

0

请尝试以下代码(我已将df中的某些变量取消引用,否则应用程序会崩溃):

library(tidyverse)
library(shiny)
library(leaflet)

df <- data.frame(
  Number_Total = sample(c(5, 6, 1, 3)),
  Species = sample(c("Ilione trifaria", "Pherbellia argyrotarsis", 
                     "Euthycera seguyi", "Ilione trifaria")),
  X= sample(c(37, 28, 21, 30)),
  Y= sample(c(-5, -16, -10, -15))
)

ui <- (fluidPage(titlePanel("Species Checker"),  
                 sidebarLayout(
                   sidebarPanel(
                     selectizeInput('species', 'Choose species', 
                                    choices = df$Species, multiple = TRUE, 
                                    options = list(placeholder = 'select species'))
                   ),
                   mainPanel(
                     leafletOutput("CountryMap", width = 600, height = 600))
                 )
))

server <- function(input, output, session) {

  map_data <- reactive({
    #req(input$species)
    df[df$Species %in% input$species, ]
  })

  output$CountryMap <- renderLeaflet({

    leaflet() %>% addTiles() %>% 
      setView(lng = 20, lat = 40, zoom = 2) %>%
      addCircles(lng = map_data() %>% pull(Y), lat = map_data() %>% pull(X), weight = 10, 
                 radius = sqrt(map_data() %>% pull(Number_Total))*15000, 
                 popup = map_data() %>% pull(Species))

    })

}

shinyApp(ui = ui, server = server)

你需要直接从reactive上下文中提取值;我建议您在一个语句中完成,而不是分散在各处。

P.S. 如果您的初始数据框实际上包含所有变量的character格式,您可以在map_data部分添加mutate_at语句,像这样:

map_data <- reactive({
    #req(input$species)
    df[df$Species %in% input$species, ] %>%
      mutate_at(vars(c("Number_Total", "X", "Y")), funs(as.numeric))

然后,脚本函数也可以在此提供的初始数据框中运行,无需手动更改。


1
你好,它运行了!感谢您提供反馈!非常感谢! - jonas

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接