file_picker Server Functions
mod_file_picker_server.Rd
Server function of a Shiny module for selecting either a single file or multiple files to be used within the app. This module displays a modal dialog with a preview table of files and allows users to select files based on the specified `selection` mode ('single' or 'multiple'). The table columns are filterable, and different types of filters are available depending on the type of data in each column:
For numeric columns: a range slider filter allows users to filter the table by selecting a range of values.
For factor columns: a drop-down filter allows users to filter the table by selecting specific factor levels.
For other data types: a basic filter using a case-insensitive text match is available.
Usage
mod_file_picker_server(
id,
files_df,
selection = "single",
file_identifier_column = "path",
default_page_size = 10,
use_bslib_theme = FALSE,
show_guide = TRUE,
guide_content = "default",
...
)
Arguments
- id
A unique identifier for the module instance.
- files_df
A data frame or reactive expression returning a data frame, containing file information. This data frame should have a column for the file paths and any other relevant metadata. If provided as a reactive expression, ensure it returns a data frame in the required format. You can use the
get_all_project_files
function to fetch all project files along with their metadata from the SB File system within Data Studio. This function returns a data frame containing comprehensive file information, making it an ideal input for the `mod_file_picker_server()` function.- selection
A string specifying the selection mode. Can be either 'single' for single file selection or 'multiple' for multiple file selection. The default value is 'single'.
- file_identifier_column
A string specifying the column name in `files_df` from which the values of selected files will be returned. Defaults to `path`.
- default_page_size
Number of rows per page to display in the table. Defaults to 10.
- use_bslib_theme
A logical value indicating if the modal's UI should be generated using the
bslib
package. IfFALSE
(the default), the regular UI will be generated. IfTRUE
, the UI will be generated using thebslib
package and its functions. Note that to use this option, the main UI of the app must include the linetheme = bslib::bs_theme()
. This requirement ensures the correct application of thebslib
theme throughout the app.- show_guide
A boolean indicating whether to show the File Selection Guide. The default value is `TRUE`.
- guide_content
A string that controls the content of the File Selection Guide.
**"default"**: When set to `"default"` (the default value), the function displays a standard guide that provides instructions on how to manage file selection, including steps like reviewing file details, selecting files, using search and filter features, and submitting selected files.
**File path**: Alternatively, developers can provide a file path to a Markdown (`.md`) file. If a valid file path is provided, the `generate_guide_content_from_file()` function will read the Markdown file, convert it to HTML, and render it within the File Selection Guide box. This allows for custom, user-defined instructions to be displayed in the guide. If the Shiny app is being developed within the Golem framework, it is recommended to place the Markdown file inside the `inst/` directory. You can then reference the file using `system.file("path/inside/inst/filename.md", package = "yourgolemapp")` to ensure the file is properly bundled and accessible after deployment.
The guide is collapsible and can be customized to adapt to single or multiple file selection modes.
- ...
Additional parameters to be passed to the `reactable()` function this module relies on.
Value
A reactive expression containing information about the selected files based on the specified `file_identifier_column`.
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_ui
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)