2

我在一个闪亮的应用程序中有一个 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

})

有任何想法吗?

4

2 回答 2

2

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

 rhandsontable(data.frame(rv_data),
                fillHandle = list(direction='vertical', autoInsertRow=FALSE),
                maxRows = 10) %>%
    hot_context_menu(allowRowEdit = TRUE, allowColEdit = FALSE)
于 2017-07-21T12:00:17.927 回答
1

这是做你想做的吗?它不使用 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)

希望这可以帮助!

于 2017-07-21T11:56:59.153 回答