【问题标题】:Use data.table for shiny inputs and store user inputs in a data.frame使用 data.table 进行闪亮的输入并将用户输入存储在 data.frame 中
【发布时间】:2017-12-16 04:55:20
【问题描述】:

在我的闪亮应用程序中,我希望使用 data.table 通过单选按钮或复选框获取用户输入,并将用户输入存储在 data.frame 中。

这是我目前所取得的成就:

library(shiny)
library(data.table)
library(DT)
shinyApp(
  ui = fluidPage(
    title = 'Radio buttons in a table',
    DT::dataTableOutput('foo'),
    verbatimTextOutput('sel')
  ),
  server = function(input, output, session) {

    x <- data.table( 'Breed Split' = paste0("F",rep(0:16)), Frisian = rep(1,17), Jersey = rep(2,17), Cross = rep(3,17) )

    x[, Frisian := sprintf( '<input type="radio" name="%s" value="%s"/>', `Breed Split`, x[, Frisian] )]
    x[, Jersey := sprintf( '<input type="radio" name="%s" value="%s"/>', `Breed Split`, x[, Jersey] )]
    x[, Cross := sprintf( '<input type="radio" name="%s" value="%s"/>', `Breed Split`, x[, Cross] )]

    output$foo = DT::renderDataTable(
      x, escape = FALSE, selection = 'none', server = FALSE, rownames=FALSE,
      options = list(dom = 't', paging = FALSE, ordering = FALSE),
      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());")
    )
    output$sel = renderPrint({
      str(sapply(x$`Breed Split`, function(i) input[[i]]))
    })
  }

)

另一件事是如果有任何方法可以设置默认输入值,如此屏幕截图所示。

[]

【问题讨论】:

    标签: r data.table shiny dt


    【解决方案1】:

    尝试添加检查名称的列,然后在呈现 DT 时删除列

       library(shiny)
    library(data.table)
    library(DT)
    shinyApp(
      ui = fluidPage(
        title = 'Radio buttons in a table',
        DT::dataTableOutput('foo'),
        verbatimTextOutput('sel')
      ),
      server = function(input, output, session) {
    
        x <- data.table( 'Breed Split' = paste0("F",rep(0:16)), Frisian = rep(1,17), Jersey = rep(2,17), Cross = rep(3,17) ,
                         checked=c(rep("Frisian",9),rep("Jersey",5),rep("Cross",3))
                         )
    
        x[, Frisian := sprintf( '<input type="radio" name="%s" value="%s" %s/>', `Breed Split`, x[, Frisian],ifelse("Frisian"==x[, checked],"checked" ,""))]
        x[, Jersey := sprintf( '<input type="radio" name="%s" value="%s" %s/>', `Breed Split`, x[, Jersey],ifelse("Jersey"==x[, checked],"checked" ,"" ))]
        x[, Cross := sprintf( '<input type="radio" name="%s" value="%s" %s/>', `Breed Split`, x[, Cross] ,ifelse("Cross"==x[, checked],"checked" ,""))]
    
        output$foo = DT::renderDataTable(
          x[,-c("checked")], escape = FALSE, selection = 'none', server = FALSE, rownames=FALSE,
          options = list(dom = 't', paging = FALSE, ordering = FALSE),
          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());")
        )
        output$sel = renderPrint({
          str(sapply(x$`Breed Split`, function(i) input[[i]]))
        })
        }
    
    )
    

    【讨论】:

    • 感谢您的回答,非常感谢您的宝贵时间。最后,我希望将用户输入保存在向量中。例如inputs &lt;- output$sel
    猜你喜欢
    • 2016-03-26
    • 2018-06-17
    • 2017-08-22
    • 2017-03-02
    • 1970-01-01
    • 1970-01-01
    • 2018-07-21
    • 2021-04-13
    • 2021-10-31
    相关资源
    最近更新 更多