【问题标题】:Issue with rHandsontable dropdown not preserving edits in Shiny app?rHandsontable 下拉列表不保留 Shiny 应用程序中的编辑的问题?
【发布时间】: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


    【解决方案1】:

    有两点需要调整:

    1. “颜色”列应初始化为字符。
    2. 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-29
      • 1970-01-01
      • 2014-10-04
      • 1970-01-01
      • 2016-09-23
      • 2017-10-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多