I am trying to make a shiny app that first imports data in the first tab and plots a ggplot
based on the imported data and the user-chosen data range in the second tab. The below code doesn't give me an error, but my plot is not getting updated as I change my date range.
library(shiny)
library(ggplot2)
library(dplyr)
df <- data.frame("date" = c("2020-01-01", "2020-01-01", "2020-01-02", "2020-01-02"), "id" = c("A", "A", "B", "B"), "number" = c(1, 3, 2, 5))
df
write.csv(df, "df.csv", row.names = F)
if (interactive()) {
ui <- fluidPage(
fileInput("file", "Upload Your File:"),
uiOutput("daterange"),
plotOutput("plot")
)
server <- function(input, output, session) {
df <- reactive({
req(input$file)
inFile <- input$file
read.csv(inFile$datapath, stringsAsFactors = F)
})
output$daterange <- renderUI({
dateRangeInput("daterange", "Select the date range:",
start = as.Date(min(df()$date), format = "%m/%d/%Y"),
end = as.Date(max(df()$date), format = "%m/%d/%Y"),
min = as.Date(min(df()$date), format = "%m/%d/%Y"),
max = as.Date(max(df()$date), format = "%m/%d/%Y")
)
})
output$plot <- renderPlot({
dfplot <- subset(df(), date >= input$daterange[1] & date <= input$daterange[2])
g <- ggplot(df(), aes(id, number)) +
geom_boxplot() +
theme_classic()
print(g)
})
}
shinyApp(ui=ui, server = server)
}