Conditional Card. #938
-
Hi everyone, I'm a newcomer to this community and currently working on a Shiny app using the bslib package. It's been quite an experience, and I find the pack very useful!! I've encountered a challenge that I'm struggling to overcome. Specifically, I'm aiming to create a "conditional card" (specifically the XXX card) only if the .csv file is successfully loaded. I've attached an example of the code I'm working on for your reference. Your insights and assistance would be greatly appreciated! Thank you in advance for your help. Cheers, #--------#
# Card a #
#--------#
pacman::p_load(b, data.table, DT, shiny, tidyverse, openxlsx)
cards.a <- list(
card(full_screen = FALSE,
card_header("YYY"),
# Sidebar ----
layout_sidebar(
open = TRUE, # Open sidebar
fillable = TRUE,
# Side bar
sidebar = sidebar(
width = 400, # sidebar width
# Input file
fileInput("file1",
markdown("Chose CSV File"),
accept = c(".csv")),
),
# Main content ----
card(card_header("XXX"),
full_screen = FALSE,
DT::dataTableOutput("inputDataframe"))
)
)
)
# cards.a[[1]]
#----#
# UI #
#----#
ui <- page_navbar(
theme = bs_theme(version = 5, bootswatch = "superhero"), # Bootstrap version and theme
title = "AAA App", # title of the app
# Taxonomy panel ----
nav_panel(title = "ZZZ",
layout_columns(
fill = TRUE,
col_widths = 12,
cards.a[[1]]
)
)
)
#--------#
# SERVER #
#--------#
server <- function(input, output, session) {
# Objects to store files ----
temp_df <- reactiveValues(df_data = NULL) # <- Store .csv file input
# Show table whit uploaded data (temp_df)
output$inputDataframe <- DT::renderDataTable({
req(input$file1)
temp_df$df_data <- fread(input$file1$datapath, sep = ",") %>%
as.data.frame()
temp_df$df_data
},
options = (list(scrollX = TRUE, paging = FALSE, scrollY = "240px")),
rownames= FALSE)
}
shinyApp(ui, server) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Hi @TommasoCanc! For a conditional card, I'd recommend creating the card on the server with dynamic UI using |
Beta Was this translation helpful? Give feedback.
-
It works! Thx so much! I wrote my solution just in case. Cheers, #---------------#
# Card a #
#---------------#
pacman::p_load(bslib, data.table, DT, shiny, tidyverse, openxlsx)
cards.a <- list(
card(full_screen = FALSE,
card_header("YYY"),
# Sidebar ----
layout_sidebar(
open = TRUE, # Open sidebar
fillable = TRUE,
# Side bar
sidebar = sidebar(
width = 400, # sidebar width
# Input file
fileInput("file1",
markdown("Chose CSV File"),
accept = c(".csv")),
),
# Main content ----
uiOutput("moreControls")
)
))
#----#
# UI #
#----#
ui <- page_navbar(
theme = bs_theme(version = 5, bootswatch = "superhero"), # Bootstrap version and theme
title = "AAA App", # title of the app
# Side panel ----
nav_panel(title = "ZZZ",
layout_columns(
fill = TRUE,
col_widths = 12,
cards.a[[1]]
)
)
)
#--------#
# SERVER #
#--------#
server <- function(input, output, session) {
# Objects to store files ----
temp_df <- reactiveValues(df_data = NULL) # <- Store .csv file input
output$inputDataframe <- DT::renderDataTable({
req(input$file1)
temp_df$df_data <- fread(input$file1$datapath, sep = ",") %>%
as.data.frame()
temp_df$df_data
},
options = (list(scrollX = TRUE, paging = FALSE, scrollY = "240px")),
rownames= FALSE)
output$moreControls <- renderUI({
req(input$file1)
card(
card_header("XXX"),
full_screen = FALSE,
DT::dataTableOutput("inputDataframe")
)
})
}
shinyApp(ui, server) |
Beta Was this translation helpful? Give feedback.
Hi @TommasoCanc! For a conditional card, I'd recommend creating the card on the server with dynamic UI using
renderUI()
. If you're placing the card in a filling layout, likelayout_columns()
, you should also addfill = TRUE
to theuiOutput()
that adds the card to the UI.