添加带有 TRUE/FALSE 的列并将其显示为复选框 [英] adding a column with TRUE/FALSE and showing that as a checkbox

查看:22
本文介绍了添加带有 TRUE/FALSE 的列并将其显示为复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一列带有 TRUEFALSE

I have a column with TRUE or FALSE values

data("mtcars")
mtcars$Favorite <- "FALSE"

我有兴趣将此列(收藏夹)显示为闪亮的复选框.

I am interested in displaying this column (Favorite) as a checkbox on shiny.

runApp(list(
  ui = basicPage(
    h2('The mtcars data'),
    dataTableOutput('mytable')
  ),
  server = function(input, output) {
    output$mytable = renderDataTable({
      mtcars
    })
  }
))

来源:http://shiny.rstudio.com/articles/datatables.html

不知道如何使它工作,非常感谢任何帮助.

Not sure how to make it work, any help is much appreciated.

推荐答案

这是您要找的吗?

library(shiny)
library(DT) # dev from github
runApp(list(
  ui = basicPage(
    h2('The mtcars data'),
    DT::dataTableOutput('mytable'),
    h2("Selected"),
    tableOutput("checked")
  ),

  server = function(input, output) {
    # helper function for making checkbox
    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 
    } 
    # datatable with checkbox
    output$mytable = DT::renderDataTable({
      data.frame(mtcars,Favorite=shinyInput(checkboxInput,nrow(mtcars),"cbox_"))
    }, server = FALSE, escape = FALSE, options = list( 
      paging=FALSE,
      preDrawCallback = JS('function() { 
Shiny.unbindAll(this.api().table().node()); }'), 
      drawCallback = JS('function() { 
Shiny.bindAll(this.api().table().node()); } ') 
    ) )
    # helper function for reading checkbox
    shinyValue = function(id, len) { 
      unlist(lapply(seq_len(len), function(i) { 
        value = input[[paste0(id, i)]] 
        if (is.null(value)) NA else value 
      })) 
    } 
    # output read checkboxes
    output$checked <- renderTable({
      data.frame(selected=shinyValue("cbox_",nrow(mtcars)))
    })
  }
))

这篇关于添加带有 TRUE/FALSE 的列并将其显示为复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆