【问题标题】:DT: Dynamically change column values based on selectinput from another column in R shiny appDT:根据 R 闪亮应用程序中另一列的 selectinput 动态更改列值
【发布时间】:2021-11-19 12:09:35
【问题描述】:

我正在尝试创建一个表(使用 DT,请不要使用 rhandsontable),其中包含很少的现有列、一个 selectinput 列(每行都有可供选择的选项),最后是另一列将根据用户从每行的 selectinput 下拉列表中选择的内容。

在我的示例中,“反馈”列是用户下拉选择列。 我无法更新将基于从“反馈”列下拉列表中选择的“分数”列。

if(interactive()){
  library(DT)
  library(shiny)
  tbl1 <- data.frame(A = c(1:10), B = LETTERS[1:10], C = c(11:20), D = LETTERS[1:10])
  ui <- fluidPage(
    DT::dataTableOutput(outputId = 'my_table')
  )
  server <- function(input, output, session) {
    rv <- reactiveValues(tbl = tbl1)
    observe({
      for (i in 1:nrow(rv$tbl)) {
        rv$tbl$Feedback[i] <- as.character(selectInput(paste0("sel", i), "",
                                                       choices = c(1,2,3,4)
        ))
        
        if(!is.null(input[[paste0("sel", i)]])) {
          if(input[[paste0("sel", i)]] == 1) {
            rv$tbl$Score[i] <- 10
          } else if(input[[paste0("sel", i)]] == 2) {
            rv$tbl$Score[i] <- 20
          } else if(input[[paste0("sel", i)]] == 3) {
            rv$tbl$Score[i] <- 25
          } else if(input[[paste0("sel", i)]] == 4) {
            rv$tbl$Score[i] <- 30
          }
        }
      }
    })
          
          output$my_table = DT::renderDataTable({
            
            datatable(
              rv$tbl, escape = FALSE, selection = 'none', rownames = F,
              options = list( paging = FALSE, ordering = FALSE, scrollx = T, dom = "t",
                              preDrawCallback = JS('function() { Shiny.unbindAll(this.api().table().node()); }'),
                              drawCallback = JS('function() { Shiny.bindAll(this.api().table().node()); } ')
              )
            )
          }, server = FALSE)
          
          
  }
  
  shinyApp(ui = ui, server = server)
}


【问题讨论】:

    标签: r shiny datatables reactive-programming dt


    【解决方案1】:

    我建议使用dataTableProxyreplaceData 来实现所需的行为。这比重新渲染datatable 更快。

    此外,重新渲染表格似乎与 selectInputs 的绑定有关。

    另外请注意:为此我需要切换到server = TRUE

    library(DT)
    library(shiny)
    
    selectInputIDs <- paste0("sel", 1:10)
    
    initTbl <- data.frame(
      A = c(1:10),
      B = LETTERS[1:10],
      C = c(11:20),
      D = LETTERS[1:10],
      Feedback = sapply(selectInputIDs, function(x){as.character(selectInput(inputId = x, label = "", choices = c(1, 2, 3, 4), selected = 1))}),
      Score = rep(10, 10)
    )
    
    ui <- fluidPage(
      DT::dataTableOutput(outputId = 'my_table')
    )
    
    server <- function(input, output, session) {
      
      displayTbl <- reactive({
          data.frame(
            A = c(1:10),
            B = LETTERS[1:10],
            C = c(11:20),
            D = LETTERS[1:10],
            Feedback = sapply(selectInputIDs, function(x){as.character(selectInput(inputId = x, label = "", choices = c(1, 2, 3, 4), selected = input[[x]]))}),
            Score = sapply(selectInputIDs, function(x){as.integer(input[[x]])*10})
          )
      })
      
      output$my_table = DT::renderDataTable({
        DT::datatable(
          initTbl, escape = FALSE, selection = 'none', rownames = FALSE,
          options = list(paging = FALSE, ordering = FALSE, scrollx = TRUE, dom = "t",
                         preDrawCallback = JS('function() { Shiny.unbindAll(this.api().table().node()); }'),
                         drawCallback = JS('function() { Shiny.bindAll(this.api().table().node()); } ')
          )
        )
      }, server = TRUE)
      
      my_table_proxy <- dataTableProxy(outputId = "my_table", session = session)
      
      observeEvent({sapply(selectInputIDs, function(x){input[[x]]})}, {
        replaceData(proxy = my_table_proxy, data = displayTbl(), rownames = FALSE) # must repeat rownames = FALSE see ?replaceData and ?dataTableAjax
      }, ignoreInit = TRUE)
      
    }
    
    shinyApp(ui = ui, server = server)
    

    【讨论】:

    猜你喜欢
    • 2019-01-30
    • 2022-06-19
    • 2017-10-26
    • 2019-07-15
    • 2016-01-02
    • 2017-03-21
    • 2020-03-20
    • 2020-09-30
    • 2018-11-01
    相关资源
    最近更新 更多