Writes file content extracted from Shinylive applications to disk, handling both text and binary content appropriately. Creates any necessary parent directories and ensures proper encoding of content. For binary files, automatically decodes the base64-encoded content before writing.
Arguments
- content
Character string containing the file content. For binary files, this should be base64-encoded content. For text files, this should be the raw text content.
- file_path
Character string specifying the path where the file should be written. Parent directories will be created if they don't exist.
- type
Character string specifying the file type, either "text" (default) or "binary". Binary files are assumed to be base64 encoded, as this is the standard format for binary content in Shinylive applications.
Details
The function handles two types of content:
Text files (
type = "text"
):Content is converted to UTF-8 encoding using
enc2utf8()
Written using
writeLines()
withuseBytes = TRUE
Binary files (
type = "binary"
):Content is decoded from base64 using
jsonlite::base64_dec()
Written as raw binary data using
writeBin()
Parent directories in the file path are automatically created if they don't
exist using fs::dir_create()
with recurse = TRUE
.
Examples
# Writing a text file
write_file_content(
content = "library(shiny)\n\nui <- fluidPage()",
file_path = "app/app.R",
type = "text"
)
# Write base64 encoded image
b64img <- paste0(
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAA",
"DUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg=="
)
write_file_content(b64img, "test.png", type = "binary")