添加带有TRUE/FALSE的列并将其显示为复选框
我有一列包含添加带有TRUE/FALSE的列并将其显示为复选框,r,datatable,shiny,R,Datatable,Shiny,我有一列包含TRUE或FALSE值 data("mtcars") mtcars$Favorite <- "FALSE" 资料来源: 不知道如何使其工作,非常感谢任何帮助。这就是您要找的吗 library(shiny) library(DT) # dev from github runApp(list( ui = basicPage( h2('The mtcars data'), DT::dataTableOutput('mytable'), h2("Selec
TRUE
或FALSE
值
data("mtcars")
mtcars$Favorite <- "FALSE"
资料来源:
不知道如何使其工作,非常感谢任何帮助。这就是您要找的吗
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)))
})
}
))
库(闪亮)
图书馆(DT)#github开发
runApp(列表(
ui=基本页面(
h2(“mtcars数据”),
DT::dataTableOutput('mytable'),
h2(“选定”),
表格输出(“已检查”)
),
服务器=功能(输入、输出){
#用于生成复选框的助手函数
shinyInput=函数(FUN,len,id,…{
输入=字符(len)
对于(i在seq_len(len)){
输入[i]=as.character(FUN(paste0(id,i),label=NULL,…)
}
投入
}
#带有复选框的数据表
输出$mytable=DT::renderDataTable({
data.frame(mtcars,Favorite=shinyInput(checkboxInput,nrow(mtcars),“cbox”))
},server=FALSE,escape=FALSE,options=list(
分页=假,
preDrawCallback=JS('function(){
.unbindAll(this.api().table().node();}'),
drawCallback=JS('function(){
.bindAll(this.api().table().node();}'))
) )
#用于读取复选框的帮助函数
shinyValue=函数(id,len){
unlist(lapply)(seq_len(len),函数(i){
值=输入[[paste0(id,i)]]
if(is.null(value))NA else值
}))
}
#输出读取复选框
输出$checked这可能会有帮助:就是这样,正是我想要的。谢谢。这是一个很好的解决方案。是否还有一个选项可以根据其他行的值跳过某些行的复选框?
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)))
})
}
))