我在一个闪亮的应用程序中有一个rhandsontable,它有两行。它使用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
})有什么想法吗?
发布于 2017-07-21 20:00:18
很抱歉我问得太快了。在花了2个小时将其发布在这里之后,我找到了一个简单的解决方案:将maxRows = 10添加到rhandsontable中,仅此而已。
rhandsontable(data.frame(rv_data),
fillHandle = list(direction='vertical', autoInsertRow=FALSE),
maxRows = 10) %>%
hot_context_menu(allowRowEdit = TRUE, allowColEdit = FALSE)发布于 2017-07-21 19:56:59
这是你想要的吗?它不使用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)希望这能有所帮助!
https://stackoverflow.com/questions/45236883
复制相似问题