Greconomist
Greconomist

Reputation: 396

Link data with dateRangeInput in Shiny

    ## app.R ##
    library(shiny)
    library(shinydashboard)
    library(ggplot2)
    library(data.table)

    ui <- dashboardPage(
      dashboardHeader(title = "Smart Survey Insights"),
      dashboardSidebar(
        dateRangeInput("dateRange", "Please choose date", "2017-01-01", NULL),
        checkboxInput("comparePreviousPeriod", "Compare to previous period")
      ),
      dashboardBody(
        fluidRow(
          column(12, plotOutput("boxPlot")),
        )
      )
    )

    server <- function(input, output) { 


    mydata <- data_frame(ended = c("14/11/2016 13:37", "14/11/2016 13:37",
"14/11/2016 13:47", "14/11/2016 13:51", "14/11/2016 13:51"),
task = c("Find licensing information", "Sign in to my existing account",
"Sign in to my existing account", "Bus registrations", "Make changes to my existing Operator’s Licence"), taskLevelOfDifficulty = c("Extremely difficult", "Extremely easy", "Neither easy nor difficult", "Extremely difficult", "Extremely easy"))

   mydata <- mydata  %>% mutate(ended = as.Date(ended, format = "%Y-%m-%d"))




      filteredData <- mydata %>% filter(ended >= input$date_range[1] && mydata$ended <= input$date_range[2])
      output$boxPlot <- renderPlot(ggplot(filteredData, aes(taskLevelOfDifficulty))) + geom_bar()

      }

    shinyApp(ui, server)

My piece of code doesn't seem to work, the issue is that my "ended" column has the 14/11/2016 13:37 format whereas dateRangeInput has the yyyt-mm-dd format. I don't know how to link my graph with the the dateRangeInput.

Upvotes: 0

Views: 594

Answers (1)

Pork Chop
Pork Chop

Reputation: 29397

I had to fix quite a bit of your code so please have a look and make sure you understand the filtering and the dependencies on the output. For example purposes I moved dataframe outside the server

library(shiny)
library(shinydashboard)
library(ggplot2)
library(data.table)
library(dplyr)

mydata <- data.frame(ended = c("14/11/2016 13:37", "15/11/2016 13:37","16/11/2016 13:47", "17/11/2016 13:51", "18/11/2016 13:51"),
                     task = c("Find licensing information", "Sign in to my existing account",
                              "Sign in to my existing account", "Bus registrations", "Make changes to my existing Operator’s Licence"), 
                     taskLevelOfDifficulty = c("Extremely difficult", "Extremely easy", "Neither easy nor difficult", "Extremely difficult", "Extremely easy"))

mydata <- mydata  %>% mutate(ended = as.Date(ended, format = "%d/%m/%Y"))

ui <- dashboardPage(
  dashboardHeader(title = "Smart Survey Insights"),
  dashboardSidebar(
    dateRangeInput("dateRange", "Please choose date", "2016-11-14", "2016-11-15"),
    checkboxInput("comparePreviousPeriod", "Compare to previous period")
  ),
  dashboardBody(
    fluidRow(
      column(12, plotOutput("boxPlot"))
    )
  )
)

server <- function(input, output) { 

  filteredData <- reactive({
    req(input$dateRange)
    mydata[mydata$ended >= input$dateRange[1] & mydata$ended <= input$dateRange[2],]
  })

  output$boxPlot <- renderPlot(ggplot(filteredData(), aes(taskLevelOfDifficulty)) + geom_bar())
}

shinyApp(ui, server)

enter image description here

Upvotes: 2

Related Questions