Link Calendar with Scatter Plot in Shiny

Viewed 70

I would like you to help me, if possible, to link my calendar to some scatter plots that I want to do. I'm gonna explain.

My idea is that when the person selects the 01/01/2021 option on the calendar, he generates a scatter plot with the first points of each category, that is, D1, DR01, DR02, DR03 and DR04, which are the poitns 3, 2, 6, 5 and 3.

If selected in the calendar 02/01/2021, generate a scatter plot with the points 1,4,3,2 and 5.

Did you get the idea?

I left a structure below the APP.

Thank you very much!

library(shiny)
library(shinythemes)
function.cl<-function(){
  df <- structure(list(date = c("2021-01-01","2021-01-02","2021-01-03","2021-01-04","2021-01-05"),
         D1 = c(3,1,4,5,6), DR01 = c(2,4,5,6,7), DR02 = c(6,3,2,6,1),DR03 = c(5,2,8,9,7),
         DR04 = c(3,5,3,3,7)),class = "data.frame", row.names = c(NA, -5L))

  #Scatter Plot
  
  
  
  
  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)
1 Answers

You can keep the data in long format, filter for the selected date and plot the data.

library(shiny)
library(shinythemes)
library(dplyr)
library(ggplot2)

function.cl<-function(){
df <- structure(list(date = c("2021-01-01","2021-01-02","2021-01-03","2021-01-04","2021-01-05"),
                       D1 = c(3,1,4,5,6), DR01 = c(2,4,5,6,7), DR02 = c(6,3,2,6,1),DR03 = c(5,2,8,9,7),
                       DR04 = c(3,5,3,3,7)),class = "data.frame", row.names = c(NA, -5L))

tidyr::pivot_longer(df, cols = -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({
    req(input$date)
    
    data() %>%
      filter(date == input$date) %>%
      ggplot(aes(name, value)) + geom_point()
  })
  
  
}

shinyApp(ui = ui, server = server)
Related