Shiny 中的条件默认值 rhandsontable

Conditional Default value rhandsontable in Shiny

问题: 在下面的 Shiny 应用程序中,我想通过 rhandsontable 更改并向以下数据插入新行:

data.frame(car = c("Opel", "Nissan", "Opel", "VW"),
                 location = c("Ruesselsheim", "Frankreich", "Ruesselsheim", "Wolfsburg"))
  1. 例如,如果我将 car == Opel 更改为 car == VW,位置将从 Ruesselsheim 更改为 Wolfsburg。
  2. 如果我插入一个新行,例如用 car == Opel 填充它,那么在提供汽车输入后,位置应直接位于 Ruesselsheim

问题:如何有条件地更改单元格值并定义条件默认值?

尝试次数: hot_col 有默认参数,但它只接受字符串而不接受条件。通过观察者跟踪变化似乎并不是最好的方法。

关于如何处理这个问题有什么想法吗?非常感谢!

library(shiny)
library(rhandsontable)

ui = shinyUI(fluidPage(
  
  titlePanel("Handsontable"),
  
  sidebarLayout(
    sidebarPanel(
      rHandsontableOutput("hot")
    ),
    mainPanel(
      verbatimTextOutput("debug")
    )
  )
))

server = function(input, output) {

  
  data <- reactive({
      data.frame(car = c("Opel", "Nissan", "Opel", "VW"),
                 location = c("Ruesselsheim", "Frankreich", "Ruesselsheim", "Wolfsburg"))
    })
  
  output$hot <- renderRHandsontable({
    DF <- data()
    rhandsontable(DF, useTypes = FALSE, selectCallback = TRUE)
  })
  
  ### DEBUG
  output$debug <- renderPrint({
    req(input$hot)
  
    input$hot$changes
  })  
}

shinyApp(ui = ui, server = server)

可以通过使用 reactiveVal 和 lookup-table 将当前选择与以下内容合并来实现所需的行为:

library(shiny)
library(rhandsontable)

ui = shinyUI(fluidPage(
  
  titlePanel("Handsontable"),
  
  sidebarLayout(
    sidebarPanel(
      rHandsontableOutput("hot")
    ),
    mainPanel(
      verbatimTextOutput("debug")
    )
  )
))

server = function(input, output) {
  
  LUT_DF <- data.frame(car = c("Opel", "Nissan", "VW"),
                   location = c("Ruesselsheim", "Frankreich", "Wolfsburg"))
  
  data <- reactiveVal(data.frame(car = c("Opel", "Nissan", "Opel", "VW"),
                                 location = c("Ruesselsheim", "Frankreich", "Ruesselsheim", "Wolfsburg")))
  
  output$hot <- renderRHandsontable({
    rhandsontable(data(), useTypes = FALSE, selectCallback = TRUE)
  })
  
  observeEvent(input$hot, {
    data(merge(LUT_DF, hot_to_r(input$hot)[1], by = "car"))
    }, ignoreInit = TRUE)

}

shinyApp(ui = ui, server = server)