Decode Shinylive link(s) to extract files to local directory
Source:R/decode-shinylive-link.R
decode_shinylive_link.RdDecodes Shinylive sharelinks to extract the embedded files and save them to a local directory.
Arguments
- url
Character string or vector containing Shinylive URL(s)
- output_dir
Character string specifying the output directory path. Defaults to a
shinylive_filesdirectory inside the session temporary directory. Pass an explicit path to extract somewhere permanent.- overwrite
Logical. Whether to overwrite existing files. Defaults to
FALSE.- create_subdir
Logical. If
TRUE(default), each decoded link is extracted into its own subdirectory underoutput_dirrather than directly into it. For a single URL the subdirectory is namedshinylive_<hash>, where<hash>is a short fingerprint of the URL. For multiple URLs, seename_dirs. SetFALSEto extract straight intooutput_dir. With multiple URLs that means identically named files (such asapp.R) collide, so a later app overwrites an earlier one whenoverwrite = TRUE, or is skipped whenoverwrite = FALSE.- name_dirs
Logical. For multiple URLs, this controls how the per-link subdirectories are named.
TRUE(default) numbers themapp_01,app_02, and so on.FALSEnames each oneshinylive_<hash>from the URL fingerprint. Ignored for a single URL, and ignored whencreate_subdir = FALSE.- binary
Logical. Whether to write binary files held in the link (default:
FALSE). A preview can show you text but not bytes, so a binary is left alone unless you ask for it.
Value
The decoded contents, in one of two shapes.
For a single URL, a shinylive_decoded object, which is a list with these
entries.
files_info, a data frame with one row per file written, with the columnsfilename,path,type, andsize_bytes.output_dir, the directory the files were written to.url, the link that was decoded.engine,"r"or"python", read off the link.mode,"editor"when the link opens the editor,"app"when it opens the running app on its own.total_files, how many files were written.total_size, the size of those files added up, in bytes.
For several URLs, a shinylive_decoded_batch object, which is a list with
these entries.
results, a named list of theshinylive_decodedobjects described above, one per link that decoded, each named after the subdirectory it went into.base_dir, the directory those per-link subdirectories sit in.urls, the links you passed in.total_urls, how many links you passed in.successful_urls, how many of them decoded.total_files, how many files were written across all of them.total_size, the size of those files added up, in bytes.
Details
This is the reverse operation of creating Shinylive links. Handles both single URLs and multiple URLs automatically.
See also
preview_shinylive_link() to inspect a link without writing files.
shinylive_r_link() and shinylive_py_link() to create links.
Examples
# Round-trip: build a link, then decode it back to files
url <- as.character(shinylive_r_link("library(shiny)"))
result <- decode_shinylive_link(url)
#> Decompressing Shinylive data...
#> Parsing file data...
#> Created directory: /tmp/RtmpjOtcTs/shinylive_files/shinylive_e2010f1a
#> Decoding 1 file...
#> app.R (text, 14 bytes)
#> ✔ Successfully decoded 1 file to
#> /tmp/RtmpjOtcTs/shinylive_files/shinylive_e2010f1a
print(result)
#>
#> ── Shinylive R Decoded Files ──
#>
#> Source:
#> <https://shinylive.io/r/editor/#code=NobwRAdghgtgpmAXGKAHVA6ASmANGAYwHsIAXOMpMAGwEsAjAJykYE8AKAZwAtaJWAlHjClWqBMnIAPUmAC+AXSA>
#> Output: /tmp/RtmpjOtcTs/shinylive_files/shinylive_e2010f1a
#>
#> Files (1):
#> app.R (14 bytes)
#>
#> Total size: 14 bytes
#> Mode: "editor"
# Extract to a directory of your choosing
out <- file.path(tempdir(), "my_app")
decode_shinylive_link(url, output_dir = out, create_subdir = FALSE, overwrite = TRUE)
#> Decompressing Shinylive data...
#> Parsing file data...
#> Created directory: /tmp/RtmpjOtcTs/my_app
#> Decoding 1 file...
#> app.R (text, 14 bytes)
#> ✔ Successfully decoded 1 file to /tmp/RtmpjOtcTs/my_app
#>
#> ── Shinylive R Decoded Files ──
#>
#> Source:
#> <https://shinylive.io/r/editor/#code=NobwRAdghgtgpmAXGKAHVA6ASmANGAYwHsIAXOMpMAGwEsAjAJykYE8AKAZwAtaJWAlHjClWqBMnIAPUmAC+AXSA>
#> Output: /tmp/RtmpjOtcTs/my_app
#>
#> Files (1):
#> app.R (14 bytes)
#>
#> Total size: 14 bytes
#> Mode: "editor"
list.files(out)
#> [1] "app.R"
# Both engines at once
urls <- c(url, as.character(shinylive_py_link("from shiny import App")))
decode_shinylive_link(urls, output_dir = file.path(tempdir(), "my_apps"))
#> Processing 2 Shinylive URLs...
#>
#>
#> ── Processing URL 1/2: app_01
#> Decompressing Shinylive data...
#> Parsing file data...
#> Created directory: /tmp/RtmpjOtcTs/my_apps/app_01
#> Decoding 1 file...
#> app.R (text, 14 bytes)
#> ✔ Successfully decoded 1 file to /tmp/RtmpjOtcTs/my_apps/app_01
#>
#>
#> ── Processing URL 2/2: app_02
#> Decompressing Shinylive data...
#> Parsing file data...
#> Created directory: /tmp/RtmpjOtcTs/my_apps/app_02
#> Decoding 1 file...
#> app.py (text, 21 bytes)
#> ✔ Successfully decoded 1 file to /tmp/RtmpjOtcTs/my_apps/app_02
#>
#> ✔ Successfully processed 2/2 URLs
#>
#> ── Shinylive Decoded Batch ──
#>
#> Base directory: /tmp/RtmpjOtcTs/my_apps
#> Total URLs: 2
#>
#> Successfully processed 2 URLs:
#> /tmp/RtmpjOtcTs/my_apps/app_01 (1 file)
#> /tmp/RtmpjOtcTs/my_apps/app_02 (1 file)
#>
#> Total files: 2
#> Total size: 35 bytes