Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use lzstring R package for encode/decode Shinylive URL functions #92

Open
wants to merge 2 commits into
base: feat/encode-decode-url
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Suggests:
httpuv (>= 1.6.12),
spelling,
testthat (>= 3.0.0),
V8
lzstring
Config/Needs/website: tidyverse/tidytemplate
Config/testthat/edition: 3
Language: en-US
42 changes: 0 additions & 42 deletions R/lzstring.R

This file was deleted.

48 changes: 39 additions & 9 deletions R/url.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
url_encode_dir <- function(
dir,
language = c("auto", "r", "py"),
mode = c("editor", "app"),
hide_header = FALSE,
exclude = "rsconnect/"
dir,
language = c("auto", "r", "py"),
mode = c("editor", "app"),
hide_header = FALSE,
exclude = "rsconnect/"
) {
check_v8_installed()

stopifnot(fs::dir_exists(dir))

Expand Down Expand Up @@ -46,19 +45,50 @@ url_encode_dir <- function(
}

names <- fs::path_rel(files, path_root)

bundle <- unname(Map(as_file_list, files, names))

bundle <- jsonlite::toJSON(bundle, auto_unbox = TRUE, null = "null", na = "null")
URI <- lzstring::compressToEncodedURIComponent(bundle)
URI <- gsub("/", "-", URI)
sprintf(
"https://%s/%s/%s/#%scode=%s",
getOption("shinylive.host", "shinylive.io"),
language,
mode,
if (hide_header) "h=0&" else "",
lzstring_compress_uri(bundle)
URI
)
}

url_decode <- function(encoded_url, dir = NULL, json = FALSE) {
url_in <- strsplit(encoded_url, "code=")[[1]][2]
sl_app <- lzstring::decompressFromEncodedURIComponent(url_in)
sl_app <- jsonlite::fromJSON(sl_app, simplifyVector = FALSE, simplifyDataFrame = FALSE, simplifyMatrix = FALSE)
if (json) {
sl_app <- jsonlite::toJSON(sl_app)
return(sl_app)
}
if (!is.null(dir)) {
write_files(sl_app, dir)
} else {
print(sl_app)
}
}

write_files <- function(sl_app, dest) {
if (!fs::dir_exists(dest)) {
fs::dir_create(dest)
}
for (file in sl_app) {
if ("type" %in% names(file) && file[["type"]] == "binary") {
file_content <- base64enc::base64decode(file[["content"]])
writeBin(file_content, file.path(dest, file[["name"]]))
} else {
file_content <- iconv(file[["content"]], "UTF-8", "UTF-8", sub = "")
writeLines(file_content, file.path(dest, file[["name"]]), sep = "")
}
}
return(dest)
}

as_file_list <- function(path, name = fs::path_file(path), type = NULL) {
if (is.null(type)) {
Expand Down
1 change: 0 additions & 1 deletion inst/lz-string/lz-string.min.js

This file was deleted.

20 changes: 0 additions & 20 deletions package-lock.json

This file was deleted.

5 changes: 0 additions & 5 deletions package.json

This file was deleted.