Hello,
I'm working on building an interactive shiny app based on multiple datasets that are all similar and can be filtered by the app user by selecting date inputs. In my app, I'm simply trying to display separate data tables using a single reactive function (because the datasets have the same columns).
I've tried calling the dataset in my output
line but that doesnt return variables (see code below). Is it possible to pass multiple datasets similar to what I've tried below.
df1<-data.frame(Group=c("A","B","C","D","E","A","B","C","D","E","A","B","C","D","E"),
ProcessDate=c("2020-01-30","2020-01-30","2020-01-30","2020-01-30","2020-01-30",
"2020-04-30","2020-04-30","2020-04-30","2020-04-30","2020-04-30",
"2019-01-30","2019-01-30","2019-01-30","2019-01-30","2019-01-30"),
Amt=c(500,700,600,400,200,500,600,750,300,950,100,400,600,500,1200))
df2<-data.frame(Group=c("A","B","C","D","E","A","B","C","D","E","A","B","C","D","E"),
ProcessDate=c("2020-01-30","2020-01-30","2020-01-30","2020-01-30","2020-01-30",
"2020-04-30","2020-04-30","2020-04-30","2020-04-30","2020-04-30",
"2019-01-30","2019-01-30","2019-01-30","2019-01-30","2019-01-30"),
Amt=c(500,700,600,400,200,500,600,750,300,950,100,400,600,500,1200))
ui <- dashboardPage(
dashboardHeader(title = "Testing"),
dashboardSidebar(
menuItem("Date Inputs",tabName = "dates",startExpanded = TRUE,
dateInput("date1", "Date1:", value = "2020-01-30",startview = "month",format="M yyyy", autoclose = TRUE),
dateInput("date2", "Date2:", value = "2020-04-30", startview = "month",format="M yyyy", autoclose = TRUE),
dateInput("date3", "Date3:", value = "2019-01-30",startview = "month",format="M yyyy", autoclose = TRUE))),
dashboardBody(
fluidPage(
box(dataTableOutput("df1table")),
box(dataTableOutput("df2table")))))
server <- function(input, output) {
tableformat<-
reactive({
data %>%
filter(ProcessDate==as.Date(anydate(input$date1))|ProcessDate==as.Date(anydate(input$date2))|ProcessDate==as.Date(anydate(input$date3)))%>%
group_by(ProcessDate)%>%
pivot_wider(names_from = ProcessDate,values_from = Amt) %>%
mutate(
QoQ= scales::percent(((as.numeric(.[[4]])-as.numeric(.[[3]]))/as.numeric(.[[3]])),3),
YoY= scales::percent(((as.numeric(.[[4]])-as.numeric(.[[2]]))/as.numeric(.[[2]])),3))%>%
mutate_at(2:4,funs(prettyNum(.,digits=0,big.mark=",")))})
output$df1table<-renderDataTable({
datatable(tableformat(data=df1), options = list(dom = 't'),rownames = FALSE,)})
output$df2table<-renderDataTable({
datatable(tableformat(data=df2), options = list(dom = 't'),rownames = FALSE,)})
}
shinyApp(ui, server)