0
votes

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)
2
Where does datecorrected_totals come from?Srizza
It's the dataset Im using, it's only two columns, a date and a corresponding y value (load) for that datemkb123
I am trying to understand you question. How do you want the plot to change and why do you need two sliders if you only have one variable load?Srizza
Sorry for being unclear! So basically the the date column is the X axis on my plot, and I want the X axis to reflect the selected date range in the dateRangeInput. Now it only shows the entirety of the time span of the data set, but if for example someone selected March 1 through April 15, I would like the X axis to reflect that. As for the sliders, they are for a later part in the project.mkb123
If you add some sample data I can better help you troubleshoot.Srizza

2 Answers

0
votes

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") 
0
votes

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().