我的目标是通过单击an按钮在模态中显示(有条件的)可编辑rhandsontable。创建和显示表工作得很好,但是我很难在ui中为表正确分配ID,以便识别用户更改。
感谢您的帮助,谢谢:)
library(shiny)
ui <- fluidPage(
actionButton('openModal', "Open Modal")
# rHandsontableOutput("DataEditor") # doesn't work
)
server <- function(input, output, session) {
observeEvent(input$openModal, {
irrelevant_condition <- FALSE # include that rhandsontable doesn't have to appear always
if (irrelevant_condition == TRUE) {
return(showModal(modalDialog("Choose some variables to display first")))
} else {
# display rhandsontable if user made a valid choice
showModal(modalDialog(
updDataEditor()
))
}
})
updDataEditor <- function() {
output$DataEditor <- renderRHandsontable({
# in real app some conditional calculations leading to a DF called 'current.DF'
# why function?: in real app with variation, depending on some inputs the user chose
current.DF <- data.frame(Name = c("Name1", "Name2"), value1 = c(0,0), value2=c(0,0)) # example df
rhandsontable(current.DF)
})
}
observeEvent(input$DataEditor, {
# Here's the problem
# won't get called when DataEditor is modified by the user
browser()
return()
})
}
shinyApp(ui,server)发布于 2022-04-05 11:36:12
它的工作方式如下:
library(shiny)
library(rhandsontable)
ui <- fluidPage(
actionButton('openModal', "Open Modal")
)
server <- function(input, output, session) {
current.DF <- reactive(
data.frame(Name = c("Name1", "Name2"), value1 = c(0,0), value2=c(0,0))
)
observeEvent(input$openModal, {
irrelevant_condition <- FALSE # include that rhandsontable doesn't have to appear always
if (irrelevant_condition == TRUE) {
return(showModal(modalDialog("Choose some variables to display first")))
} else {
# display rhandsontable if user made a valid choice
showModal(modalDialog(
rHandsontableOutput("DataEditor")
))
}
})
output$DataEditor <- renderRHandsontable({
rhandsontable(current.DF())
})
observeEvent(input$DataEditor, {
print(input$DataEditor$changes)
})
}
shinyApp(ui,server)是你想要的吗?对于可以更改的dataframe,我将使用一个反应性值。如果只更改了一些单元格,您可以更好地使用set_data函数。
编辑解决注释中提出的问题:
output$DataEditor <- renderRHandsontable({
rhandsontable(current.DF()) %>% htmlwidgets::onRender(
"function(el){var hot = this.hot; setTimeout(function(){hot.render();}, 1000)}"
)
})https://stackoverflow.com/questions/71749888
复制相似问题