【发布时间】:2020-01-14 18:35:09
【问题描述】:
我已经构建了一个 Shiny 应用程序,它提示用户输入作为行动态添加到 rHandsontable 的输入。用户可以添加行、编辑行,然后添加更多行。该应用程序正在正常工作,但创建为 hot_cols 且 type = 'dropdown' 的列除外。用户对这些列所做的任何编辑都不会保留到下次添加行时,但所有其他编辑都会保留。
下面是我的应用程序。保留对 word 列的编辑,但不会保留对由 hot_col() 创建的 color 列的编辑。
library(shiny)
library(rhandsontable)
library(dplyr)
ui <- fluidPage(
fluidRow(
textAreaInput('wordlines', "Enter words separated by newlines"),
actionButton('submit', "Submit Words"),
rHandsontableOutput('hot')
)
)
server <- function(input, output, session) {
colors <- c("red", "orange", "yellow", "green", "blue", "indigo", "violet")
build_df <- function(input) {
data.frame(
word = unlist(strsplit(input$wordlines, "\n")),
number = sample(100, 1),
color = NA,
stringsAsFactors = FALSE
)
}
values <- reactiveValues()
observeEvent(input$submit, {
req(input$wordlines)
if(input$submit == 1) { # if button pressed the first time
values$df <- build_df(input)
} else {
tmp <- hot_to_r(input$hot)
values$df <- bind_rows(tmp, build_df(input)) # add new rows to values$DF
}
updateTextAreaInput(session, 'wordlines', value = "") # clear textAreaInput
output$hot <- renderRHandsontable({
rhandsontable(values$df, overflow = 'visible') %>% hot_col(col = "color", type = "dropdown", source = colors) # dropdown column which, when edited, should preserve those edits through future adding of rows ('submit' button presses)
})
})
}
# Run the application
shinyApp(ui = ui, server = server)
【问题讨论】:
标签: shiny rhandsontable