Passing datasets through a single reactive function w/ separate output tables

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)




you are mixing up regular R functions and their uses with reactive expressions.
if you want tableformat to be a function such that it can accept a data param that you set to be either df1 or df2 when you call it, that would be a traditional r function. and you leave reactive out of it.
However, given your use case, you would have to hoist up the other reactive params to conventional params, and pass them also. i.e. input$date1's content should be passed as a param to tableformat function, you get your reactivity within the renderDataTable expression, at the point where you call your normal function, because at that point you pass call possibly tableformat(data=df1,id1=input$date1) etc. and its the calling context that gives your the reactivity rather than tableformat itself (which does not need reactivity)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.