【问题标题】:Shiny - true or false value when check a checkbox in datatable闪亮 - 检查数据表中的复选框时的真或假值
【发布时间】:2016-10-19 13:25:38
【问题描述】:

我对 R 闪亮代码有疑问,当我想查看复选框中的真值或假值时,我必须做什么?因为当我尝试 print(input$row1) 结果为 NULL 时,当我在复选框中进行检查时,打印语句中没有响应。这是代码:

library(shiny)
library(DT)
mymtcars = mtcars
mymtcars$id = 1:nrow(mtcars)
runApp(
  list(ui = pageWithSidebar(
    headerPanel('Examples of DataTables'),
    sidebarPanel(

    ),
    mainPanel(
      uiOutput("mytable")
    )
  )
  , server = function(input, output, session) {

    output$mytable = renderUI({
      addCheckboxButtons <- paste0('<input type="checkbox" id="row', mymtcars$id, '">',"")
      yyy<- cbind(mymtcars[, names(mymtcars[,2:4]), drop=FALSE],Pick=addCheckboxButtons)

      #try
      print(input$row1) #didnt work
      print(input$row2) #didnt work

      #Display table with checkbox buttons
      list(
      renderDataTable(yyy,
                    options = list(orderClasses = TRUE,
                                   lengthMenu = c(5, 25, 50),
                                   pageLength = 25, 
                                   callback = JS("function(table) {
                                                 table.on('change.dt', 'tr td input:checkbox', function() {
                                                 setTimeout(function () {
                                                 Shiny.onInputChange('rows', $(this).add('tr td input:checkbox:checked').parent().siblings(':last-child').map(function() {
                                                 return $(this).text();
                                                 }).get())
                                                 }, 10); 
                                                 });
    }")),escape = FALSE

                    ))
      } 
  )
    }
      )
                    )

【问题讨论】:

    标签: r shiny dt


    【解决方案1】:

    查看简短示例(仅适用于第 1 行)

    1) 你可以使用Shiny.bindAll

    2) 你需要在observe看到打印

     library(shiny)
    library(DT)
    mymtcars = mtcars
    mymtcars$id = 1:nrow(mtcars)
    runApp(
      list(ui = pageWithSidebar(
        headerPanel('Examples of DataTables'),
        sidebarPanel(
          checkboxGroupInput('show_vars', 'Columns to show:', names(mymtcars),
                             selected = names(mymtcars))
          ,textInput("collection_txt",label="Foo")
        ),
        mainPanel(
          DT::dataTableOutput("mytable")
        )
      )
      , server = function(input, output, session) {
    
        observe({
          print(input$row1)
        })
    
        output$mytable = DT::renderDataTable({
          #Display table with checkbox buttons
          DT::datatable(cbind(Pick=paste0('<input type="checkbox" id="row', mymtcars$id, '" value="', mymtcars$id, '">',""), mymtcars[, input$show_vars, drop=FALSE]),
                        options = list(orderClasses = TRUE,
                                       lengthMenu = c(5, 25, 50),
                                       pageLength = 25 ,
    
                                       drawCallback= JS(
                                         'function(settings) {
                                             Shiny.bindAll(this.api().table().node());}')
                        ),selection='none',escape=F)
    
    
        } )
    
      })
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-10
      • 2016-09-01
      • 1970-01-01
      • 2012-10-17
      • 1970-01-01
      • 2015-02-25
      • 2020-02-29
      • 1970-01-01
      相关资源
      最近更新 更多