file_picker UI Function
mod_file_picker_ui.Rd
UI function of the file picker Shiny module. It adds an action button that triggers a file picker modal. This module allows users to select either a single file or multiple files to be used within the app. The UI function should be placed in the UI part of the Shiny app where the file picker button is required.
Usage
mod_file_picker_ui(
id,
button_label = "Add files",
button_icon = icon("circle-plus"),
button_width = "100%"
)
Details
To incorporate this module into your Shiny app, you need to include both the UI and server functions in the appropriate places in your app code:
**UI Function**: Add `mod_file_picker_ui("file_picker_1")` to the UI part of your Shiny app where you want the action button to appear.
**Server Function**: Add `mod_file_picker_server("file_picker_1", files_df)` to the server part of your Shiny app. Make sure to replace `files_df` with your actual data frame which contains file information.
See also
mod_file_picker_server
for the corresponding server
part of the module.
Examples
library(shiny)
library(sbShinyModules)
# library(bslib) # uncomment if you want to use a Bootstrap theme
# App's UI
ui <- fluidPage(
titlePanel("File Picker Module Examples"),
# theme = bslib::bs_theme(), # uncomment if you want to use a Bootstrap theme
sidebarLayout(
sidebarPanel(
# Single File Picker - UI
fluidRow(
h3("Single File Picker"),
br(),
sbShinyModules::mod_file_picker_ui("single_file_picker"),
br(),
h5("Selected File"),
verbatimTextOutput("single_file_picker_selection", placeholder = TRUE)
),
hr(),
# Multiple Files Picker - UI
fluidRow(
h3("Multiple Files Picker"),
br(),
sbShinyModules::mod_file_picker_ui("multiple_files_picker"),
br(),
h5("Selected Files"),
verbatimTextOutput("mult_files_picker_selection", placeholder = TRUE)
)
),
mainPanel(
# Placeholder
)
)
)
# App Server Logic
server <- function(input, output, session) {
# ----------------------------- Load Files ----------------------------------
# Load a built-in data frame for files
files_df <- sbShinyModules::file_picker_example_data
# Remove units (bytes) from the size column and make it numeric so that it
# has a range filter
files_df$size <- as.numeric(gsub(" bytes", "", files_df$size))
# Alternatively, fetch files from a provided directory (path) using the
# get_all_project_files() utility function. Note that this requires the
# xattrs package, which is not available for Windows systems. Therefore,
# this approach will only work on Unix-based systems.
# files_df <- sbShinyModules::get_all_project_files(
# path = "/sbgenomics/project-files"
# )
# ---------------------------------------------------------------------------
## -------------------- Single File Picker - Server Code --------------------
# Call the file picker module
selected_files_single_picker <- sbShinyModules::mod_file_picker_server(
id = "single_file_picker",
files_df = files_df,
# use_bslib_theme = TRUE, # Uncomment if you want to use a Bootstrap theme
selection = "single",
default_page_size = 5,
show_guide = TRUE,
guide_content = "default"
# If you want to use a custom guide instead of the default,
# comment out the line above (guide_content = "default")
# and uncomment the following lines to use the built-in example Markdown
# file:
# guide_content = system.file("app/md/file_picker_custom_guide_example.md",
# package = "sbShinyModules")
)
# Display selected files
output$single_file_picker_selection <- renderPrint({
validate(
need(
selected_files_single_picker(),
"No file has been selected."
)
)
cat(selected_files_single_picker(), sep = "\n")
})
# ---------------------------------------------------------------------------
## ----------------- Multiple Files Picker - Server Code --------------------
# Call the file picker module
selected_files_mult_picker <- sbShinyModules::mod_file_picker_server(
id = "multiple_files_picker",
files_df = files_df,
# use_bslib_theme = TRUE, # uncomment if you want to use a Bootstrap theme
selection = "multiple",
default_page_size = 5,
show_guide = TRUE,
guide_content = "default"
# If you want to use a custom guide instead of the default,
# comment out the line above (guide_content = "default")
# and uncomment the following lines to use the built-in example Markdown
# file:
# guide_content = system.file("app/md/file_picker_custom_guide_example.md",
# package = "sbShinyModules")
)
# Display selected files
output$mult_files_picker_selection <- renderPrint({
validate(
need(
selected_files_mult_picker(),
"No files have been selected."
)
)
cat(selected_files_mult_picker(), sep = "\n")
})
# ---------------------------------------------------------------------------
}
# Note: To use a Bootstrap theme, ensure you have the 'bslib' package
# installed and loaded in your app.
# 1. Uncomment `library(bslib)` at the beginning of the script.
# 2. Uncomment the `theme = bslib::bs_theme()` line in the UI section.
# 3. In the server logic, set `use_bslib_theme = TRUE` in the
# `mod_file_picker_server()` function calls.
# Run the Shiny app
shinyApp(ui, server)