问题描述
在下面的 MWE 代码中,操作按钮“显示”触发一个模式对话框,其中包含一个用户可以使用 rhandsontable 操作的表格。按预期工作,除了“重置”按钮不起作用。重置应该将所有用户输入反转到表中,并带回一个 2 列 5 行的随机变量表。
如何重置表格?我今天早上一直在玩这个,但还没有运气。
顺便说一句,这需要留在modalDialog中!模态对话框中的 Rhandsontable 看起来很棘手,但对于这个有很多输入的模型来说非常有用。
请参阅底部的图片。第一张图片显示了首次调用应用程序并单击“显示”操作按钮时会发生什么。好 - 出现 5 行 2 列的表格。第二个图像是用户右键单击表格以在底部插入额外的行后显示的内容,在第 6 行的 x 和 y 列中输入 6。如果您单击“关闭”模态并再次单击“显示”,那么好 - 会按预期显示相同的修改表。反复单击“关闭/显示”会不断带回相同的修改后的 6 行表——很好!但是,如果您点击“重置”,添加了第 6 行的修改后的表格会不断返回。应该恢复到 5 行表。如果用户手动将多行输入到表中,而不是像本例中那样只添加了一行,reset 应该会带回一个 5 行的表。
library(shiny)
library(rhandsontable)
ui <- fluidPage(actionButton("show","Show"),actionButton("reset","Reset"))
server <- function(input,output,session){
dat <- reactiveVal(data.frame(x=runif(5),y=runif(5)))
dat1 <- reactive({
if(is.null(input$hot)){dat()}
else {as.data.frame(hot_to_r(input$hot))}
}) # close reactive
observeEvent(input$show,{showModal(modalDialog(rHandsontableOutput("hot")))})
observeEvent(input$reset,{dat(data.frame(x=runif(5),y=runif(5)))})
output$hot <- renderRHandsontable(rhandsontable(dat1()))
} # close Server
shinyApp(ui,server)
下面是一张图片,显示了对它进行更改并点击“显示”按钮后被遮挡的表格(应该显示整个表格):
解决方法
您的条件 if(is.null(input$hot)){dat()}
阻止了重置。
请检查以下内容:
library(shiny)
library(rhandsontable)
ui <- fluidPage(actionButton("show","Show"),actionButton("reset","Reset"))
server <- function(input,output,session) {
dat <- reactiveVal(data.frame(x = runif(5),y = runif(5)))
observeEvent(input$hot,{
dat(as.data.frame(hot_to_r(input$hot)))
})
observeEvent(input$show,{
showModal(modalDialog(rHandsontableOutput("hot")))
})
observeEvent(input$reset,{
dat(data.frame(x = runif(5),y = runif(5)))
})
output$hot <- renderRHandsontable({
# input$show
rhandsontable(dat())
})
} # close Server
shinyApp(ui,server)