DTYK
DTYK

Reputation: 1200

Shiny - Using Date range from sliderInput in a reactive dplyr expression used for a ggplot

I am creating a Shiny App showing the trend of various financial metrics for various stocks. The financial metrics for the various stocks are provided at the end of every quarter.

I want to provide my end-users the ability to use a Slider Input to select the date range for their analysis. Previous questions on SO involves using a single value for their slider instead of a range (e.g. this post). Hence, I am unable to replicate the solution.

The following is the packages and the simulated data file I am using. There are 3 columns: (a) Date, (b) Stock, (c) Value for a particular metric.

library(shiny)
library(readxl)
library(dplyr)
library(ggplot2)
library(lubridate)

df <- data.frame(Date = c("30/09/2018", "30/06/2018", "31/03/2018", "31/12/2017", "30/09/2017", "30/06/2017",
                      "31/03/2017", "30/09/2018", "30/06/2018", "31/03/2018", "31/12/2017", "30/09/2017", "30/06/2017",
                      "31/03/2017"),
             Stock = c(rep("AAA", 7), rep("BBB", 7)),
             Value = c(5.1, 5.2, 5.6, 5.5, 5.6, 5.7, 5.6, 6.4, 6.9, 6.7, 7.2, 7.2, 7.2, 7.7))

df$Date <- as.Date(df$Date, format = "%d/%m/%Y")
df$Stock <- as.character(df$Stock)

The following is the user interface:

# Define UI for application
ui <- fluidPage(

  # Application title
  titlePanel("Stock Financials Trend"),

  # Sidebar with slider input to select date range
  sidebarLayout(
    sidebarPanel(
      selectInput("Stock_selector",
                  "Stock:",
                  c("AAA", "BBB")),

      # Add a Slider Input to select date range
      sliderInput("Date_range_selector", "Select Date Range",
                  min = 2017,
                  max = 2018,
                  value = c(2017, 2018))
    ),

    # Show a plot of the trend
    mainPanel(
      plotOutput("plot")
    )
  )
)

The server is as follow:

server <- function(input, output) {

  filtered_df <- reactive({
    df %>%
      filter(Stock == input$Stock_selector & year(Date) == between(year(Date), input$Date_range_selector[1], input$Date_range_selector[2]))
  })

  output$plot <- renderPlot({
    ggplot(filtered_df(), aes_string(x = "Date", y = "Value")) + geom_line() + geom_point() +
  labs(title = paste(input$Stock_selector, "Trend", sep = " "), y = "Value")
  })    
}

# Run the application 
shinyApp(ui = ui, server = server)

My script shows that filtering is done using a dplyr expression which is then assigned to a reactive expression to be used subsequently for plotting with ggplot.

The above script shows a blank output.

I have also tried replacing the numeric values of 2017 and 2018 in the sliderInput function with year(as.Date("2017", format = "%d/%m/%Y")) but the output still fails as well.

The desired output looks something like the following (assuming Stock AAA is selected and the range is set from 2018 to 2018):

Expected output

Thanks!

Upvotes: 2

Views: 4139

Answers (1)

arg0naut91
arg0naut91

Reputation: 14764

You need to remove year(Date) == in your filter statement, i.e change it to:

filtered_df <- reactive({
    df %>%
      filter(Stock == input$Stock_selector & between(year(Date), input$Date_range_selector[1], input$Date_range_selector[2]))
  })

Upvotes: 8

Related Questions