gpt4 book ai didi

How to show/hide cards in with shinyjs and bslib with a D.R.Y. approach(如何用DR.Y.方法显示/隐藏带有shinyjs和bslb的卡片)

翻译 作者:bug小助手 更新时间:2023-10-26 22:15:09 37 4
gpt4 key购买 nike



Consider the following mockup shiny app. It does what I need (effectively toggling the show/hide of each card based on the checkbox, input$checkbox), but it's not very D.R.Y. and would be hard to scale and maintain.

考虑一下下面的样机闪亮的应用程序。它做了我需要的事情(根据复选框有效地切换每张牌的显示/隐藏,输入$CheckBox),但它不是非常D.R.Y,而且很难扩展和维护。


library(shiny)
library(tidyverse)
library(bslib)
library(shinyjs)

cards <- list(
card(
id = "card1",
card_header("Card 1"),
plotOutput("card1_plot")
),
card(
id = "card2",
card_header("Card 2"),
plotOutput("card2_plot")
),
card(
id = "card3",
card_header("Card 3"),
plotOutput("card3_plot")
),
card(
id = "card4",
card_header("Card 4"),
plotOutput("card4_plot")
)
)

ui <- page_sidebar(
useShinyjs(),
title = "My Dashboard",
sidebar = sidebar(
"Controls",
checkboxGroupInput("checkbox", "Select Stuff",
choices = c("card1", "card2", "card3", "card4"),
selected = c("card1", "card2", "card3", "card4"))
),
"Main Content Area",
fillable = FALSE,
cards
)

server <- function(input, output) {

observeEvent(input$checkbox, {
if (is.null(input$checkbox)) hide("card1"); hide("card2"); hide("card3"); hide("card4");
if ("card1" %in% input$checkbox) show("card1") else hide("card1")
if ("card2" %in% input$checkbox) show("card2") else hide("card2")
if ("card3" %in% input$checkbox) show("card3") else hide("card3")
if ("card4" %in% input$checkbox) show("card4") else hide("card4")
}, ignoreNULL = FALSE)

}

shinyApp(ui = ui, server = server)

Quick visual:

快速查看:


enter image description here


Is there a way to abstract the multiple similar calls to if ("cardXX" %in% input$checkbox) show("cardXX") else hide("cardXX")? Likewise for the multiple calls to hide("cardXX"); in the if (is.null(input$checkbox)) line?

有没有办法将多个类似的调用抽象为if(“cardXX”%in%INPUT$CheckBox)show(“cardXX”)或者Hide(“cardXX”)?同样,对于要隐藏的多个调用(“cardXX”);在if(is.ull(Input$CheckBox))行中?


更多回答
优秀答案推荐

You could vectorize the content of the observeEvent:

您可以向量化观察事件的内容:


card_ids = c("card1", "card2", "card3", "card4")

observeEvent(input$checkbox, {
lapply(card_ids, function(card) {
toggle(id = card, condition = card %in% input$checkbox)
})
}, ignoreNULL = FALSE)

Notice that the toggle call here is a short form for the if () show () else hide () parts.

注意,这里的切换调用是if()show()Else Hide()部分的简短形式。


Concerning the if (is.null(input$checkbox)) clause which shall hide all cards if nothing is selected I don't think that it is needed at all. If it is really needed for some reason, you could use the fact that all of your card ids start with "card" and shorten it to

关于if(is.NULL(INPUT$CHECKBOX))子句,如果没有选择任何内容,它将隐藏所有卡片,我认为根本不需要它。如果出于某种原因确实需要它,您可以利用这样一个事实,即您所有的卡ID都以“Card”开头,并将其缩短为


if (is.null(input$checkbox)) hide(selector = "[id^='card']");

Complete example:

完整的示例:


library(shiny)
library(tidyverse)
library(bslib)
library(shinyjs)

card_ids = c("card1", "card2", "card3", "card4")

cards <- list(
card(id = "card1",
card_header("Card 1"),
plotOutput("card1_plot")),
card(id = "card2",
card_header("Card 2"),
plotOutput("card2_plot")),
card(id = "card3",
card_header("Card 3"),
plotOutput("card3_plot")),
card(id = "card4",
card_header("Card 4"),
plotOutput("card4_plot"))
)

ui <- page_sidebar(
useShinyjs(),
title = "My Dashboard",
sidebar = sidebar(
"Controls",
checkboxGroupInput(
"checkbox",
"Select Stuff",
choices = card_ids,
selected = card_ids
)
),
"Main Content Area",
fillable = FALSE,
cards
)

server <- function(input, output) {

observeEvent(input$checkbox, {
lapply(card_ids, function(card) {
toggle(id = card, condition = card %in% input$checkbox)
})
}, ignoreNULL = FALSE)

}

shinyApp(ui = ui, server = server)

更多回答

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com