【问题标题】:Allow conditional editing of cells in rHandsonTable in R shiny允许在 R Shiny 中对 rHandsonTable 中的单元格进行条件编辑
【发布时间】:2018-11-12 11:26:33
【问题描述】:
我在 R Shiny 中有一个 rhandsontable。该表有 5 列。我正在寻找一种功能,如果我编辑第 1 列,那么我不应该能够编辑第 2 列,反之亦然,即用户不应该能够同时编辑两列(第 1 列和第 2 列)。其他列应该不受影响。
谁能帮我解决这个问题?
【问题讨论】:
标签:
r
shiny
rhandsontable
【解决方案1】:
您可以通过相应地渲染 Handsontables 来实现您想要的。您可以使用 observeEvent 根据修改后的列检查handsontable 中的更改,并将所需的列设置为只读。为此,我建议使用反应值,这样,您可以获得已更改的列
只需 column_index = input$hot$changes$changes[[1]][[2]]。
希望这会有所帮助。
library(rhandsontable)
library(shiny)
ui <- fluidPage(
mainPanel(
rHandsontableOutput("hot")
)
)
server = function(input, output, session){
df <- data.frame(a = rnorm(10), b = rnorm(10))
values <- reactiveValues(data = df)
observeEvent(
input$hot$changes$changes, # observe any changes in the cells of the rhandsontable
{
column_index =input$hot$changes$changes[[1]][[2]] # get the index of the column that was changed
values$data <- hot_to_r(input$hot)
if(column_index == 1){
output$hot <- renderRHandsontable({
rhandsontable(values$data) %>%
hot_col('a', readOnly = T)
})
}
else{
output$hot <- renderRHandsontable({
rhandsontable(values$data) %>%
hot_col('b', readOnly = T)
})
}
}
)
output$hot <- renderRHandsontable({
rhandsontable(values$data)
})
}
shinyApp(ui, server)