简体   繁体   中英

R Shiny ggplot reactive to dateRangeInput

I am relatively new to R, and I'm trying to build a reactive ggplot in Shiny where the X-axis (dates) is reactive to a dateRangeInput in the UI. I've been googling everywhere, but every thing I try returns an error.

In the ggplot, the aes() calls from a dataset called datecorrected_totals, where x is the dates, and y=load are the two values that I would like to be reactive to the dateRangeInput so the ggplot will adjust the scale based on the period within the daterangeinput.

library(tidyverse)
library(shiny)
library(tidyr)
library(lubridate)
library(zoo)
data <- read_csv("--")

# Define UI ----
ui <- fluidPage(

  titlePanel("--"),

  sidebarLayout(
    sidebarPanel(
      h3("Calculator"), 
      dateRangeInput("dates", label = "Dates",
                     start = ("10-18-2018"),
                     end = max("05-29-2019"),
                     min = min("10-18-2018"),
                     max = max("05-29-2019"),
                     format = "mm-dd-yyyy"),
      sliderInput("slider_a", label = "--",
                  min = 0, 
                  max = 7, 
                  value = 0),
      sliderInput("slider_c", label = "--",
                  min = 7, 
                  max = 42, 
                  value = 7)
    ),
    mainPanel(plotOutput('bar_chart'))

  )
)

# Define server logic ----
server <- function(input, output, session) {

  RE <- reactive({

  })

  output$bar_chart <- renderPlot(

    ggplot(data = datecorrected_totals, aes(x = x, y = load)) +
           geom_bar(stat = "identity")
  )
}


# Run the app ----
shinyApp(ui = ui, server = server)

You need to filter the original dataset by the input dates. In this example data would be your original dataset.

RE <- reactive({
  data %>% 
    filter(x>=input$dates[1] & x<=input$dates[2])
})

output$bar_chart <- renderPlot(

  ggplot(data = RE(), aes(x = x, y = load)) +
    geom_bar(stat = "identity") 

There is no need to create a separate reactive() expression (unless required otherwise). The filter can be applied directly in renderPlot() . Thus, output$bar_chart becomes

  output$bar_chart <- renderPlot(
    datecorrected_totals %>%
      filter(between(x, input$dates[1], input$dates[2])) %>%
      ggplot(aes(x = x, y = load)) +
      geom_bar(stat = "identity")
  )

Below is a self-contained minimal reproducible example :

library(tidyverse)
library(lubridate)
library(shiny)

datecorrected_totals <- tibble(x = seq(as.Date("2018-10-18"), as.Date("2019-05-29"), length.out = 10L),
                               load = day(x))

# Define UI ----
ui <- fluidPage(

  titlePanel("--"),

  sidebarLayout(
    sidebarPanel(
      h3("Calculator"), 
      dateRangeInput("dates", label = "Dates",
                     start = mdy("10-18-2018"),
                     end = mdy("05-29-2019"),
                     min = mdy("10-18-2018"),
                     max = mdy("05-29-2019"),
                     format = "mm-dd-yyyy"),
    ),
    mainPanel(plotOutput('bar_chart'))

  )
)

# Define server logic ----
server <- function(input, output, session) {

  output$bar_chart <- renderPlot(
    datecorrected_totals %>%
      filter(between(x, input$dates[1], input$dates[2])) %>%
      ggplot(aes(x = x, y = load)) +
      geom_col()
  )
}

# Run the app ----
shinyApp(ui = ui, server = server)

Note that the date strings have been coerced to valid Date objects by calling mdy() to avoid error messages.

In addition, geom_bar(stat = "identity") has been replaced by geom_col() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM