I'm looking for a good tutorial so that the data imported into a shiny app can feed a dataframe. I mainly want to filter csv data from widget. At the end they will have to be displayed on a geojson map background (I don't know how I'm going to do it yet). So to start what is the best way to filter the data from my uploaded csv?
We assume that the header contains name;meta1;meta2;meta3;date_start;date_end;value1;value2
For example, I would like to keep name, value1 and meta2 data for certain dates (date_start to date_end)
Regards
For the moment I manage to load the csv and here is my starting point in the server part :
Welcome to the community @chris3522! Below is an example app that demonstrates a couple of the concepts you describe. The file that's uploaded is stored in a reactive named my_data. This is the data frame that will drive the app's two outputs. First, a table of the raw data is displayed (i.e. the sample csv that was created). Second, the my_data() reactive is filtered via the date inputs and then displayed. The filter was applied within the second table output to show a comparison between the raw and filtered data. The date filter could have been added directly within the my_data() reactive right after it was initially read in. I hope this helps.
library(shiny)
library(dplyr)
library(lubridate)
ui <- fluidPage(
fileInput('file', 'Upload Data'),
dateRangeInput('date', 'Select date range',
start = today() - days(7),
end = today(),
max = today()),
br(),
HTML('Raw Uploaded Data'),
tableOutput('uploaded_data'),
br(),
HTML('Filtered Data'),
tableOutput('filtered_data')
)
server <- function(input, output, session) {
# raw data from upload
my_data = reactive({
req(input$file)
read.csv(input$file$datapath, sep = ',')
})
# table of raw uploaded data
output$uploaded_data = renderTable({
my_data()
})
# table of data filtered by date inputs
output$filtered_data = renderTable({
req(input$date)
my_data() %>%
filter(as.Date(date_start) >= input$date[1] &
as.Date(date_end) <= input$date[2]
) %>%
select(name, value1, meta2)
})
}
shinyApp(ui, server)
Thanks Scotty for the ideas. I'm making progress slowly but surely. Now I'm looking at the lesson 5 tutorial (Shiny - Use R scripts and data) and I don't understand how the basemap is set by helpers.R functions.
I would like to choose another basemap from a geojson file of polygons that I could spatially join with the filtered dataframe. I like the idea of sticking to the tutorial to perform this action. So how do I define the basemap of my polygons in helpers.R?
I finally succeeded but I still have problems with a reactive variable.
Here is the code, I would like "max" to be a reactive parameter and replace it with "parametre()" but I have an error: 'x' must be numeric if I replace fillColor = ~pal(max) with fillColor = ~pal(parameter())
geojsonWithValueAdded() is my geojson filtered with data and now it contains a "max" property, it is reactive.
parametre <- reactive(input$param)
output$mymap <- renderLeaflet({
pal <- colorBin(
palette = "viridis",
domain = geojsonWithValueAdded()$parametre_choix,
reverse = TRUE
)
leaflet(geojsonWithValueAdded()) %>% addTiles() %>% setView(lng = 2.80, lat = 46.80, zoom = 5) %>% addPolygons(
data = geojsonWithValueAdded(),
label = ~labelBV,
fill = TRUE,
# Application de la fonction palette
fillColor = ~pal(max),
popup = ~paste0("max : ", max, " mm"),
fillOpacity = 0.8)
})
If input$param is returning a character number (i.e. "5"), you could try the code below. Notice, since parametre is a reactive that is only returning the value of the input, it can be eliminated and the input can be passed to the function directly.