我想用R Shiny绘制一些数据点的空间分布图.当我将不同区域的过滤器添加到R脚本中时,它无法在默认 map 上显示所有区域的所有数据点.

对于如何改进 playbook 有什么建议?

下面是我的R脚本和一个示例数据:

library(shiny)
library(shinythemes)
library(leaflet)

# generate the data
dat <- data.frame(
  region=sample(c("A","B","C"),100,replace=T),
  x=rnorm(100, mean=50, sd=10),
  y=rnorm(100, mean=40, sd=10)
)

# Define UI for application that draws a histogram
ui <- shinyUI(fluidPage(
  theme = shinytheme("united"),
  # Application title
  titlePanel("Test"),
  
  # Sidebar
  sidebarLayout(
    sidebarPanel(
      selectizeInput(
        "regionInput", label=h3("Region"),choices=c("A","B","C"))
    ),
    
    # Show a plot of the generated distribution
    mainPanel(
      leafletOutput(outputId = 'map')
    )
  )
)
)

# Define server logic to map the distribution
server <- shinyServer(function(input, output) {
  
  output$map <- renderLeaflet({
    leaflet(dat) %>%
      addProviderTiles("Esri.WorldImagery") %>%
      addCircleMarkers(color = "blue", radius = 2, stroke = FALSE, fillOpacity = 0.5, lng=dat$x[which(dat$region == input$regionInput)], lat=dat$y[which(dat$region == input$regionInput)])
  })
  
  
})

# Run the application 
shinyApp(ui = ui, server = server)

推荐答案

你需要添加一个(All)元素到你的子菜单中,并在渲染函数中添加逻辑,如下所示:

library(shiny)
library(shinythemes)
library(leaflet)

# generate the data
dat <- data.frame(
  region = sample(c("A", "B", "C"), 100, replace = TRUE),
  x = rnorm(100, mean = 50, sd = 10),
  y = rnorm(100, mean = 40, sd = 10)
)

# Define UI for application that draws a histogram
ui <- fluidPage(
  theme = shinytheme("united"),
  # Application title
  titlePanel("Test"),
  
  # Sidebar
  sidebarLayout(
    sidebarPanel(
      selectizeInput(
        "regionInput", 
        label = h3("Region"),
        choices = c("(All)", "A", "B", "C"))
    ),
    
    # Show a plot of the generated distribution
    mainPanel(
      leafletOutput(outputId = "map")
    )
  )
)


# Define server logic to map the distribution
server <- shinyServer(function(input, output) {
  
  output$map <- renderLeaflet({
    if (input$regionInput == "(All)") {
      idx <- rep(TRUE, nrow(dat))
    } else {
      idx <- dat$region == input$regionInput
    }
    leaflet(dat) %>%
      addProviderTiles("Esri.WorldImagery") %>%
      addCircleMarkers(color = "blue",
                       radius = 2, 
                       stroke = FALSE, 
                       fillOpacity = 0.5, 
                       lng = dat[idx, "x"], 
                       lat = dat[idx, "y"])
  })
})

# Run the application 
shinyApp(ui, server)

另一个选项是允许在您的selectizeInput中进行多个 Select :

selectizeInput(
       "regionInput", 
       label = h3("Region"),
       selected = c("A", "B", "C"),
       choices = c("A", "B", "C"),
       multiple = TRUE)

然后将服务器中的逻辑更改为:

idx <- dat$region %in% input$regionInput

然后,您可以 Select 多个区域(例如全部).

R相关问答推荐

如何在弹性表中为类别值的背景上色

将带有范围的字符串转换为R中的数字载体

使用sensemakr和fixest feols模型(R)

使用tidyverse / Mutate的存款账户余额

迭代通过1个长度的字符串长字符R

计算具有奇数日期的运行金额

将饼图插入条形图

如何使用tryCatch执行语句并忽略警告?

计算满足R中条件的连续列

安全地测试文件是否通过R打开

R -如何分配夜间GPS数据(即跨越午夜的数据)相同的开始日期?

远离理论值的伽马密度曲线下面积的近似

将工作目录子文件夹中的文件批量重命名为顺序

为什么函数toTitleCase不能处理english(1),而toupper可以?

以任意顺序提取具有多个可能匹配项的组匹配项

有毒元素与表观遗传年龄的回归模型

对一个数据帧中另一个数据帧中的值进行计数

在R中添加要打印的垂直线

如何创建直方图与对齐的每月箱?

如何在一种 colored颜色 中设置数值变量的 colored颜色 和高于阈值的 colored颜色 点?