问题描述
我对 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 代码时会出现什么:
我想做什么:
解决方法
也许你正在寻找这个
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)