R有光泽反应传单输入

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

所以我刚开始使用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选择的数字相同。

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

它正在工作,但您可以在服务器部分添加“验证”功能,以防您的选择为空:

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)
© www.soinside.com 2019 - 2024. All rights reserved.