R有光泽反应传单输入

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了R有光泽反应传单输入相关的知识,希望对你有一定的参考价值。

所以我刚开始使用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)

以上是关于R有光泽反应传单输入的主要内容,如果未能解决你的问题,请参考以下文章

r - 空 textInput() 导致传单闪亮应用程序出错

r - 空文本输入()导致传单闪亮应用程序中的错误

如何在闪亮的 R 应用程序中使用传单添加控制输入?

反应传单地图未正确显示

反应传单地图未正确显示

反应传单地图不显示