Shiny应用点击按钮后新标签页无法展示数据框完整行问题排查
Fix: Display Filtered Data in Shiny Tab After Saving
Let's break down why your current code isn't working, then fix it step by step:
The Root Issues
- Your
newDFis a local variable inside thesaveDatafunction — it doesn't exist outside that function, sooutput$contentFinalcan't access it. - The
output$contentFinalrenderer isn't set up to react to changes when you click the save button; it's trying to return a variable that doesn't exist in its scope.
Modified Working Code
Server.R
library(dplyr) library(rhandsontable) options(shiny.maxRequestSize = 9*1024^2) function(input, output) { # Initialize reactive values to store both raw and filtered data values <- reactiveValues( rawData = data.frame(Post = c("", ""), list2 = c(12,13)), filteredData = NULL ) # Render the editable table in Summary tab output$contents <- renderRHandsontable({ rhandsontable(values$rawData, width = 550, height = 300) %>% hot_col(col = "Post", type = "dropdown") }) # Function to process and save filtered data saveData <- function(){ # Get the current table data from the handsontable finalDF <- hot_to_r(input$contents) # Remove rows with missing values values$filteredData <- finalDF[complete.cases(finalDF),] # Update the raw data to keep edits (optional, if you want to persist edits in Summary tab) values$rawData <- finalDF } # Render the filtered data in Tab tab output$contentFinal <- renderRHandsontable({ if(is.null(values$filteredData)){ # Show a message if no data has been saved yet rhandsontable(data.frame(Message = "Click 'Save changes' in Summary tab to see filtered data"), width = 550, height = 300, readOnly = TRUE) } else { rhandsontable(values$filteredData, width = 550, height = 300, readOnly = TRUE) } }) # Trigger saveData when the button is clicked observeEvent(input$saveBtn, saveData()) }
UI.R
library(rhandsontable) fluidPage( # App title ---- titlePanel("Tabsets"), # Sidebar layout with input and output definitions ---- sidebarLayout( # Sidebar panel for inputs ---- sidebarPanel( ), # Main panel for displaying outputs ---- mainPanel( # Output: Tabset w/ plot, summary, and table ---- tabsetPanel(type = "tabs", tabPanel("Summary", rHandsontableOutput('contents'), actionButton("saveBtn", "Save changes") ), tabPanel("Filtered Data", rHandsontableOutput('contentFinal')) ) ) ) )
Key Changes Explained
- Added
values$filteredDatato the reactiveValues object: This acts as a shared "storage" that both the save function and the output renderer can access. When you click save, we update this value, and the output automatically re-renders. - Updated
saveDatato assign the filtered data tovalues$filteredDatainstead of a local variable. - Made
output$contentFinalreactive tovalues$filteredData: It checks if data exists, shows a helpful message if not, and renders the filtered table once saved. - Optional: Added
values$rawDatato persist edits in the Summary tab after saving (so your edits don't reset when you switch tabs).
内容的提问来源于stack exchange,提问作者Henk Straten
相关产品推荐
相关产品推荐

