I have a function where it generates a scatter plot according to the selected date. As you can see I have three days 01/08, 08/08 and 13/08. That is, there are three different scatter plots. However, on shiny I can only show one, which is from the 01/08.
Could you help me tweak the code so that when it picks one of those three days on the calendar, it shows the correct scatterplot on shiny?
This resolved question may help: Link Calendar with Scatter Plot in Shiny
Executable code below:
library(shiny)
library(shinythemes)
library(dplyr)
library(ggplot2)
library(tidyr)
library(lubridate)
function.cl<-function(){
df <- structure(
list(date = c("2021-08-01","2021-08-01","2021-08-01","2021-08-01","2021-08-01",
"2021-08-08","2021-08-08","2021-08-08","2021-08-08","2021-08-08","2021-08-08",
"2021-08-13","2021-08-13","2021-08-13","2021-08-13","2021-08-13"),
Week= c("Sunday","Sunday","Sunday","Sunday","Sunday","Sunday","Sunday","Sunday",
"Sunday","Sunday","Sunday","Friday","Friday","Friday","Friday","Friday"),
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))
scatter_date <- function(dt, dta = df) {
dta %>%
mutate(date = ymd(date)) %>%
filter(date == ymd(dt)) %>%
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))
}
Plot1<-scatter_date("2021-08-01")
return(list(
"Plot1" = Plot1,
date = df$date
))
}
ui <- fluidPage(
ui <- shiny::navbarPage(theme = shinytheme("flatly"), collapsible = TRUE,
br(),
tabPanel("",
sidebarLayout(
sidebarPanel(
uiOutput("date"),
br(),
),
mainPanel(
tabsetPanel(
tabPanel("",plotOutput("Graph",width = "95%", height = "600"))),
))
)))
server <- function(input, output,session) {
data <- reactive(function.cl())
output$date <- renderUI({
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 = max(data()$date),
format = "dd-mm-yyyy",
datesdisabled = disabled)
})
output$Graph <- renderPlot({
function.cl()[["Plot1"]]
})
}
shinyApp(ui = ui, server = server)