首页 > 解决方案 > Shiny modal 中的handsontable 无法正确渲染

问题描述

问题

在我的代码中,我想rhandsontablemodal. 当我第一次显示模态时,表格很好地呈现。但是,当我关闭模式并重新打开它(不更改基础数据)时,我只能看到表格的一部分。只有在我点击表格后,它才能再次正确呈现。如果数据在重新打开模态之前发生变化,表格会再次很好地呈现。

问题

我怎样才能避免这种情况?我可以调用一个javascript例程来强制handsontable重新渲染作为解决方法吗?

代表

library(shiny)
library(rhandsontable)

ui <- fluidPage(actionButton("show", "show"), 
                actionButton("change", "Change"))

server <- function(input, output, session) {
  dat <- reactiveVal(data.frame(x = runif(2), 
                                y = runif(2)))

  observeEvent(input$show, {
    showModal(modalDialog(rHandsontableOutput("hot")))
  })

  observeEvent(input$change, dat(data.frame(x = runif(2), 
                                            y = runif(2))))

  output$hot <- renderRHandsontable(rhandsontable(dat()))
}

如何重现错误

  1. 打开模态并关闭它
  2. 重新打开模态,你会看到它也没有正确渲染
  3. 再次关闭并刷新数据
  4. 重新打开模态框,看到它再次正确渲染

截图

handsontable 正确渲染 Handsontable 未正确渲染

环境

R version 3.5.0 (2018-04-23)
Platform: i386-w64-mingw32/i386 (32-bit)
Running under: Windows 7 (build 7601) Service Pack 1

Matrix products: default

locale:
[1] LC_COLLATE=German_Germany.1252  LC_CTYPE=German_Germany.1252   
[3] LC_MONETARY=German_Germany.1252 LC_NUMERIC=C                   
[5] LC_TIME=German_Germany.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] shiny_1.1.0         rhandsontable_0.3.6

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.17    digest_0.6.15   later_0.7.2     mime_0.5       
 [5] R6_2.2.2        xtable_1.8-2    jsonlite_1.5    magrittr_1.5   
 [9] rlang_0.2.1     promises_1.0.1  tools_3.5.0     htmlwidgets_1.2
[13] httpuv_1.4.3    yaml_2.1.19     compiler_3.5.0  htmltools_0.3.6

浏览器测试:Chrome 版本 69.0.3497.100,IE 11

标签: rshinyhandsontablerhandsontable

解决方案


不是最优雅的解决方案,但这有效:

library(shiny)
library(rhandsontable)

ui <- fluidPage(actionButton("show", "show"), 
                actionButton("change", "Change"))

server <- function(input, output, session) {
  dat <- reactiveVal(data.frame(x = runif(2), 
                                y = runif(2)))

  dat1 <- reactive({
    if(is.null(input$hot)){
      dat()
    } else {
      as.data.frame(hot_to_r(input$hot))
    }
  })

  observeEvent(input$show, {
    showModal(modalDialog(rHandsontableOutput("hot")))
  })

  observeEvent(input$change, 
               dat(data.frame(x = runif(2), y = runif(2))))

  output$hot <- renderRHandsontable(rhandsontable(dat1()))

}

shinyApp(ui,server)


推荐阅读