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() | |
}) | |
} | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
never mind, I had to wrap my
requireNamespace
in ado.call
to evade dependency detection. So it still works!