I am trying to use bslib to theme my Shiny application but I am having trouble understanding how to utilize the layout and card properties to create the base Shiny navbarPage and tabPanel look.
I have provided code below:
library(shiny)
library(bslib)
library(DT)
library(dplyr)
options(shiny.port = 8080)
test_theme <- bs_theme(
bootswatch = "cosmo",
version = 5,
bg = "#FFFFFF", # Replaces default white with Adaptive off-white
fg = "#000000", # Default black
"navbar-bg" = "#0169d6",
"navbar-light-bg" = "#0169d6",
"navbar-light-color" = "#F6F6F6",
"navbar-light-hover-color" = "#0B2663",
"navbar-light-active-color" = "#F6F6F6",
)
# UI/Server for Module_1
module_1_ui <- function(id) {
ns <- NS(id)
nav_panel(
title = "Main",
layout_columns(
col_widths = c(3, 9),
card(
title = "Card 1",
max_height = "150px",
class = "well",
textInput(ns("textInput"), 'Enter text')
),
layout_columns(
col_widths = 12,
card(
height = "100vh",
style = "border: none;",
title = "Card 2",
uiOutput(ns('textOutput'))
)
)
)
)
# tabPanel("Main", fluid = TRUE,
# sidebarLayout(
# sidebarPanel(
# textInput(ns('textInput'), 'Enter text')
# ),
# mainPanel(
# uiOutput(ns('textOutput'))
# )
# )
# )
}
module_1_server <- function(id, session) {
moduleServer(id, function(input, output, session) {
ns <- session$ns
output$textOutput <- renderUI({
req(input$textInput)
HTML(paste0('You entered: ', input$textInput))
})
})
}
# UI/Server for Module_2
module_2_ui <- function(id) {
ns <- NS(id)
nav_panel(
title = "Side",
layout_columns(
col_widths = c(3, 9),
card(
title = "Card 1",
class = "well",
HTML("This always shows!"),
br(),
uiOutput(ns("selectUI")),
conditionalPanel(
condition = sprintf("input['%s'] == 'Test2'", ns("side_tabsets")),
uiOutput(ns("group_by_select")),
actionButton(ns("button3"), "Button 3")
)
),
layout_columns(
col_widths = 12,
card(
height = "100vh",
style = "border: none;",
title = "Card 2",
uiOutput(ns('sideOutput'))
)
)
)
)
# tabPanel("Side", fluid = TRUE,
# sidebarLayout(
# sidebarPanel(
# HTML("This always shows!"),
# br(),
# uiOutput(ns("selectUI")),
# conditionalPanel(
# condition = sprintf("input['%s'] == 'Test2'", ns("side_tabsets")),
# uiOutput(ns("group_by_select")),
# actionButton(ns("button3"), "Button 3")
# )
# ),
# mainPanel(
# uiOutput(ns('sideOutput'))
# )
# )
# )
}
module_2_server <- function(id, session) {
moduleServer(id, function(input, output, session) {
ns <- session$ns
output$selectUI <- renderUI({
selectInput(ns("data_select"), "Select Data", choices = colnames(iris), multiple = TRUE)
})
output$sideOutput <- renderUI({
tabsetPanel(
id = ns("side_tabsets"),
tabPanel(title = "Test1", br(), fluidRow(column(DT::DTOutput(ns("table")), width = 6))),
tabPanel(title = "Test2", br(), fluidRow(column(DT::DTOutput(ns("group_by_table")), width = 6)))
)
})
output$group_by_select <- renderUI({
selectInput(ns("group_by_select"), "Group By", choices = colnames(iris))
})
output$table <- DT::renderDT({
iris %>% select(input$data_select)
})
output$group_by_table <- DT::renderDT({
NULL
})
observeEvent(input$button3, {
output$group_by_table <- DT::renderDT({
iris %>% select(input$data_select) %>% group_by(input$group_by_select) %>% summarise(n = n())
})
})
})
}
# Overall UI
ui <- page_fluid(
theme = test_theme,
page_navbar(title = "",
module_1_ui("main"),
module_2_ui("side")
)
)
# Overall Server
server <- function(input, output, session) {
module_1_server("main", session)
module_2_server("side", session)
}
shinyApp(ui = ui, server = server)
I want the appearance to match the commented out bit of my original Shiny code. The few things I cannot get to work well is having a floating sidebar using card that does not span the entire page:
Why do the dropdown menus get cutoff?
I want the sidebar to grow as more elements get added but not take up the entire page.
I have tried using the height, min_height, and max_height properties inside of card and it does not grow properly and always assumes the max_height.
Hpatel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.