Upload a csv with fileInput, Filter its data with dateInput and use the results in a Leaflet map

...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)

Hi Pedro,

You're morel likely to get help if you provide a reproducible example. The code you've provided isn't valid R code because of:

  a_data <- eventReactive(input$filedata, {
    <-read.csv2(input$filedata$datapath)
  
  })

It also looks like a lot of that code is unrelated to the problem you're having. It's easy for other people to understand what's wrong if you reduce your example to something smaller. There's some advice at https://mastering-shiny.org/action-workflow.html#making-a-minimal-reprex.

Hello, i'm really sorry, but i cut and pastes this code so many times that it must have gone this way... nevertheless, i ran many times without this sintax mistake and it did not run either. Sorry for not providing a work example, but, any dataset with a date column and any other column will do.
In my specific case, reading the data and putting into a table was not the problem. My problem is to filter by date and update the map.

When I use "data" (raw data inserted with fileInput) the map works...

when I use data_filteres (that is the data with a specific date filter) then everything crashes when I mark the checkbox named "positive"

And other interesting thing, when I read a csv file (without the fileInput but with regular code like read.csv), the date filter worked the way I did before. Nevertheless, I needed that the map let the user insert his data and dynamically filter by date or by any other attribute... i saw dozens of examples in forums but neither of them seem to deal with this specific problem. Is this really impossible to make? I think there is a work around...maybe creating a button to read a script and generate a csv already filtered by date... but this solution won't be as good... i much appreciate any help

The real issue is this part of the code here:

output$mymap <- renderLeaflet({

    leaflet(data_filtered) %>% 
      setView(lng = -45, lat = 22, zoom = 2)  %>% #setting the view over ~ center of Brazil
      addTiles() 

  })



  observe({

    proxy <- leafletProxy("mymap", data = data_filtered )
    proxy %>% clearMarkers()

    if (input$positive == TRUE) {

      proxy %>% clearMarkers() %>% clearShapes()


      proxy %>% addCircles(data = data_filtered(), 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()

    }
  })

proxy <- leafletProxy("mymap", data = data_filtered )

You are missing () after data_filtered ?

Also it will be harder to help you without reprex.
If you say any dataset will do so long as it has x and y. Then why not make such a dataset and provide it ? Also removing all unnecessary parts of app to show the problem will greatly improve your chances of being helped effectively. Sure it's work, but ... It's helpful work.

Hello, i tried with and without the parenthesis.

well, i put the whole code so you could replicate it easily, but ok, i'm not used to post in foruns, up to this point in my life i could pretty much do all i have to only consulting previous doubts, nevertheless, and i'm sorry again. Actually in my last reply i put only the code where the problem lies...but, only this part won't run, so in my head it would harden things for who wants to help me. Nevertheless, there is the full code and there is only where the problem lies.
Here is some sample of my dataset:

"","latitude","longitude","Place","negative","positive","anger","anticipation","disgust","fear","joy","sadness","surprise","trust","Date","class_positive"
"1",-118.25651,34.14437,"Americana",0,1,0,1,0,0,1,1,0,1,2020-04-10,"Weak"
"2",-46.55671,-23.11755,"Atibaia",1,2,1,0,0,1,0,0,0,0,2020-04-10,"Weak"
"3",-48.49018,-1.45573,"Belém",1,1,1,0,0,0,0,0,0,0,2020-04-12,"Weak"
"4",-43.93449,-19.91668,"Belo Horizonte",1,2,1,0,0,2,0,1,0,1,2020-04-11,"Weak"
"5",-47.92182,-15.82669,"Brasília",0,2,0,1,0,1,1,1,1,2,2020-04-11,"Weak"
"6",-34.84301,-7.04026,"Cabedelo",1,1,0,1,0,1,0,0,1,0,2020-04-12,"Weak"

Sorry Pedro, but this is not an efficient way to share data.
you can use dput() function, or the reprex package, or datapasta package.
dput() function is in base R.

ok friend, i'm deeply sorry. Thanks anyway for your help. Just a thought, if I'm here asking for help I'm not a computational ace.

I'm gonna make one more try and if is still inconvenient I once again thank you and I'm gonna try another approach.

I generated a small dataset like I said, although I really don't know if this is efficient. Anyway, you will have to save the csv and then upload it using inputFile in order to replicate. My only objective is to filter the Date from a csv file inserted with inputFile and then make the leaflet understand it.


Df <- data.frame(A = c("Americana", "Rio de Janeiro", "Belo Horizonte"),
                 B = c(2020-04-10, 2020-04-11, 2020-04-10))

write.csv(df, file = "test.csv")

output$mymap <- renderLeaflet({

    leaflet(data_filtered) %>% 
      setView(lng = -45, lat = 22, zoom = 2)  %>% #setting the view over ~ center of Brazil
      addTiles() 

  })



  observe({

    proxy <- leafletProxy("mymap", data = data_filtered )
    proxy %>% clearMarkers()

    if (input$positive == TRUE) {

      proxy %>% clearMarkers() %>% clearShapes()


      proxy %>% addCircles(data = data_filtered(), 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()

    }
  })

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.