Last active
April 18, 2024 08:42
-
-
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 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
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/