rhandsontable 如何限制最大值。行数

rhandsontable how to restrict max. rows count

我在闪亮的应用程序中有一个 rhandsontable,它有 2 行。它使用 reactiveValues() 在其中加载值。

禁止通过拖动单元格来创建额外的行

fillHandle = list(direction='vertical', autoInsertRow=FALSE))

应该允许用户通过上下文菜单创建额外的行,但不超过 10 行。我想用 customOpts 来做,用户可以添加新行直到 nrow(table) == 10,但我javascript 非常糟糕。我试着用不同的方式来做(见下面的代码),但没能成功。另外,有没有办法换一种方式呢?

这是我到目前为止的代码片段:

output$table <- renderRHandsontable({
  rhandsontable(data.frame(rv_values),
                fillHandle = list(direction='vertical', autoInsertRow=FALSE)) %>%
    hot_context_menu(allowRowEdit = TRUE, allowColEdit = FALSE)
})

我试过像这样手动更改 allowRowEdit,但不太明白如何让它工作:

observeEvent(input$table, {
  if(nrow(hot_to_r(input$table)) > 10)
#magic happens here

})

有什么想法吗?

这是否符合您的要求?它不使用 Javascript,但它让用户添加行,直到达到最大值:

max_rows = 5

require(shiny)
library(DT)

ui<-shinyUI(
  fluidPage(
    actionButton("action","Add row"),
    rHandsontableOutput("table")

  )
)

server <- function(input, output, session) {

  rv_values <- reactiveVal()
  rv_values(head(mtcars,3))


  observeEvent(input$action,{
    if(!nrow(rv_values())==5)
    {
      rv_values(head(mtcars,nrow(rv_values())+1))    
    }
    else
    {
      showModal(modalDialog(
        title = "Important message",
        "Already reached maximum number of rows!"
      ))
    }
  }
  )

  output$table <- renderRHandsontable({
    rhandsontable(data.frame(rv_values()),
                  fillHandle = list(direction='vertical', autoInsertRow=FALSE)) %>%
      hot_context_menu(allowRowEdit = TRUE, allowColEdit = FALSE)
  })

}

shinyApp(ui,server)

希望对您有所帮助!

对不起,我问得太快了。花了 2 个小时并发布在这里之后,我找到了一个简单的解决方案:将 maxRows = 10 添加到 rhandsontable,就是这样。

 rhandsontable(data.frame(rv_data),
                fillHandle = list(direction='vertical', autoInsertRow=FALSE),
                maxRows = 10) %>%
    hot_context_menu(allowRowEdit = TRUE, allowColEdit = FALSE)