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

enhance readme; add params to create_shinylive_url; other minor docs … #2

Open
wants to merge 4 commits into
base: main
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
14 changes: 11 additions & 3 deletions R/parse_url.R
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
#' Creates Shinylive url for the app code.
#' Creates Shinylive url based on app code.
#'
#' @importFrom jsonlite unbox toJSON
#' @importFrom lzstring compressToEncodedURIComponent
#'
#' @param code (`character(1)`) A string with app code.
#' @param mode (`character(1)`) A string with mode. One of "app" or "editor".
#' @param header (`logical(1)`) A logical value indicating whether to include header.
#' Ignored if `mode` is "editor".
#' @return (`character(1)`) Shinylive app url.
#'
#' @export
#'
#' @examples
#' code <- "this is your app code as a string"
#' create_shinylive_url(code)
create_shinylive_url <- function(code) {
create_shinylive_url <- function(code, mode = c("app", "editor"), header = TRUE) {
stopifnot(is.character(code) && length(code) == 1)
mode <- match.arg(mode)
stopifnot(is.logical(header) && length(header) == 1)

# implementation based on "Create ShinyLive Link" feature of Shiny VSCode extension
# https://github.com/posit-dev/shiny-vscode/blob/80560bf36d516ff89dffe88bd9a28cee9edd4d43/src/shinylive.ts#L499
Expand All @@ -23,5 +28,8 @@ create_shinylive_url <- function(code) {
files_json <- jsonlite::toJSON(list(files))
files_lz <- lzstring::compressToEncodedURIComponent(as.character(files_json))
files_lz <- gsub("/", "-", files_lz)
sprintf("https://shinylive.io/r/app/#code=%s", files_lz)

header_param <- ifelse(mode == "app" && isFALSE(header), "h=0&", "")

sprintf("https://shinylive.io/r/%s/#%scode=%s", mode, header_param, files_lz)
}
8 changes: 4 additions & 4 deletions R/tag_examplesShinylive.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#' Custom `examplesShinylive` tag.
#' Custom `@examplesShinylive` tag.
#'
#' This function generates a new "Examples in Shinylive" section in the documentation. This section contains URL to
#' the application in Shinylive as well as an iframe with the application.
#' This function generates a new "Examples in Shinylive" section in the documentation.
#' This section contains URL to the application in Shinylive and for HTML outputs: an iframe with the application.
#' If no code is provided then the code is taken from the following `@examples` or `@examplesIf` tag.
#'
#' The application code must be executable inside Shinylive. If the application code includes functions from your
Expand Down Expand Up @@ -206,7 +206,7 @@ format.rd_section_examplesShinylive <- function(x, ...) {
$(function() {
var if_pkgdown = [...document.scripts].filter(x => x.src.includes(\"pkgdown.js\")).length > 0;
if (if_pkgdown) {
$(\"iframe.iframe_shinylive\").css(\"width\", \"140\\%\");
$(\"iframe.iframe_shinylive\").css(\"width\", \"150\\%\");
}
});"
paste0(
Expand Down
47 changes: 47 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,25 @@ This package provides a `roxygen2` extension that automatically takes the code f
## Install

```r
# install.packages("pak")
pak::pak("insightsengineering/roxy.shinylive")
```

## Usage

### Creating a URL manually

```r
code <- "\"Hello World\""
roxy.shinylive::create_shinylive_url(code, mode = "editor")
```

This returns the following URL:

<https://shinylive.io/r/editor/#code=NobwRAdghgtgpmAXGKAHVA6ASmANGAYwHsIAXOMpMAHTAAk4AbRogAgHUiAnRgE1rABfALpA>

### In the roxygen2 documentation

In your `DESCRIPTION` file, add the following:

```yaml
Expand Down Expand Up @@ -47,4 +61,37 @@ Which would produce a following output in your documentation:
}
```

![example of function](inst/function.gif)

### In the vignettes

In your `DESCRIPTION` file, add the following:

```yaml
Suggests:
roxy.shinylive
```

Then in your vignette:

````Rmd
```{r shinylive_url, echo = FALSE, results = 'asis'}
# extract the code from knitr code chunks by ID
code <- paste0(c(
... # add more IDs if needed
knitr::knit_code$get("app"),
knitr::knit_code$get("shinyapp")
), collapse = "\n")

url <- roxy.shinylive::create_shinylive_url(code)
cat(sprintf("[Open in Shinylive](%s)\n\n", url))
```

```{r shinylive_iframe, echo = FALSE, out.width = '150%', out.extra = 'style = "position: relative; z-index:1"', eval = knitr::is_html_output() && identical(Sys.getenv("IN_PKGDOWN"), "true")}
knitr::include_url(url, height = "800px")
```
````

![example of vignette](inst/vignette.gif)

See the package documentation for more details.
Binary file added inst/function.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inst/vignette.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 8 additions & 3 deletions man/create_shinylive_url.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions man/tag-examplesShinylive.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading