...Hello!
i Want to upload a file to my shiny app, them i want to filter this file using dateInput and add the result to a Leaflet map, Nevertheless, I can't find an example and all my tries did not work. It says the file is not subsetable. I would greatly appreciate any help!
Here is my code:
library(shiny)
library(shinydashboard)
library(leaflet)
library(RColorBrewer)
library(leaflet.extras)
library(rgdal)
library(DT)
ui <- fluidPage(
titlePanel(p("Spatial app", style = "color:#3474A7")),
sidebarLayout(
sidebarPanel(
fileInput(
inputId = "filedata",
label = "Upload data. Choose csv file",
accept = c(".csv")
),
checkboxInput(
inputId = "positive",
label = "Positive",
value = FALSE
),
checkboxInput(
inputId = "class_positive",
label = "class",
value = FALSE
),
dateInput(inputId = "date",
label="Insira a Data",
value = NULL,
min=Sys.Date()-20,
max = Sys.Date()+20,
textOutput("seldate"),
format = "yyyy-mm-dd"
),
p("Made with", a("Shiny",
href = "http://shiny.rstudio.com"
), "."),
img(
src = "imageShiny.png",
width = "70px", height = "70px"
)
),
mainPanel(
leafletOutput(outputId = "map"),
leafletOutput("mymap", height = 500),
DTOutput(outputId = "table")
)
)
)
server <- function(input, output) {
data <- reactive({
read.csv(input$filedata$datapath)
})
output$table <- renderDT(data())
output$mymap <- renderLeaflet({
leaflet(cities) %>%
setView(lng = -45, lat = 22, zoom = 2)
addTiles()
})
rawData <- eventReactive(input$filedata, {
read.csv(input$filedata$datapath)
})
pal <- reactive({colorNumeric(
palette = c('gold', 'orange', 'dark orange', 'orange red', 'red', 'dark red'),
domain = req(rawData$positive))
})
pal2 <- reactive({colorFactor(
palette = c('blue', 'yellow', 'red'),
domain = req(rawData$class_positive))
})
a_data <- eventReactive(input$filedata, {
<-read.csv2(input$filedata$datapath)
})
observe({
proxy <- leafletProxy("mymap", data = a_data )
proxy %>% clearMarkers()
if (input$positive == TRUE) {
proxy %>% clearMarkers() %>% clearShapes()
proxy %>% addCircles(data = a_data(), lat = ~ longitude,
lng = ~ latitude, weight = 1,
radius = ~sqrt(positive)*25000,
popup = ~Place,
label = ~Place,
color = ~pal, fillOpacity = 0.5)
}
else {
proxy %>% clearMarkers() %>% clearControls() %>% clearMarkerClusters() %>% clearShapes()
}
})
}
shinyApp(ui = ui, server = server)