| Title: | Interactive File Import and Export Blocks |
|---|---|
| Description: | Extends 'blockr.core' with interactive blocks for reading and writing data files. Supports CSV, Excel, Parquet, RDS, and other formats through a graphical interface without writing code directly. Includes file browser integration and configurable import/export options. |
| Authors: | Christoph Sax [aut, cre] (ORCID: <https://orcid.org/0000-0002-7192-7044>), Nicolas Bennett [aut], David Granjon [aut], Mike Page [aut], Bristol Myers Squibb [fnd] |
| Maintainer: | Christoph Sax <[email protected]> |
| License: | GPL (>= 3) |
| Version: | 0.1.0.9000 |
| Built: | 2026-05-26 15:07:10 UTC |
| Source: | https://github.com/bristolmyerssquibb/blockr.io |
Categorizes a file by its extension into a broad format family that determines reader dispatch and UI adaptation.
file_category(path)file_category(path)
path |
Character. File path. |
One of "csv", "excel", "arrow", "other".
Returns a character vector of file extensions (without dots) supported by the read block. Useful for sibling packages that need to filter or validate file paths before passing them to blockr.io.
file_extensions()file_extensions()
Character vector of file extensions (without dots)
A board-level option that sets a default data directory for read and write blocks. When set, file paths entered in blocks are resolved relative to this directory.
new_data_dir_option( value = blockr_option("data_dir", ""), category = "Data", ... )new_data_dir_option( value = blockr_option("data_dir", ""), category = "Data", ... )
value |
Character. Initial directory path. Default: empty string (no data directory). |
category |
Character. Option category for UI grouping. |
... |
Forwarded to |
A board_option object.
A variadic block that lets the user download one or more data frames as a
file in the browser. Intended as a lightweight counterpart to
new_write_block() for the common case where no server-side save is
needed.
new_download_block(filename = "", format = "csv", args = list(), ...)new_download_block(filename = "", format = "csv", args = list(), ...)
filename |
Character. Optional fixed filename (without extension). If empty (default), a timestamped filename is generated. |
format |
Character. One of the values in |
args |
Named list of format-specific writing parameters (same as
|
... |
Forwarded to |
Multi-input behavior matches new_write_block(): multiple inputs produce
a multi-sheet Excel file for format = "excel", or a ZIP archive for CSV,
Parquet, and Feather.
Adding a new format (e.g. SAS) only requires extending
write_formats(), format_extension(), and the dispatch in
write_expr() - both new_write_block() and new_download_block()
pick it up automatically.
A blockr transform block exposing a download button.
if (interactive()) { library(blockr.core) serve(new_download_block()) }if (interactive()) { library(blockr.core) serve(new_download_block()) }
A single block for reading files in various formats with smart UI that adapts based on detected file type. Supports "From Browser" (upload) and "Location" (path/URL input) modes with persistent storage for uploaded files.
new_read_block( path = character(), source = "upload", combine = "auto", args = list(), ... )new_read_block( path = character(), source = "upload", combine = "auto", args = list(), ... )
path |
Character vector of file paths to pre-load. Accepts local paths and URLs. When provided, automatically switches to "path" mode regardless of the source parameter. |
source |
Either "upload" for file upload widget or "path" for path/URL input. Default: "upload". Automatically set based on path parameter. |
combine |
Strategy for combining multiple files: "auto", "rbind", "cbind", "first" |
args |
Named list of format-specific reading parameters. Only specify values that differ from defaults. Available parameters:
|
... |
Forwarded to |
The block supports two modes:
From Browser mode (upload):
User uploads files from their computer via the browser
Files are copied to persistent storage directory (upload_path)
State stores permanent file paths
Works across R sessions with state restoration
Location mode (path):
User enters a file path or URL in a text input with autocomplete
For server paths: reads directly from original location
For URLs: downloads to a temporary file each time
When a board-level data directory is set, paths are resolved relative to it
After file selection, the UI detects file type and shows relevant options:
CSV/TSV: Delimiter, quote character, encoding options
Excel: Sheet selection, cell range
Other formats: Minimal or no options (handled automatically)
When multiple files are selected:
"auto": Attempts rbind, falls back to first file if incompatible
"rbind": Row-binds files (requires same columns)
"cbind": Column-binds files (requires same row count)
"first": Uses only the first file
A blockr data block that reads file(s) and returns a data.frame.
The following settings are retrieved from options and not stored in block state:
upload_path: Directory for persistent file storage. Set via
options(blockr.upload_path = "/path") or environment variable BLOCKR_UPLOAD_PATH.
Default: tools::R_user_dir("blockr", "data")
# Create a read block for a CSV file csv_file <- tempfile(fileext = ".csv") write.csv(mtcars[1:5, ], csv_file, row.names = FALSE) block <- new_read_block(path = csv_file) block # With custom CSV parameters block <- new_read_block( path = csv_file, args = list(n_max = 3) ) if (interactive()) { # Launch interactive app serve(new_read_block()) }# Create a read block for a CSV file csv_file <- tempfile(fileext = ".csv") write.csv(mtcars[1:5, ], csv_file, row.names = FALSE) block <- new_read_block(path = csv_file) block # With custom CSV parameters block <- new_read_block( path = csv_file, args = list(n_max = 3) ) if (interactive()) { # Launch interactive app serve(new_read_block()) }
A variadic block for writing dataframes to files in various formats. Accepts multiple input dataframes and handles single files, multi-sheet Excel, or ZIP archives depending on format and number of inputs.
new_write_block( directory = "", filename = "", format = "csv", auto_write = FALSE, args = list(), mode = NULL, ... )new_write_block( directory = "", filename = "", format = "csv", auto_write = FALSE, args = list(), mode = NULL, ... )
directory |
Character. Default directory for file output. When non-empty,
enables server-side writing. Can be configured via
|
filename |
Character. Optional fixed filename (without extension).
|
format |
Character. Output format: "csv", "excel", "parquet", or "feather". Default: "csv" |
auto_write |
Logical. When TRUE, automatically writes files when data changes (requires a non-empty directory). When FALSE (default), user must click "Save to File" button. |
args |
Named list of format-specific writing parameters. Only specify values that differ from defaults. Available parameters:
|
mode |
|
... |
Forwarded to |
This block accepts multiple dataframe inputs (1 or more) similar to bind_rows_block.
Inputs can be numbered ("1", "2", "3") or named ("sales_data", "inventory").
Input names are used for sheet names (Excel) or filenames (multi-file ZIP).
Single input:
Writes single file in specified format
Filename: {filename}.{ext} or data_{timestamp}.{ext}
Multiple inputs + Excel:
Single Excel file with multiple sheets
Sheet names derived from input names
Multiple inputs + CSV/Arrow:
Single ZIP file containing individual files
Each file named from input names
Fixed filename (filename = "output"):
Reproducible path: always writes to {directory}/output.{ext}
Overwrites file on every upstream data change
Ideal for automated pipelines
Auto-timestamped (filename = ""):
Unique files: {directory}/data_YYYYMMDD_HHMMSS.{ext}
Preserves history, prevents accidental overwrites
Safe default behavior
Both options are always available in a flat layout (no tabs):
Download to Browser:
Always available via the download button
Triggers a download to your browser's download folder
Save to Server:
Active when a server directory path is set (non-empty)
User enters a directory path in the path input
Files persist on server
When running locally, this is your computer's file system
A blockr transform block that writes dataframes to files
# Create a write block for CSV output block <- new_write_block( directory = tempdir(), filename = "output", format = "csv" ) block # Write block for Excel with auto-timestamp block <- new_write_block( directory = tempdir(), filename = "", format = "excel" ) if (interactive()) { # Launch interactive app serve(new_write_block()) }# Create a write block for CSV output block <- new_write_block( directory = tempdir(), filename = "output", format = "csv" ) block # Write block for Excel with auto-timestamp block <- new_write_block( directory = tempdir(), filename = "", format = "excel" ) if (interactive()) { # Launch interactive app serve(new_write_block()) }
A Shiny module that provides a text input with server-side file/directory autocomplete. Used by the read and write blocks to replace shinyFiles browser widgets.
path_input_ui(id, prefix = NULL, upload_id = NULL) path_input_server( id, data_dir = reactive(""), mode = c("file", "directory"), extensions = NULL )path_input_ui(id, prefix = NULL, upload_id = NULL) path_input_server( id, data_dir = reactive(""), mode = c("file", "directory"), extensions = NULL )
id |
Module namespace ID. |
prefix |
Optional initial prefix text shown before the input (typically the data directory path). |
upload_id |
Optional ID of a hidden Shiny |
data_dir |
Reactive returning the current data directory path (from board options). Empty string means no data directory. |
mode |
Either |
extensions |
Optional character vector of file extensions (without dots)
to show in autocomplete. Defaults to |
path_input_ui() returns a tagList with the widget HTML.
path_input_server() returns a reactive containing the current
path text value.
Convenience wrapper that detects the file category from the path and returns an unevaluated R expression for reading the file. Useful for sibling packages that construct pipelines programmatically.
read_file_expr(path, ...)read_file_expr(path, ...)
path |
Character. Path to a single file. |
... |
Additional parameters forwarded to the reader (e.g. |
A language object (unevaluated call) that, when evaluated, reads the file into a data frame.
Convenience wrapper that detects the file format from the path extension and returns an unevaluated R expression for writing the data. Useful for sibling packages that construct pipelines programmatically (e.g. DM blocks).
write_file_expr(data, path, ...)write_file_expr(data, path, ...)
data |
Character. Name of the data object to write. |
path |
Character. Output file path (extension determines format). |
... |
Additional parameters forwarded to the writer (e.g. |
A language object (unevaluated call) that, when evaluated, writes the data frame to the file.
write_file_expr("mtcars", "/tmp/cars.csv") write_file_expr("iris", "/tmp/flowers.xlsx") write_file_expr("df", "/tmp/data.parquet")write_file_expr("mtcars", "/tmp/cars.csv") write_file_expr("iris", "/tmp/flowers.xlsx") write_file_expr("df", "/tmp/data.parquet")
Returns a named character vector of supported output formats for writing data frames. Names are display labels, values are format identifiers. Used to populate format dropdowns in the write block UI.
write_formats()write_formats()
A named character vector.