在 R 中,如何将使用 rhandsontable 生成的表格和相关输入移动到模态对话框中?

问题描述

我对 rhandsontable 包和在 R Shiny 中模态对话的使用非常陌生,但是在模态对话框中渲染反应表对于我运行的模型类型非常重要。我需要学习如何做好这件事!

下面的 MWE 生成一个简单的反应式表,它的工作方式类似于我经常使用的表类型,例如。运行时,如何将当前显示在“显示”操作按钮下方的所有项目移动到模式对话框中?

下面的图片显示了我正在尝试做的事情。

MWE 代码

library(shiny)
library(rhandsontable)

ui <- shinyUI(fluidPage(
  h4("Click ´Show´ button below to trigger modal dialogue:"),actionButton("show","Show"),br(),actionButton(inputId = "reset_input",label = "Reset"),rHandsontableOutput("two_by_two"),tableOutput(outputId = "chg_data")
))

server <- shinyServer(function(input,output,session) {
  DF <- data.frame(A = c(1,2),B = c(3,4),row.names = c("C","D"))
  
  output$two_by_two <- renderRHandsontable({
    input$reset_input # trigger rendering on reset
    rhandsontable(DF)
  })
  
  output$chg_data = renderTable({
    hot_to_r(req({input$two_by_two}))*2},rownames = TRUE)
})

shinyApp(ui,server)

运行此 MWE 代码时会出现什么:

enter image description here

我想做什么:

enter image description here

解决方法

也许你正在寻找这个

library(shiny)
library(rhandsontable)

ui <- shinyUI(fluidPage(
  h4("Click ´Show´ button below to trigger modal dialogue:"),#tags$head(tags$style(" .modal-dialog{ width:800px}")),tags$head(tags$style(" .modal-body{ min-height:500px}")),actionButton("show","Show"),br(),br()
))

server <- shinyServer(function(input,output,session) {
  DF <- data.frame(A = c(1,2),B = c(3,4),row.names = c("C","D"))
  
  output$two_by_two <- renderRHandsontable({
    input$reset_input # trigger rendering on reset
    rhandsontable(DF)
  })
  
  output$chg_data = renderTable({
    hot_to_r(req({input$two_by_two}))*2},rownames = TRUE)
  
  observeEvent(input$show,{
    showModal(modalDialog(title = "my display",actionButton(inputId = "reset_input",label = "Reset"),rHandsontableOutput("two_by_two"),tableOutput(outputId = "chg_data"),easyClose = TRUE,footer = NULL,class = 'success'
    ))
  })
  
})

shinyApp(ui,server)

output