【问题标题】:R Shiny dq_render_handsontable Error when adding columns and trying to Edit new columns' cellsR Shiny dq_render_handsontable 添加列并尝试编辑新列的单元格时出错
【发布时间】:2020-02-28 13:02:33
【问题描述】:

我在使用闪亮的dq_render_handsontable 时遇到错误,我猜这是 dq_shiny 包的错误。如果有人能知道任何解决方法,我将不胜感激。 我正在尝试通过操作按钮(下面的代码中的“生成”)以交互方式更新表格。我试图在其中切换的表具有不同数量的列。所有工作都可以显示新表,即,一旦我单击“生成”,我可以看到带有附加列的新表。但问题是,一旦我在编辑第一个列较少的数据框后尝试用更多列编辑数据框的单元格,就会出现以下错误:

Warning: Error in [<-.data.frame: new columns would leave holes after existing columns

我猜这肯定是dq_render_handsontable 的一个错误,它无法识别添加到handsontable 的新列。有人知道解决方法吗?也许在显示具有更多列的新数据框之前刷新表格?

我附上代码来重现错误:

library(shiny)
library(rhandsontable)
library(dqshiny)
library(rlang)

  ui = fluidPage(


    dq_handsontable_output("InputTable", 9)
    ,
    # actionButton("render", "Render HoT"),
    actionButton("simulationInput_2", "Generate"),
    fluidRow(id="bigRow", class="hidden",
             style="height:100vh;background:#ff8f00;")
  )

  server = function(input, output) {
    hw <- c("Hello", "my", "funny", "world!")
    data1 <- data.frame(A=hw, B=hw[c(2,3,4,1)], C=1:4, D=Sys.Date() - 0:3,
                        A2=hw, B2=hw[c(2,3,4,1)], C2=1:4, D2=Sys.Date() - 1:4,
                       stringsAsFactors = FALSE)
    hw <- c("Hello", "my", "funny", "world!")
    data2 <- data.frame(A=hw, B=hw[c(2,3,4,1)], C=1:4, D=Sys.Date() - 0:3,
                        # A2=NA, B2=NA, C2=NA, D2=NA,
                        stringsAsFactors = FALSE)
    cont = 0


    observeEvent(input$simulationInput_2, {
      cont <<- cont+1
      print(cont)
      if(mod(cont,2)==0){
        data <- data2
      }else{
        data <- data1
      }
      renderInputTable(data)
      render_hot("InputTable")
      })

    renderInputTable <- function(data){
      dq_render_handsontable(
      "InputTable",
                           data, #"rand",
                           # filters = F, #c("S", "T", "R", "R"),
                           filters = rep(NA, ncol(data)),
                           table_param = list(rowHeaders = NULL, selectCallback = TRUE))

    }

    observeEvent(input$random_select, toggle("bigRow"))
    observeEvent(input$render, render_hot("InputTable"))
  }

shinyApp(ui, server)

【问题讨论】:

    标签: shiny shinydashboard shiny-server shiny-reactivity shinyapps


    【解决方案1】:

    我可以通过重命名 dq_shiny 表 ID 的技巧来解决这个问题,这实际上是 dq_render_handsontable 的错误:

    library(shiny)
    library(rhandsontable)
    library(dqshiny)
    library(rlang)
    library(magrittr)
    library(data.table)
    
    
    ui = fluidPage(
      tags$div(id="simulationInputTable_divOutside", style="padding:0px;margin:0px",
               tags$div(id="simulationInputTable_divInside1", style="padding:0px;margin:0px",
                        dq_handsontable_output("InputTable1", 9)),
               tags$div(id="simulationInputTable_divInside2", style="padding:0px;margin:0px",
                        dq_handsontable_output("InputTable2", 9)),
               tags$div(id="simulationInputTable_divInside3", style="padding:0px;margin:0px",
                        dq_handsontable_output("InputTable3", 9))
      )
      ,
      # actionButton("render", "Render HoT"),
      actionButton("simulationInput_2", "Generate"),
      fluidRow(id="bigRow", class="hidden",
               style="height:100vh;background:#ff8f00;")
    )
    server = function(input, output) {
      columns <- c(1,2,3,4)
      hw <- c("Hello", "my", "funny", "world!")
    
      cont = 0
      observeEvent(input$simulationInput_2, {
        cont <<- cont+1
        data1 <- data.frame(A=hw, B=hw[c(2,3,4,1)], C=1:4, D=Sys.Date() - 0:3,
                            A2=hw, B2=hw[c(2,3,4,1)], C2=1:4, D2=Sys.Date() - 1:4,
                            stringsAsFactors = FALSE)
        name = paste0("InputTable",cont)
        divName = paste0("simulationInputTable_divInside",cont-1)
        hide(divName)
        dq_render_handsontable(
          name,
          data1, #"rand",
          # filters = F, #c("S", "T", "R", "R"),
          filters = rep(NA, ncol(data1)),
          table_param = list(rowHeaders = NULL, selectCallback = TRUE))
      })
      observeEvent(input$random_select, toggle("bigRow"))
      observeEvent(input$render, render_hot("InputTable"))
    }
    shinyApp(ui, server)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-23
      • 1970-01-01
      相关资源
      最近更新 更多