#' Create Shiny App Shortcut
#'
#' Creates an executable file that, when ran, will open the co-located shiny
#' app using the user's default internet browser.
#'
#' @param shinyDirectory The top-level directory containing the code and
#' materials for the shiny application. Also where the
#' final shortcut file will be saved.
#' @param OS The operating system that R is being ran on. Must be one of
#' \code{"windows"} or \code{"unix"}.
#' @param gitIgnore If True then all produced files will be added to a
#' \code{.gitignore} file in the specified directory (if there is no
#' such file it will be created).
#'
#' @details Calling \code{shinyShortcut} will write an executable file
#' that will run the shiny app in the user's default browser.
#' This executable is stored in a created folder named .shiny_run'
#' whilst a shortcut that will silently run the executable
#' is saved in the home directory as 'shinyShortcut' (this will
#' be a .vbs script on windows or a .desktop file on unix systems).
#' These files can then be linked to from other locations such as the
#' user's desktop.
#'
#' @examples
#' shinyShortcut()
#' @export
shinyShortcut <- function(shinyDirectory = getwd(), OS = .Platform$OS.type,
gitIgnore = FALSE) {
# if relevant files exist delete them first
unlink(file.path(shinyDirectory, ".shiny_run"),
recursive = TRUE, force = TRUE)
unlink(file.path(shinyDirectory, "shiny_run.desktop"),
recursive = FALSE, force = TRUE)
unlink(file.path(shinyDirectory, "shinyShortcut.vbs"),
recursive = FALSE, force = TRUE)
unlink(file.path(shinyDirectory, "shinyShortcut.cmd"),
recursive = FALSE, force = TRUE)
dir.create(file.path(shinyDirectory, ".shiny_run"))
if (OS == "windows") {
# write batch file to .shiny_run
rscriptForwardDash <-
file.path(R.home(), "bin", "Rscript.exe")
rscript <- gsub("/", "\\\\", rscriptForwardDash)
shinyCommand <- paste0(
"shiny::runApp(\'", shinyDirectory, "\',",
" launch.browser = TRUE)"
)
batchCode <- paste0("\"", rscript, "\"", " -e ",
"\"", shinyCommand, "\"")
batchReference <- file.path(shinyDirectory,
".shiny_run",
"shinyShortcut.cmd")
write(batchCode, batchReference)
message("* Writing .shiny_run/shinyShortcut.cmd")
# write vbs file to home directory
batchReferenceBackDash <-
gsub("/", "\\\\", batchReference)
vbsCode <- paste0(
"Set objShell = WScript.CreateObject(\"WScript.Shell\")",
"\n",
"objShell.Run(\"",
batchReferenceBackDash,
"\"), 0, True"
)
vbsReference <- file.path(shinyDirectory,
"shinyShortcut.vbs")
write(vbsCode, vbsReference)
message("* Writing shinyShortcut.vbs")
} else if (OS == "unix"){
# write bash file to .shiny_run
rscript <-
paste("#!", file.path(R.home(), "bin", "Rscript"))
shinyCommand <- paste0(
"shiny::runApp('", shinyDirectory, "',",
" launch.browser = TRUE)"
)
bashCode <- paste0(rscript, "\n\n",
shinyCommand)
bashReference <- file.path(shinyDirectory,
".shiny_run",
"shinyShortcut.r")
write(bashCode, bashReference)
message("* Writing .shiny_run/shinyShortcut.r")
# make executable
system(paste0("chmod +x ", bashReference))
# add shortuct in home directory
shortcut_code <- paste0(
"[Desktop Entry]\n",
"Name=shinyShortcut\n",
"Comment=Run Shiny App\n",
"Exec=", bashReference, "\n",
#"Icon=/home/user/youricon.gif\n",
"Terminal=false\n",
"Type=Application")
shortcut_reference <- file.path(shinyDirectory,
"shinyShortcut.desktop")
write(shortcut_code, shortcut_reference)
message("* Writing shinyShortcut.desktop")
# make executable
system(paste0("chmod +x ", shortcut_reference))
} else stop("OS must be one of \"windows\" or \"unix\"")
# if specified, add files and folder to local .gitignore file
if (gitIgnore){
cat(c("\n.shiny_run/", "\nshinyShortcut"), sep = "",
file = file.path(shinyDirectory, ".gitignore"),
append = TRUE)
message("* Adding `.shiny_run/` and `shinyShortcut` to .gitignore")
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.