R Shiny,如何使数据表对数据表中的复选框使用react

标签 r checkbox shiny dt

我希望我的数据表显示的内容取决于表中包含的复选框的状态。我在这两个方面都找到了帮助,包括 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))
    })
  }
)

最佳答案

是的,您的示例代码几乎可以工作。唯一不正确的是 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))
    })
  }
)

关于R Shiny,如何使数据表对数据表中的复选框使用react,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48854547/

相关文章:

r - 创建连续值的组名

r - 只显示正在使用的标签?

android - 在android中生成ID

r - 分析 Shiny 的服务器日志以创建使用情况统计信息

R Shiny showModal 和removeModal 适用于所有用户

r - 在 ggarrange() 中设置高图表格的宽度 - 可能的错误? (R,ggplot,鸡蛋)

r - 如何在 igraph 中提取社区的边缘列表?

javascript - jQuery 如果选中一个复选框,则根据值禁用其他特定复选框

php - 多选 MySQL 数据中的复选框,用逗号 (,) 分隔

r - 将数据表放入shinyjs模式中