【发布时间】:2020-04-05 05:41:57
【问题描述】:
我正在尝试制作一个应用程序,用户可以在其中将列和行添加到表中,他们可以使用这些表来设置规则以对存储在另一个对象中的数据集进行排序。
我建立了一个代表来隔离问题。 UI 在单击添加按钮时成功地将行和列添加到表中。额外的行工作正常。但是,不能更改新列中的单元格。您可以输入文本,但不能切换单元格。我仍在学习 Shiny 的反应规则,并怀疑问题存在。我尝试采用
dynamically add rows to rhandsontable in shiny and R
但无济于事。
library(shiny)
library(rhandsontable)
library(tidyverse)
ui <- fluidPage(
actionButton("addRow",
"Add"),
numericInput("rows",
"Number of Rows",
value = 0,
min = 0),
numericInput("colAnd",
"Number of And Cols",
value = 0,
min = 0),
numericInput("colBut",
"Number of But Cols",
value = 0,
min = 0),
rHandsontableOutput("hot")
)
server <- function(input, output) {
df <- reactive({
tibble(Topic = "Null",
And = "Null",
But = "Null")
})
df2 <- eventReactive(input$addRow,{
add_row_custom<- function (df, n, add = "null")
{
new <- data.frame(matrix(add, nrow = n, ncol = ncol(df)))
colnames(new) <- colnames(df)
rbind(df, new)
}
final <- hot_to_r(input$hot)
final %>%
add_row_custom(., input$rows ) %>%
cbind(., data.frame(matrix("null",
nrow = nrow(.),
ncol = input$colAnd + input$colBut)
)
)
})
df4 <- reactive({
if(input$addRow == 0){
df()
}else{
df2()}
})
output$hot <- renderRHandsontable(rhandsontable(df4()) %>%
hot_table(highlightRow = T,
highlightCol = T))
}
# Run the application
shinyApp(ui = ui, server = server)
【问题讨论】:
标签: r shiny shiny-reactivity rhandsontable