添加一个列为TRUE / FALSE并显示为复选框 [英] adding a column with TRUE/FALSE and showing that as a checkbox

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

问题描述

我有一个列为 TRUE FALSE

  data(mtcars)
mtcars $ Favorite< - FALSE

我有兴趣将此列(收藏夹)显示为有光泽的复选框。

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

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


解决方案

这是你正在寻找的东西?

$ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
ui = basicPage(
h2('the mtcars data'),
DT :: dataTableOutput('mytable'),
h2(Selected),
tableOutput (checked)
),

server = function(input,output){
#helper function for make 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,...))
}
输入
}
#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函数读取复选框
shinyValue = function(id,len){
unlist(lapply(seq_len(len)),function(i){
value = input [[paste0 (id,i)]]
if(is.null(value))NA else value
}))
}
#输出读取复选框
输出$ checked < - renderTable({
data.frame(selected = shinyValue(cbox _,nrow(mtcars)))
})
}
))


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
    })
  }
))

Source : http://shiny.rstudio.com/articles/datatables.html

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

解决方案

Is this what you're looking for?

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天全站免登陆