使用 selectInput 的闪亮仪表板和传单

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

我正在尝试使用带有传单包的闪亮仪表板。我尝试使用仪表板中的“SelectInput”功能根据所选的输入(地理区域)创建反应式地图。但是,我无法使传单和 SelectInput 相互连接。

我还想区分数据集中的两组并将其绘制在传单/闪亮中(列名称“up.and.down”具有正值和负值)。在Base R中,我可以使用tidyverse中的过滤器选项来做到这一点包装并为每个提供不同的颜色,但我不确定这在 Shinydashboard 中如何工作。

library(shiny)
    library(shinydashboard)
    library(shinydashboardPlus)
    library(leaflet)
    library(tidyverse)


    
datafile<- read.csv("/Users/prabeshkc/Desktop/stackoverflow data.csv")

`ui<- dashboardPage(
  skin = "blue",
  dashboardHeader(title = "Cluster Map"),
  dashboardSidebar(
    selectInput("Area",label = "Geo Area",
                choices = datafile$geoArea)
  ),
  dashboardBody(
    fluidRow(box(width = 12,leafletOutput(outputId = "mymap")))
  ))`
    
`server <- function(input, output) {
 data_input<-reactive({
   datafile %>% 
     leaflet() %>% 
     addTiles() %>% 
     addMarkers(lng = datafile$lng,lat = datafile$lat)
 })
  data_input_ordered<-reactive({
    data_input()[order(match(data_input)$geoArea)]
  })`

  `labels<- reactive({
    paste("<p>", datafile$goals,"</p>"),
    paste("<p>", datafile$achieved,"</p>")
  })`
  
 `output$mymap<- renderLeaflet(
    datafile %>% 
      leaflet() %>% 
      addTiles() %>% 
      addCircleMarkers(lng = datafile$lng,lat = datafile$lat)
  )
}
 shinyApp(ui = ui, server = server)`
r shiny r-leaflet shiny-reactivity
1个回答
1
投票

试试这个:

library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
library(leaflet)
library(tidyverse)


datafile<-read.csv("/Users/prabeshkc/Desktop/stackoverflow data.csv") %>% 
  mutate(color=ifelse(up.and.down<0,"red","blue"))

ui<- dashboardPage(
  skin = "blue",
  dashboardHeader(title = "Cluster Map"),
  dashboardSidebar(
    selectInput("Area",label = "Geo Area",
                choices = datafile$geoArea)
  ),
  dashboardBody(
    fluidRow(box(width = 12,leafletOutput(outputId = "mymap")))
  ))

server <- function(input, output) {

  output$mymap<- renderLeaflet({
    validate(need(datafile,"Add file"))
    validate(need(input$Area,"Select Area"))
    datafile %>% 
      filter(geoArea %in% input$Area) %>% 
      leaflet() %>% 
      addTiles() %>% 
      addCircleMarkers(lng = ~lng,lat = ~lat,color=~color,
                       popup = ~paste(paste0("Goals - ",goals),paste0("Achieved - ",achieved), sep="<br>"))
  })
}
shinyApp(ui = ui, server = server)
© www.soinside.com 2019 - 2024. All rights reserved.