rHandsontable 下拉列表的问题不保留 Shiny 应用程序中的编辑?
Issue with rHandsontable dropdown not preserving edits in Shiny app?
我构建了一个 Shiny 应用程序,提示用户输入动态添加为 rHandsontable 的行。用户可以添加行、编辑它们,然后添加更多行。除了创建为 hot_col
s 且类型 = '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)
有两点需要调整:
- 列 "color" 应初始化为字符。
- 将
render*
函数包装在 observeEvent
中是不好的做法
这是您的代码的工作版本:
library(shiny)
library(rhandsontable)
library(dplyr)
ui <- fluidPage(
fluidRow(
textAreaInput('wordlines', "Enter words separated by newlines"),
actionButton('submit', "Submit Words"),
p(),
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_character_,
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({
req(values$df)
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 的行。用户可以添加行、编辑它们,然后添加更多行。除了创建为 hot_col
s 且类型 = '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)
有两点需要调整:
- 列 "color" 应初始化为字符。
- 将
render*
函数包装在observeEvent
中是不好的做法
这是您的代码的工作版本:
library(shiny)
library(rhandsontable)
library(dplyr)
ui <- fluidPage(
fluidRow(
textAreaInput('wordlines', "Enter words separated by newlines"),
actionButton('submit', "Submit Words"),
p(),
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_character_,
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({
req(values$df)
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)