Try the below.
I saved your df as a .xlsx file using the openxlsx package:
library(openxlsx)
df <- structure(
list(date = c("01-08-2021","01-08-2021","01-08-2021","01-08-2021","01-08-2021",
"08-08-2021","08-08-2021","08-08-2021","08-08-2021","08-08-2021","08-08-2021",
"13-08-2021","13-08-2021","13-08-2021","13-08-2021","13-08-2021"),
D1 = c(0,1,0,0,5,0,1,0,0,9,4,3,4,5,6,7), DR01 = c(2,1,0,0,3,0,1,0,1,7,2,3,4,6,7,8),
DR02 = c(2,0,0,0,4,2,1,0,1,4,2,3,4,5,6,7), DR03 = c(2,0,0,2,6,2,0,0,1,5,2,2,4,5,7,5),
DR04 = c(2,0,0,5,6,2,0,0,3,7,2,3,4,5,6,4), DR05 = c(2,0,0,5,6,2,0,0,7,7,2,3,4,5,6,7),
DR06 = c(2,0,0,5,7,2,0,0,7,7,1,3,5,6,7,8), DR07 = c(2,0,0,6,9,2,0,0,7,8,1,3,5,6,4,3)),
class = "data.frame", row.names = c(NA, -16L))
write.xlsx(df, "test.xlsx")
I then used the readxl package to read the file back into your application using the fileInput. I had to change your functions since the date is only available after you have uploaded your file.
library(shiny)
library(shinythemes)
library(dplyr)
library(ggplot2)
library(tidyr)
library(lubridate)
library(readxl)
ui <- fluidPage(ui <-
shiny::navbarPage(
theme = shinytheme("flatly"),
collapsible = TRUE,
br(),
tabPanel("",
sidebarLayout(
sidebarPanel(
fileInput("file1", "Choose XLSX File", accept = ".xlsx"),
uiOutput("date"),
br(),
),
mainPanel(tabsetPanel(tabPanel(
"", plotOutput("Graph", width = "95%", height = "600")
)),)
))
))
server <- function(input, output, session) {
#Function to read your file
fileInput <- reactive({
data <- read_excel(input$file1$datapath)
data$date <- parse_date_time(data$date, c('ymd', 'dmy'))
return(data)
})
output$date <- renderUI({
req(input$file1) #Only render if there is a file uploaded
data <- fileInput()
all_dates <-
seq(as.Date('2021-01-01'), as.Date('2021-01-15'), by = "day")
disabled <-
as.Date(setdiff(all_dates, as.Date(data()$date)), origin = "1970-01-01")
dateInput(
input = "date",
label = "Select Date",
min = min(data$date),
max = max(data$date),
value = '',
format = "dd-mm-yyyy",
datesdisabled = disabled
)
})
output$Graph <- renderPlot({
req(input$date)
plot <-
fileInput() %>%
filter(date == ymd(input$date)) %>%
summarize(across(starts_with("DR"), sum)) %>%
pivot_longer(everything(),
names_pattern = "DR(.+)",
values_to = "val") %>%
mutate(name = as.numeric(name)) %>%
plot(xlab = "Days",
ylab = "Types",
xlim = c(0, 7))
return(plot)
})
}
shinyApp(ui = ui, server = server)