Anytime a row is edited on table 1, output row to table 2

I am building a fairly simple Shiny app that has a table on the first tab with an editable column using the rhandsontable library. I want to have a new table created in the 2nd tab that contains any row in which the editable column is not blank.

Here is my code so far:

#----- Load libraries -----#
library(shiny)
library(shinydashboard)
library(DT)
library(shinyWidgets)
library(dplyr)
library(rhandsontable)
#----- Read in dataset -----#

df = mtcars
df$insert_info=NA

#----- Create lists -----#
binary_list = c('Yes','No')

# Define UI for application 

ui <- fluidPage(
  dashboardPage(
    dashboardHeader(title="Draft"),
    dashboardSidebar(
      sidebarMenu(
        menuItem("All Data",
                 tabName = "all_data",
                 icon=icon("table")
        ),
        menuItem("Filtered Data",
                 tabName = "filtered_data",
                 icon = icon("table")
                 
        )
      )
    ),
    dashboardBody(
      tabItems(
        tabItem(
          tabName = "all_data",
          rHandsontableOutput("all_table")
        ),
        tabItem(
          tabName = 'filtered_data',
          rHandsontableOutput("filtered_table")
        )
      ))))

# Define server logic 
server <- function(input, output) {
  
  
  #----------All Data Table----------#
  output$all_table=renderRHandsontable({
    
    
    my_table = rhandsontable(df) %>%
      hot_col(c(1:11), readOnly = TRUE) %>%
      hot_col(12,type='dropdown',source=binary_list) %>%
      hot_context_menu(allowRowEdit = FALSE, allowColEdit = FALSE) %>%
      hot_table(highlightCol = TRUE, highlightRow = TRUE)

    
 
    return(my_table)
    
  })
  
  #----------Filtered Data Table----------#
  
  output$filtered_table=renderRHandsontable({
    #return all_table[,c(1,2,3,12)]
  })
  
}

# Run the application 
shinyApp(ui = ui, server = server)

If I were to select values from the dropdown menus of the last column for the first three rows, then I would want the new table on the 2nd tab to populate with those first three rows.

How should I go about creating this logic? Any help would be appreciated. Thank you!
...

I believe this gets to your desired output.

 #----------Filtered Data Table----------#
  output$filtered_table=renderRHandsontable({
    # filter to rows with final column populated
    out = hot_to_r(input$all_table) %>%
      filter(!is.na(insert_info))
    
    # if rows exist, return all_table[,c(1,2,3,12)]
    if(nrow(out) > 0) {
      rhandsontable(out[,c(1,2,3,12)])
    }
    
  })
1 Like

This is fantastic! Thank you @scottyd22 ! One follow up question:

Let's say I have a new binary list defined as:

binary_list = c('vs', 'am', 'gear', 'carb')which is just a list of column names that feeds into the dropdown for the insert_info column.

How would I configure the logic to populate a 2nd column added on the end of df titled: insert_info_from_column and I wanted to add the value of the selected column from insert_info into the insert_info_from_column column. How would I go about that?

Does this do it?

#----------Filtered Data Table----------#
  output$filtered_table=renderRHandsontable({
    
    # filter to rows with final column populated
    out = hot_to_r(input$all_table) %>%
      filter(!is.na(insert_info)) %>%
      mutate(row = row_number())
    
    # if rows exists, return all_table[,c(1,2,3,12,13)]
    if(nrow(out) > 0) {
      new_values = out[,c(binary_list, 'insert_info', 'row')] %>%
        pivot_longer(cols = c(-'insert_info', -'row')) %>%
        filter(insert_info == name) %>%
        select(row, insert_info_from_column = value)
      
      out = out %>%
        left_join(new_values) %>%
        select(-row)
        
      rhandsontable(out[,c(1,2,3,12,13)])
    }
    
  })
1 Like

That's it! Thank you @scottyd22 !

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.