R Shiny,如何让DT包含复选框和radioButtons

时间:2018-02-14 00:51:27

标签: r checkbox shiny radio-button dt

我正在寻找一种方法来选择一组给定的项目(比如一年中的几个月)一部分项目(比如说我喜欢的几个月)以及(同时)一个项目(可能是我的生日月)。根据选择,需要打印更多信息(这可能是本月对最喜欢的月份所占的天数百分比)。包含

列的数据表
  • 所有可能的项目(月份名称)
  • 复选框
  • 单选按钮
  • 额外输出

将是这个想法的技术实现。

我已分别为checkboxes (entry of 2015-06-11)radiobuttons (answer of 2016-08-12)找到了工作示例,但我无法将这些结合起来。在我看来,潜在的机制是不同的;但也许我只是缺乏JS知识。以下代码尝试将radioButtons包含在复选框示例中;复选框仍在工作,radioButtons似乎正在工作,但我无法访问实际选择: - (

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

server = function(input, output) {
  # 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)) NA else value
    }))
  }

  # a sample data frame
  res = data.frame(
    Month = month.abb,
    Like = shinyInput(checkboxInput, 12, 'v2_', value = TRUE),
    # v3 is constructed as in the radioButton matrix example
    Birthday = sprintf('<input type="radio" name="%s" value="%s"/>', "rButton", 1:12),
    Info = sample(LETTERS, 12, TRUE),
    stringsAsFactors = FALSE
  )

  # render the table containing shiny inputs
  output$x1 = DT::renderDataTable(
    res, server = FALSE, escape = FALSE, selection = 'none', options = list(
      dom = 't', paging = FALSE, ordering = FALSE,
      # is the preDrawCallback argument needed? Seems to have no effect at all.
      preDrawCallback = JS('function() { Shiny.unbindAll(this.api().table().node()); }'),
      drawCallback = JS('function() { Shiny.bindAll(this.api().table().node()); } ')#,
      # the following is from the radioButton matrix example, but doesn't work here
      #       callback = JS("table.rows().every(function(i, tab, row) {
      #                   var $this = $(this.node());
      #                   $this.attr('id', this.data()[0]);
      #                   $this.addClass('shiny-input-radiogroup');
      #                   });
      #                   Shiny.unbindAll(table.table().node());
      #                   Shiny.bindAll(table.table().node());")
    )
  )
  # print the values of inputs
  output$x2 = renderPrint({
    data.frame(Like = shinyValue('v2_', 12))
  })
  output$sel = renderPrint({
    # this is from the radioButton matrix example but doesn't work here
    str(sapply("rButton", function(i) input[[i]]))
  })
})

另一方面,我不确定这是否会在实现时形成一个直观,有用的界面。我也对有关用户界面结构的建议感兴趣。

0 个答案:

没有答案