簡體   English   中英

R閃亮對傳單輸入做出反應

[英]R shiny react to leaflet input

所以我剛開始使用 R 並試圖制作一個傳單應用程序來響應我在滑塊中的用戶輸入。 我試圖通過使用滑塊的輸入對我正在使用的數據進行子集化,但它不起作用。 我收到錯誤“參數的‘類型’(列表)無效”。

我在下面附上了我的代碼:


  titlePanel("Hello Shiny!"),

  sidebarLayout(

    sidebarPanel(

      selectizeInput(inputId = 'lsoa', 
                     label = 'Choose your lsoa', 
                     choices = c('Ealing' = 'ealing', 
                                 'Camden' = 'camden') , 
                     selected = 'camden', multiple = TRUE),

      uiOutput(outputId = 'time_var'),

      sliderInput("Date_of_year",
                  "Dates",
                  min = as.Date("2017-09-01","%Y-%m-%d"),
                  max = as.Date("2018-07-31","%Y-%m-%d"),
                  value=as.Date("2017-09-01"),
                  timeFormat="%Y-%m-%d"),
      uiOutput(outputId = 'datevar'),


      sliderInput("slider_hours", "Hours:", min=0, max=23, value=0, step = 1),

      uiOutput(outputId = 'hour_var')

      # sliderInput("slider_mins", "Mins:",min = 0, max = 45, value = 0, step = 15),
      # 
      # uiOutput(outputId = 'min_var')


    ),


    mainPanel(

      leafletOutput(outputId = "map")

    )
  )
)


server <- function(input, output) {

  output$map <- renderLeaflet({
    m <- leaflet() %>% 

      addTiles()%>%
      setView(lng = -0.1911, lat = 51.5371, zoom = 11)%>%
      addMarkers(data = subset(noise_sample, hour_time == input$slider_hours ),
        lng = ~longitude, 
        lat = ~latitude, 
        popup = ~as.character(lpaeq_T), 
        label = ~as.character(lsoa11nm))%>%
      addPolygons(data = subset(main_shape, grepl(paste(input$lsoa, collapse = '|'), 
                                                  tolower(lsoa11nm))), 
                  color = "#444444", 
                  weight = 1, 
                  smoothFactor = 0.5,
                  opacity = 1.0, 
                  fillOpacity = 0.5)
   m
  })
}

shinyApp(ui, server)

這里的 'hour_time' 是我的 noise_sample 數據中列的名稱。 它應該只給出一個數字,該數字應該與我的 slider_hours 選擇的數字相同。

它正在工作,但您可以在服務器部分添加“驗證”功能,以防您的選擇為空:

noise_sample <- tibble("longitude" = c(-0.1914,-0.1943), "latitude"= c(51.5371,51.6),
                   "lpaeq_T"= c("toto","tata"), "lsoa11nm"= c("toto","tata"),
                   "hour_time" = c(1,2))
ui <- fluidPage(
     titlePanel("Hello Shiny!"),
 sidebarLayout(
 sidebarPanel(
selectizeInput(inputId = 'lsoa', 
               label = 'Choose your lsoa', 
               choices = c('Ealing' = 'ealing', 
                           'Camden' = 'camden') , 
               selected = 'camden', multiple = TRUE),
uiOutput(outputId = 'time_var'),
sliderInput("Date_of_year",
            "Dates",
            min = as.Date("2017-09-01","%Y-%m-%d"),
            max = as.Date("2018-07-31","%Y-%m-%d"),
            value=as.Date("2017-09-01"),
            timeFormat="%Y-%m-%d"),
uiOutput(outputId = 'datevar'),
sliderInput("slider_hours", "Hours:", min=0, max=23, value=1, step = 1),
uiOutput(outputId = 'hour_var')
# sliderInput("slider_mins", "Mins:",min = 0, max = 45, value = 0, step = 15),
# 
# uiOutput(outputId = 'min_var')
),
 mainPanel(
     leafletOutput(outputId = "map")
 )
)
)

server <- function(input, output) {
  output$map <- renderLeaflet({

  data1 <- subset(noise_sample, hour_time == input$slider_hours)

validate(
  need(dim(data1)[1] >0, "No data")
)

 m <- leaflet() %>% 
  addTiles()%>%
  setView(lng = -0.1911, lat = 51.5371, zoom = 11)%>%
  addMarkers(data = data1,
             lng = ~longitude, 
             lat = ~latitude, 
             popup = ~as.character(lpaeq_T), 
             label = ~as.character(lsoa11nm))
# %>%
#   addPolygons(data = subset(main_shape, grepl(paste(input$lsoa, collapse = '|'), 
#                                               tolower(lsoa11nm))), 
#               color = "#444444", 
#               weight = 1, 
#               smoothFactor = 0.5,
#               opacity = 1.0, 
#               fillOpacity = 0.5)
m
  })
}

shinyApp(ui, server)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM