Display the results of a datatable from one tab into another and assign it to a list of datasets

I have a shiny application that takes an ID as input from the user and returns the results of the dataframe as a datatable in the "Show data" tab. I am trying to assign the table results from the "Show data" tab to a list in the "Datasets" tab.

app.R

library(shiny)
library(shinyWidgets)
library(dplyr)
library(tidyverse)
library(shinyjs)
library(magrittr)
library(purrr)

ID <- c("A","A","A","A","A","A","B","B","B","B","B","B")
Day <- c("Mon","Mon","Mon","Fri","Fri","Fri","Tue","Tue","Tue","Wed","Wed","Wed")
minute <- c(49,32,15,38,18,16,06,16,26,31,33,38)
second <- c(12,22,08,16,21,42,41,48,32,21,26,18)
hour0 <- c(0,0,0,60,0,0,0,0,0,0,0,0)
hour1 <- c(0,100,0,0,0,0,68,0,0,0,0,0)
hour2 <- c(0,0,0,0,0,0,0,92,0,0,0,72)
hour3 <- c(0,0,92,0,62,0,0,0,81,0,0,0)
hour4 <- c(110,0,0,0,0,0,0,0,0,93,0,0)
hour5 <- c(0,0,0,0,0,112,0,0,0,0,0,0)
hour6 <- c(0,0,0,0,0,0,0,0,0,0,105,0)

df_data <- data.frame(ID,Day,minute,second,hour0,hour1,hour2,hour3,hour4,hour5,hour6,
                      stringsAsFactors=FALSE)


ui <- fluidPage(
  titlePanel("Dataset Tool"),
  sidebarLayout(
    sidebarPanel(width = 3,
                 conditionalPanel(
                   condition = "input.tabs=='Show data'",
                   textInput("id", "Enter ID:", "", 
                             placeholder = "Eg: A"),
                   actionButton("submit", "Go")
                 ),
                 conditionalPanel(
                   condition = "input.tabs=='Datasets'",
                   uiOutput("ui_datasets")
                 )
                 
    ),
    mainPanel(
      tabsetPanel(id = "tabs",
                  tabPanel("Show data",
                           
                           DT::dataTableOutput("dataTable")
                  ),
                  tabPanel("Datasets",
                           DT::dataTableOutput("dataviewer")
                  )
      )
    )
  )
)
  
  server = function(input, output,session) {
    
    r_state <- list()
    r_info <- reactiveValues()
    datasetlist <- c()
    df <- list()
    
    myData <- eventReactive(input$submit,{
      data <- df_data %>%
        modify_if(is.character, as.factor)
      
      data %<>%
        filter(ID == input$id)
    })
    
    observeEvent(input$submit, {
      updateTabsetPanel(session, 
                        inputId = "tableTab",
                        selected = "Show Data"
      )
    })
    
    # Show the data in a table
    output$dataTable <- DT::renderDataTable(
      {
        dat <- myData()
        
        search <- r_state$datatable_state$search$search
        if (is.null(search)) search <- ""
        fbox <- if (nrow(dat) > 5e6) "none" else list(position = "top")
        
        DT::datatable(
          dat,
          selection = "none",
          rownames = FALSE,
          filter = fbox,
          fillContainer = FALSE,
          escape = FALSE,
          style = "bootstrap",
          options = list(
            stateSave = TRUE, ## maintains state
            searchCols = lapply(r_state$datatable_search_columns, function(x) list(search = x)),
            search = list(search = search, regex = TRUE),
            order = {
              if (is.null(r_state$datatable_state$order)) {
                list()
              } else {
                r_state$datatable_state$order
              }
            },
            columnDefs = list(
              list(orderSequence = c("desc", "asc"), targets = "_all"),
              list(className = "dt-center", targets = "_all")
            ),
            autoWidth = TRUE,
            processing = isTRUE(fbox == "none"),
            pageLength = {
              if (is.null(r_state$datatable_state$length)) 10 else r_state$datatable_state$length
            },
            lengthMenu = list(c(5, 10, 25, 50, -1), c("5", "10", "25", "50", "All"))
          ),
          callback = DT::JS('$(window).on("unload", function() { table.state.clear(); })')
        )
        
      }
    )
    
    
    # new_df <- myData()
    new_df <- df_data[0,]
    
    df_filt <- list("Dataset1" = new_df)
    
    df_names <- c("Dataset1")
    for (dn in df_names) {
      df[[dn]] <- df_filt[[dn]]
      datasetlist <- c(datasetlist, dn)
    }
    r_info[["datasetlist"]] <- datasetlist
    
    output$ui_datasets <- renderUI({
      tagList(
        selectInput(
          inputId = "dataset",
          label = "Datasets:",
          choices = r_info[["datasetlist"]],
          multiple = FALSE
        )
      )
    })
    
    
    observeEvent(input$dataviewer_search_columns, {
      r_state$dataviewer_search_columns <<- input$dataviewer_search_columns
    })
    
    observeEvent(input$dataviewer_state, {
      r_state$dataviewer_state <<-
        if (is.null(input$dataviewer_state)) list() else input$dataviewer_state
    })
    
    output$dataviewer <- DT::renderDataTable({
      dat <- df[[(input$dataset)]]
      
      search <- r_state$dataviewer_state$search$search
      if (is.null(search)) search <- ""
      fbox <- if (nrow(dat) > 5e6) "none" else list(position = "top")
      
      
      DT::datatable(
        dat,
        filter = fbox,
        selection = "none",
        rownames = FALSE,
        fillContainer = FALSE,
        escape = FALSE,
        style = "bootstrap",
        options = list(
          stateSave = TRUE, ## maintains state
          searchCols = lapply(r_state$dataviewer_search_columns, 
                              function(x) list(search = x)),
          search = list(search = search, regex = TRUE),
          order = {
            if (is.null(r_state$dataviewer_state$order)) {
              list()
            } else {
              r_state$dataviewer_state$order
            }
          },
          columnDefs = list(
            list(orderSequence = c("desc", "asc"), targets = "_all"),
            list(className = "dt-center", targets = "_all")
          ),
          autoWidth = TRUE,
          processing = isTRUE(fbox == "none"),
          pageLength = {
            if (is.null(r_state$dataviewer_state$length)) 10 else r_state$dataviewer_state$length
          },
          lengthMenu = list(c(5, 10, 25, 50, -1), c("5", "10", "25", "50", "All"))
        ),
        callback = DT::JS('$(window).on("unload", function() { table.state.clear(); })')
      )
    })
    
  }
  
  # Run the application 
  shinyApp(ui = ui, server = server)

I am currently assigning an empty dataframe to be displayed in the "Datasets" tab like this
new_df <- df_data[0,]

but I would like to display the datatable results in "Show data" to this "Datasets" tab and assign it to "Dataset1" list under the list of Datasets.

I tried using the below line to capture and assign the datatable results in "Show data" but not quite getting it right.

new_df <- myData()

Can someone point me in the right direction?

This topic was automatically closed 54 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.