I have a dataset :
vru_line call_id customer_id priority type date
AA0101 38080 50395060 2 PS 4/1/2019
AA0101 38080 50395060 0 PS 4/1/2019
AA0101 38081 50353564 2 NW 4/1/2019
AA0102 38082 13000567 2 PS 4/2/2019
AA0102 38083 50395060 1 NW 4/2/2019
AA0102 38084 50353564 0 PS 4/2/2019
AA0103 38085 50353564 1 NW 4/3/2019
AA0103 38086 13000567 1 PS 4/3/2019
AA0103 38087 13000567 0 NW 4/3/2019
Here date is in (mm/dd/yyyy) format.
What I am trying to do using flexdashboard and shiny
is to subset the dataset depending upon the conditions:
- Filter either for "ALL" vru_line or for Selected one from a drop down menu.
- Filter between selected date range
The code below is from the project I am working on:
library(DT)
library(data.table)
library(dplyr)
library(shiny)
library(shinydashboard)
library(tidyverse)
library(stringr)
library(lubridate)
library(anytime)
# this will create a "File Upload" button
fileInput("file1", "Load File:",
accept = c("text/csv", "text/comma-separated-values, text/plain", ".csv", "text/tab-separated-values", ".tsv") )
checkboxInput("header", "Is first row the Header?", TRUE)
data_set <- reactive({
req(input$file1)
inFile <- input$file1
data_set <- read.csv(inFile$datapath, header=input$header, stringsAsFactors = F)
})
# Date input placeholder for date selection
dateRangeInput("dateRange", "Select Start & End Dates:",
start = "2019-04-01", end = "2019-04-02",
separator = " - ", format = "yyyy-mm-dd")
hr()
observe({
require(dplyr)
req(input$file1)
choices = c("ALL", unique(as.character(data_set()$vru_line)))
updateSelectInput(session,"VarRep",
label = "Select VRU",
choices = choices, selected=choices[1])
})
selectInput(inputId = "VarRep", label = "Select VRU",
multiple = FALSE, choices = list("ALL"), selected = "ALL" )
hr()
# click button
actionButton("displayRes","Display")
# Should select the sebset of data depending on the coditions above and below
eventReactive(input$displayRes, {
output$VarReptbl <- renderDataTable({
if(input$VarRep == "ALL") {
data_set() %>% select(everything()) %>%
filter(anydate(data_set()$date) >= anydate(input$dateRange[1]) &
anydate(data_set()$date) <= anydate(input$dateRange[2])) %>% datatable()
} else {
data_set() %>% select(everything()) %>%
filter(as.character(data_set()$vru_line) == input$VarRep) %>%
filter(anydate(data_set()$date) >= anydate(input$dateRange[1]) &
anydate(data_set()$date) <= anydate(input$dateRange[2])) %>% datatable()
}
})
})
hr()
dataTableOutput("VarReptbl")
There is no error in the code being displayed and when I import the data and click on the display button, no table is returned.
Not sure where I am doing wrong? The code looks ok though.
What is expected that if I select one of the options from drop down menu and pick two dates then the data should be filtered as a subset and displayed as the datatable.