You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

  1. Your newDF is a local variable inside the saveData function — it doesn't exist outside that function, so output$contentFinal can't access it.
  2. The output$contentFinal renderer 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$filteredData to 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 saveData to assign the filtered data to values$filteredData instead of a local variable.
  • Made output$contentFinal reactive to values$filteredData: It checks if data exists, shows a helpful message if not, and renders the filtered table once saved.
  • Optional: Added values$rawData to persist edits in the Summary tab after saving (so your edits don't reset when you switch tabs).

内容的提问来源于stack exchange,提问作者Henk Straten

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:21:08