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

如何通过单选按钮启用/禁用Shiny中的rhandsontable?

修复Shiny中rhandsontable根据单选按钮切换只读/可编辑状态的问题

问题根源

  1. 原代码的renderRHandsontable函数中,先后两次调用rhandsontable(),第一次设置的readOnly参数被第二次调用完全覆盖,导致表格始终处于默认的可编辑状态。
  2. 使用shinyjs::disable/enable无法有效控制rhandsontable的编辑权限,因为该组件的编辑状态由自身的readOnly参数直接管理。

修复方案

将readOnly参数整合到链式调用的初始rhandsontable()中,移除无效的shinyjs控制逻辑,确保单选按钮的选择能正确触发表格状态切换。

修改后的完整代码

library(rhandsontable)
library(shiny)
library(shinydashboard)

opts.adv <- c("Default","Custom")

ui <- dashboardPage(skin = "blue",
                    dashboardHeader(title = "Leslie Matrix Model",titleWidth = 450),
                    dashboardSidebar(id="",
                                     width = 450,
                                     sidebarMenu(id = "tabs",
                                                 menuItem("Welcome",tabName = "menuWelcome", icon = shiny::icon("face-smile")),
                                                 menuItem("Advanced",tabName = "menuAdvanced", icon = shiny::icon("star")),
                                                 actionButton("btnQuit","Quit",icon = shiny::icon("xmark"),class="btn-lg btn-danger"))),
                    dashboardBody(tabItems(
                        tabItem(tabName = "menuWelcome",
                                valueBox("Welcome","Population viability analysis",icon = shiny::icon("face-smile"),width=9)),

                        tabItem(tabName = "menuAdvanced",
                                fluidRow(box(title="Age-1+ Survival",status="primary",solidHeader=TRUE,width=4,
                                             radioButtons("adv.adultS.sel","Age-1+ survival options",opts.adv))),
                                fluidRow(box(title="Custom Age-1+ Survival",status="primary",solidHeader=TRUE,width=9,rHandsontableOutput("hot")))))))


server <- function(input, output, session) {

  # Create a reactive value to hold the data
  customS <- reactiveValues(data = NULL)
  
  # Initialize the table with sequential column names and no initial data
  observe({
    if (is.null(customS$data)) {
      agenum <- sprintf("age%d",seq(1:15))
      
      # Initialize an empty data frame with sequential column names
      customS$data <- setNames(data.frame(matrix(ncol = 15, nrow = 1)), agenum)
    }
  })
  
  # Render the rhandsontable with dynamic readOnly status
  output$hot <- renderRHandsontable({
    rhandsontable(customS$data, readOnly = (input$adv.adultS.sel == "Default")) %>%
      hot_col(col = seq_len(ncol(customS$data)), type = "numeric")  # Set all columns to numeric type
  })
  
  # Quit app
  observeEvent(input$btnQuit, {
    stopApp()
  })

}

shinyApp(ui, server)

关键修改说明

  • 将readOnly = (input$adv.adultS.sel == "Default")直接添加到生成rhandsontable的初始调用中,确保单选按钮的选择能实时传递给表格的编辑状态参数。
  • 移除了原代码中无效的shinyjs相关逻辑,避免冗余代码干扰组件状态控制。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 17:03:17