Hello everyone,
I'm working on a shiny app. I want to filter data from our database using a json interface (webjason). I'm having trouble setting up the filter module so that I can actually see the measured values in my graph. Can anyone help me?
You go through the filter from top to bottom and then when you click on "Apply filter (Filter anwenden)". you see the filtered data in the plot.
####Filtermodul - ui und server####
#Datenabfrage
Parameterdaten<-data.frame(fromJSON("http://XXX.XX.XX.XX/WebJSON/parameters?program=RMP_Chemie"))
Parametergruppen<-data.frame(fromJSON("http://XXX.XX.XX.XX/WebJSON/parameterGroups?"))
####Dashboard-Ui####
dashboardBody(
tabItems(
tabItem(tabName = "Datenvisualisierung",
fluidPage(
sidebarLayout(
sidebarPanel(
style = "background-color: #CCCCCC;",
selectInput("station", "Messstation auswählen", choices = unique(statdaten$monitoringSites.name), multiple = TRUE),
selectInput("parameterAuswahl", "Parameter auswählen:",
choices = unique(Parameterdaten$parameters.name), multiple = TRUE),
dateRangeInput("datumsAuswahl", "Datum auswählen:",
start = "2023-01-01", end = "2023-12-31",
format = "dd.mm.yyyy"),
actionButton("filter_btn1", "Filter anwenden")),
mainPanel(
box(
width = 12,
title = "Grafikübersicht",
heigth = "200px",
plotlyOutput("plotly_plot")
))),
###server####
####Filtermodul####
filtered_data <- eventReactive(input$filter_btn1, {
filtered <- data
if (!is.null(input$station) && input$station != "Alle") {
filtered <- filtered[filtered$Messstation == input$station, ]
}
if (!is.null(input$stoff) && input$stoff != "Alle") {
filtered <- filtered[filtered$Stoff == input$stoff, ]
}
filtered <- filtered[filtered$Jahr >= input$jahr[1] & filtered$Jahr <= input$jahr[2], ]
return(filtered)
)}
filter date
filtered_data <- eventReactive(input$filter_btn1, {
filtered_data <- daten %>%
filter(Datum >= input$datumsAuswahl[1] & Datum <= input$datumsAuswahl[2])
if (input$phaseAuswahl != " ") {
filtered_data <- filtered_data %>%
filter(Phase == input$phaseAuswahl)
}
if (input$parameterAuswahl != " ") {
filtered_data <- filtered_data %>%
filter(Parameter == input$parameterAuswahl)
}
filtered_data
})
output$plotly_plot <- renderPlotly({
plot_ly(data, x = ~Datum, y = ~Wert, type = 'scatter', mode = 'lines') %>%
layout(title = "Kupfer (mg/l)",
xaxis = list(title = "Datum"),
yaxis = list(title = "Wert"))
})