Skip to content

Instantly share code, notes, and snippets.

@wch
Last active December 2, 2024 16:14
Show Gist options
  • 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()
})
}
)
@pepijn-devries
Copy link

Hi there,

Thanks for this gist! But does this approach also work at present? Nowadays R dependencies are captured with renv, when deploying an app. I don't seem to be able to reproduce the strategy of including a tarball with my app as suggested above...

Cheers

@pepijn-devries
Copy link

never mind, I had to wrap my requireNamespace in a do.call to evade dependency detection. So it still works!

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