【问题标题】:R Shiny, how to make datatable react to checkboxes in datatableR Shiny,如何使数据表对数据表中的复选框做出反应
【发布时间】:2018-07-29 00:03:18
【问题描述】:

我希望我的数据表显示取决于表中复选框状态的内容。我已经找到了两者的帮助,包括 DT 中的复选框以及更改数据表内容,但是当我尝试结合这些解决方案时,我没有得到我想要的。选中一个框时,表格会重绘两次,第一次是我想要的方式,但稍后它会切换回来。

这是应该几乎做的代码......在我发疯之前有人可以帮忙吗?

library(shiny)
library(DT)
shinyApp(
  ui = fluidPage(
    DT::dataTableOutput('x1'),
    verbatimTextOutput('x2')
  ),

  server = function(input, output, session) {
    # create a character vector of shiny inputs
    shinyInput = function(FUN, len, id, ...) {
      inputs = character(len)
      for (i in seq_len(len)) {
        inputs[i] = as.character(FUN(paste0(id, i), label = NULL, ...))
      }
      inputs
    }

    # obtain the values of inputs
    shinyValue = function(id, len) {
      unlist(lapply(seq_len(len), function(i) {
        value = input[[paste0(id, i)]]
        if (is.null(value)) TRUE else value
      }))
    }

    n = 6
    df = data.frame(
      cb = shinyInput(checkboxInput, n, 'cb_', value = TRUE, width='1px'),
      month = month.abb[1:n],
      YN = rep(TRUE, n),
      ID = seq_len(n),
      stringsAsFactors = FALSE)

    loopData = reactive({
      df$YN <<- shinyValue('cb_', n)
      df
    })

    output$x1 = DT::renderDataTable(
      isolate(loopData()),
      escape = FALSE, selection = 'none',
      options = list(
        dom = 't', paging = FALSE, ordering = FALSE,
        preDrawCallback = JS('function() {    Shiny.unbindAll(this.api().table().node()); }'),
        drawCallback = JS('function() { Shiny.bindAll(this.api().table().node()); } ')#,
      ))

    proxy = dataTableProxy('x1')

    observe({
      replaceData(proxy, loopData())
    })

    output$x2 = renderPrint({
      data.frame(Like = shinyValue('cb_', n))
    })
  }
)

【问题讨论】:

    标签: r checkbox shiny dt


    【解决方案1】:

    是的,您的示例代码几乎有效。唯一不对的是df$cb 的值也需要更改。

    例如,假设您点击了第二行,input$cb_2 发生了变化。 shiny 会记录 input$cb_2 已更改为 FALSE。由于df$cb[[2]] 的值仍然是checkbox(..., value = TRUE),当重新绘制表格时,会显示一个选中的复选框,R 认为input$cb_2 再次更改,因此您的数据将相应更改。

    如果有什么不清楚的,请检查示例代码。

    工作示例代码

    library(shiny)
    library(DT)
    shinyApp(
      ui = fluidPage(
        DT::dataTableOutput('x1'),
        verbatimTextOutput('x2')
      ),
    
      server = function(input, output, session) {
        # create a character vector of shiny inputs
        shinyInput = function(FUN, len, id, value, ...) {
          if (length(value) == 1) value <- rep(value, len)
          inputs = character(len)
          for (i in seq_len(len)) {
            inputs[i] = as.character(FUN(paste0(id, i), label = NULL, value = value[i]))
          }
          inputs
        }
    
        # obtain the values of inputs
        shinyValue = function(id, len) {
          unlist(lapply(seq_len(len), function(i) {
            value = input[[paste0(id, i)]]
            if (is.null(value)) TRUE else value
          }))
        }
    
        n = 6
        df = data.frame(
          cb = shinyInput(checkboxInput, n, 'cb_', value = TRUE, width='1px'),
          month = month.abb[1:n],
          YN = rep(TRUE, n),
          ID = seq_len(n),
          stringsAsFactors = FALSE)
    
        loopData = reactive({
          df$cb <<- shinyInput(checkboxInput, n, 'cb_', value = shinyValue('cb_', n), width='1px')
          df$YN <<- shinyValue('cb_', n)
          df
        })
    
        output$x1 = DT::renderDataTable(
          isolate(loopData()),
          escape = FALSE, selection = 'none',
          options = list(
            dom = 't', paging = FALSE, ordering = FALSE,
            preDrawCallback = JS('function() { Shiny.unbindAll(this.api().table().node()); }'),
            drawCallback = JS('function() { Shiny.bindAll(this.api().table().node()); } ')
          ))
    
        proxy = dataTableProxy('x1')
    
        observe({
          replaceData(proxy, loopData(), resetPaging = FALSE)
        })
    
        output$x2 = renderPrint({
          data.frame(Like = shinyValue('cb_', n))
        })
      }
    )
    

    【讨论】:

      猜你喜欢
      • 2019-06-04
      • 2021-03-02
      • 2022-01-23
      • 2015-06-29
      • 2016-10-02
      • 1970-01-01
      • 1970-01-01
      • 2019-05-31
      • 2019-12-17
      相关资源
      最近更新 更多