2

I'm trying to create a shiny dashboard that allows the user to select a csv file. The file contains only two columns that are order number and dateCreated. I want the user to be able to in addition, select the date range that they desire and get a summary count statistic.

So far my code is as follows:

library(shiny)
library(plotly)
library(colourpicker)
library(ggplot2)


ui <- fluidPage(
  titlePanel("Case Referrals"),
  sidebarLayout(
    sidebarPanel(
      fileInput("file", "Select a file"),
      sliderInput("period", "Time period observed:",
                  min(data()[, c('dateCreated')]), max(data()[, c('dateCreated')]),
                  value = c(min(data[, c('dateCreated')]),max(data()[, c('dateCreated')])))
    ),
    mainPanel(
      DT::dataTableOutput("table")
    )
  )
)

# Define the server logic
server <- function(input, output) {
  
  # file input
  input_file <- reactive({
    if (is.null(input$file)) {
      return("")
    }
  })
  
  
  # summarizing data into counts
  data <- input_file()
  data <- subset(data, dateCreated >= input$period[1] & dateCreated <= input$period[2])


  output$table <- DT::renderDataTable({
    data
  })
  
  
  
}

shinyApp(ui = ui, server = server)

I get an error message saying:

Error in data()[, c("dateCreated")] : incorrect number of dimensions

Can anyone help me understand what the problem might be and/or provide a better framework for doing this? And to be clear in the csv file, the createDate variable is broken down into individual days for when the order was placed.

Thank you!

mnist
  • 6,571
  • 1
  • 18
  • 41
fkn_ez
  • 55
  • 1
  • 9

1 Answers1

4

I added comments to the faulty steps.

library(shiny)


ui <- fluidPage(
  titlePanel("Case Referrals"),
  sidebarLayout(
    sidebarPanel(
      fileInput("file", "Select a file"),
      
      # you cannot call data() in your ui. 
      # You would have to wrap this in renderUI inside of your server and use
      # uiOutput here in the ui
      sliderInput("period", "Time period observed:", min = 1, max = 10, value = 5)
    ),
    mainPanel(
      DT::dataTableOutput("table")
    )
  )
)

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

  input_file <- reactive({
    if (is.null(input$file)) {
      return("")
    }

    # actually read the file
    read.csv(file = input$file$datapath)
  })

  output$table <- DT::renderDataTable({

    # render only if there is data available
    req(input_file())

    # reactives are only callable inside an reactive context like render
    data <- input_file()
    data <- subset(data, dateCreated >= input$period[1] & dateCreated <= input$period[2])

    data
  })



}

shinyApp(ui = ui, server = server)
mnist
  • 6,571
  • 1
  • 18
  • 41
  • Thank you. This helps resolve my issue. Is there a way to change the slider to be in a date format? – fkn_ez Nov 29 '21 at 22:10
  • If so, please accept. Fpr date inputs see [here](https://shiny.rstudio.com/reference/shiny/0.14/dateRangeInput.html) – mnist Nov 29 '21 at 22:13
  • sorry, forgot to ask... how do i get the aggregated counts for the date range selected? I tried: `data <- data %>% group_by(dateCreated) %>% summarise(case_count = n()) ` but it doesn't work. – fkn_ez Nov 29 '21 at 23:01
  • please ask a different question for a different problem – mnist Nov 29 '21 at 23:02
  • It was part of my original question. I need to provide count statistics for the data range selected by the user. – fkn_ez Nov 29 '21 at 23:08
  • you had shiny specific errors that I addressed. I cannot help fixing code without data. Please ask a different question that strips the shiny part and provide a [minimal reproducible example](https://stackoverflow.com/q/5963269/8107362). Especially, provide some sample data, e.g. with `dput()` and use the [reprex-package](https://reprex.tidyverse.org/). – mnist Nov 29 '21 at 23:39