我有这个声明,让我得到关于我的变量的基本描述性统计数据:
checkboxGroupInput('show_vars', 'Columns in diamonds to show:',
names(input_data), selected = names(input_data))
然而,在不得不取消10个变量以获得我感兴趣的一个变量之后,我意识到这个用户界面不是很友好。我想添加一个按钮,在您单击它时选择/取消选择所有按钮。它可以多次点击。我甚至不确定如何开始。任何推动都会有所帮助。
ui.R:
library(shiny)
hw<-diamonds
shinyUI(fluidPage(
title = 'Examples of DataTables',
sidebarLayout(
sidebarPanel(
checkboxGroupInput('show_vars', 'Columns in diamonds to show:',
names(hw), selected = names(hw))
),
mainPanel(
verbatimTextOutput("summary"),
tabsetPanel(
id = 'dataset',
tabPanel('hw', dataTableOutput('mytable1'))
)
)
)
))
server.R:
library(shiny)
data(diamonds)
hw<-diamonds
shinyServer(function(input, output) {
output$summary <- renderPrint({
dataset <- hw[, input$show_vars, drop = FALSE]
summary(dataset)
})
# a large table, reative to input$show_vars
output$mytable1 <- renderDataTable({
library(ggplot2)
hw[, input$show_vars, drop = FALSE]
})
})
答案 0 :(得分:9)
我添加了global.R
来加载包和数据 - 并不总是必要的,但它通常更干净。可能有不同的方法来完成我在下面所做的事情,但我倾向于在这种情况下使用条件面板。
library(shiny)
shinyUI(fluidPage(
title = 'Examples of DataTables',
sidebarLayout(
sidebarPanel(
radioButtons(
inputId="radio",
label="Variable Selection Type:",
choices=list(
"All",
"Manual Select"
),
selected="All"),
conditionalPanel(
condition = "input.radio != 'All'",
checkboxGroupInput(
'show_vars',
'Columns in diamonds to show:',
choices=names(hw),
selected = "carat"
)
)
),
mainPanel(
verbatimTextOutput("summary"),
tabsetPanel(
id = 'dataset',
tabPanel('hw', dataTableOutput('mytable1'))
)
)
)
))
library(shiny)
library(ggplot2)
##
shinyServer(function(input, output) {
Data <- reactive({
if(input$radio == "All"){
hw
} else {
hw[,input$show_vars,drop=FALSE]
}
})
output$summary <- renderPrint({
## dataset <- hw[, input$show_vars, drop = FALSE]
dataset <- Data()
summary(dataset)
})
# a large table, reative to input$show_vars
output$mytable1 <- renderDataTable({
Data()
## hw[, input$show_vars, drop = FALSE]
})
})
library(shiny)
library(ggplot2)
data(diamonds)
hw <- diamonds
答案 1 :(得分:9)
这是我设置选择/取消选择所有按钮的方式。
在ui.R中,根据需要添加一个动作按钮:
actionButton("selectall", label="Select/Deselect all")
然后server.R根据操作按钮的条件使用updateCheckboxGroupInput。如果按下按钮的次数是偶数,它将选择全部,否则如果它是奇数,它将选择无。
# select/deselect all using action button
observe({
if (input$selectall > 0) {
if (input$selectall %% 2 == 0){
updateCheckboxGroupInput(session=session,
inputId="show_vars",
choices = list("carat" = "carat",
"cut" = "cut",
"color" = "color",
"clarity"= "clarity",
"depth" = "depth",
"table" = "table",
"price" = "price",
"x" = "x",
"y" = "y",
"z" = "z"),
selected = c(names(hw)))
} else {
updateCheckboxGroupInput(session=session,
inputId="show_vars",
choices = list("carat" = "carat",
"cut" = "cut",
"color" = "color",
"clarity"= "clarity",
"depth" = "depth",
"table" = "table",
"price" = "price",
"x" = "x",
"y" = "y",
"z" = "z"),
selected = c())
}}
})
以下示例的完整应用程序 - 您需要将会话添加到服务器功能,我在没有选择变量时添加了renderDataTable的条件。
library(shiny)
library(ggplot2)
data(diamonds)
hw <- diamonds
runApp(
list(
ui=(
fluidPage(
title = 'Examples of DataTables',
sidebarLayout(
sidebarPanel(
actionButton("selectall", label="Select/Deselect all"),
checkboxGroupInput('show_vars', 'Columns in diamonds to show:',
names(hw), selected = names(hw))
),
mainPanel(
verbatimTextOutput("summary"),
tabsetPanel(
id = 'dataset',
tabPanel('hw', dataTableOutput('mytable1'))
))))),
server = (function(input, output, session) {
output$summary <- renderPrint({
dataset <- hw[, input$show_vars, drop = FALSE]
summary(dataset)
})
observe({
if (input$selectall > 0) {
if (input$selectall %% 2 == 0){
updateCheckboxGroupInput(session=session, inputId="show_vars",
choices = list("carat" = "carat",
"cut" = "cut",
"color" = "color",
"clarity"= "clarity",
"depth" = "depth",
"table" = "table",
"price" = "price",
"x" = "x",
"y" = "y",
"z" = "z"),
selected = c(names(hw)))
}
else {
updateCheckboxGroupInput(session=session, inputId="show_vars",
choices = list("carat" = "carat",
"cut" = "cut",
"color" = "color",
"clarity"= "clarity",
"depth" = "depth",
"table" = "table",
"price" = "price",
"x" = "x",
"y" = "y",
"z" = "z"),
selected = c())
}}
})
# a large table, reative to input$show_vars
output$mytable1 <- renderDataTable({
if (is.null(input$show_vars)){
data.frame("no variables selected" = c("no variables selected"))
} else{
hw[, input$show_vars, drop = FALSE]
}
})
})
))
答案 2 :(得分:5)
shinyWidgets
库有一个很好的函数叫pickerInput()
,附带一个“全选/取消全选”功能。经过大量研究,我发现这是唯一内置此功能的Shiny输入: