Reputation: 11
So I just started using R and am trying to make a leaflet app that responds to my user input in the sliders. I have tried to subset the data I am using by using the input from my slider, but it is not working. I get an error 'invalid 'type' (list) of argument'.
I have attached my code below:
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)
Here 'hour_time' is the name of the column in my noise_sample data. It should just give a number, which should be the same as that selected by my slider_hours.
Upvotes: 1
Views: 464
Reputation: 729
It's working but you can add a "validate" function in the server part in case your selection is empty :
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)
Upvotes: 1