Last active
December 2, 2024 16:14
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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() | |
}) | |
} | |
) |
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
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
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/