Skip to content

Instantly share code, notes, and snippets.

@wch
Last active April 18, 2024 08:42
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save wch/c3653fb39a00c63b33cf to your computer and use it in GitHub Desktop.
Save wch/c3653fb39a00c63b33cf to your computer and use it in GitHub Desktop.
Example Shiny app that automatically installs a source package when deployed to shinyapps.io
# By default, the directories in .libPaths() aren't writable on shinyapps.io, so
# create a subdir where we'll install our package.
if (!file.exists("R-lib")) {
dir.create("R-lib")
}
# Unfortunately, there's no way to get deployapp() to ignore this directory, so
# make sure to remove it locally before you call deployapp(). This can be done
# with:
# unlink("pkgInst/R-lib", recursive = TRUE)
# You may also need to restart R before calling deployapp(), because calling
# runApp() will modify your libpath (below), which can confuse deployapp().
# Add ./R-lib/ to the libPaths
.libPaths( c(normalizePath("R-lib/"), .libPaths()) )
# Install the package if needed.
if (!do.call(require, list("myPackage"))) {
install.packages("myPackage_0.1.tar.gz", repos = NULL, type = "source")
}
# Instead of `library(myPackage)`, we'll use do.call, to evade deployapp's
# checks for packages installed locally from source.
do.call(library, list("myPackage"))
shinyApp(
ui = fluidPage(
p(
"The output of the function is: ",
verbatimTextOutput("text")
)
),
server = function(input, output) {
output$text <- renderPrint({
# This function is in myPackage
myFunc()
})
}
)
@wch
Copy link
Author

wch commented Nov 10, 2014

This is written as a single-file app, but it could also be written as a multi-file app with server.R and ui.R, and with the installation code in global.R.

myPackage_01.tar.gz is an R source package.

Live example at: https://winston.shinyapps.io/pkgInst/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment