R + Shiny + conditionalPanel:在 UI 和服务器端组合条件
R + Shiny + conditionalPanel: combining condition on UI and server side
我正在努力完成这项工作:当且仅当满足两个条件时,我想在闪亮的应用程序的侧边栏中显示一个按钮(称为“下载聚合数据”),即
(1) 我 select “显示额外按钮”下菜单中的“是”值
(2) select 编辑并显示在 table 中的数据至少有 20 行。
条件 (2) 是在“select 数据块”菜单中 selecting“large_chunk” 的结果,但我希望条件基于输出而不是在输入上。在我正在开发的实际应用程序中,我不会事先知道我将使用过滤器 select 多少数据行。
目前只实现了条件 (1)。
我在这里找到了一些关于 conditionalPanel 和服务器端条件的有用信息
Shiny - conditionalPanel - set condition as output from server
但我并没有走多远。
感谢您的帮助!
library(shiny)
library(shinyWidgets)
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(DT)
#>
#> Attaching package: 'DT'
#> The following objects are masked from 'package:shiny':
#>
#> dataTableOutput, renderDataTable
df <- tibble(x=1:30, y=51:80, z=c(rep("small_chunk", 7), rep("large_chunk", 23)))
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
pickerInput("my_selection","Select data chunk",
choices=c("large_chunk", "small_chunk"),
selected="small_chunk",
options = list(`actions-box` = TRUE,`selected-text-format` = "count > 3"),multiple = F),
pickerInput("show","Show Extra Button",
choices=c("yes", "no"),
selected="yes",
options = list(`actions-box` = TRUE,`selected-text-format` = "count > 3"),multiple = F),
conditionalPanel(
condition = "input.show == 'yes' ",
downloadButton("downloadData","Download aggregated data" ))
),
mainPanel(
DTOutput("table")
)
)
)
server <- function(input, output) {
data_reactive <- reactive({
df %>% filter(z %in% input$my_selection )
})
output$table <- renderDT({datatable(data_reactive())})
}
shinyApp(ui = ui, server = server)
#> PhantomJS not found. You can install it with webshot::install_phantomjs(). If it is installed, please make sure the phantomjs executable can be found via the PATH variable.
静态 R Markdown 文档不支持闪亮的应用程序
由 reprex package (v2.0.1)
于 2021-09-22 创建
你可以做到
output[["largeChunk"]] <- reactive({
nrow(data_reactive()) >= 20
})
outputOptions(output, "largeChunk", suspendWhenHidden = FALSE)
并在 UI 中:
conditionalPanel(
condition = "input.show == 'yes' && output.largeChunk",
......
我正在努力完成这项工作:当且仅当满足两个条件时,我想在闪亮的应用程序的侧边栏中显示一个按钮(称为“下载聚合数据”),即
(1) 我 select “显示额外按钮”下菜单中的“是”值
(2) select 编辑并显示在 table 中的数据至少有 20 行。
条件 (2) 是在“select 数据块”菜单中 selecting“large_chunk” 的结果,但我希望条件基于输出而不是在输入上。在我正在开发的实际应用程序中,我不会事先知道我将使用过滤器 select 多少数据行。
目前只实现了条件 (1)。
我在这里找到了一些关于 conditionalPanel 和服务器端条件的有用信息
Shiny - conditionalPanel - set condition as output from server
但我并没有走多远。 感谢您的帮助!
library(shiny)
library(shinyWidgets)
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(DT)
#>
#> Attaching package: 'DT'
#> The following objects are masked from 'package:shiny':
#>
#> dataTableOutput, renderDataTable
df <- tibble(x=1:30, y=51:80, z=c(rep("small_chunk", 7), rep("large_chunk", 23)))
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
pickerInput("my_selection","Select data chunk",
choices=c("large_chunk", "small_chunk"),
selected="small_chunk",
options = list(`actions-box` = TRUE,`selected-text-format` = "count > 3"),multiple = F),
pickerInput("show","Show Extra Button",
choices=c("yes", "no"),
selected="yes",
options = list(`actions-box` = TRUE,`selected-text-format` = "count > 3"),multiple = F),
conditionalPanel(
condition = "input.show == 'yes' ",
downloadButton("downloadData","Download aggregated data" ))
),
mainPanel(
DTOutput("table")
)
)
)
server <- function(input, output) {
data_reactive <- reactive({
df %>% filter(z %in% input$my_selection )
})
output$table <- renderDT({datatable(data_reactive())})
}
shinyApp(ui = ui, server = server)
#> PhantomJS not found. You can install it with webshot::install_phantomjs(). If it is installed, please make sure the phantomjs executable can be found via the PATH variable.
静态 R Markdown 文档不支持闪亮的应用程序
由 reprex package (v2.0.1)
于 2021-09-22 创建你可以做到
output[["largeChunk"]] <- reactive({
nrow(data_reactive()) >= 20
})
outputOptions(output, "largeChunk", suspendWhenHidden = FALSE)
并在 UI 中:
conditionalPanel(
condition = "input.show == 'yes' && output.largeChunk",
......