简体   繁体   中英

Automatically Install Required Packages After Deploying Shiny App on shinyapps.io

I've deployed a Shiny app on shinyapps.io that executes code that is inserted within the app. For example, if you insert the following code within the app:

1 + 2

The Shiny app returns:

3

This works fine as long as all the packages used in the inserted code have been specified already during the deployment process of the app.

However, if an unknown package is used within the inserted code, the Shiny app doesn't work anymore. For example, the following input returns an error message:

install.packages("Hmisc")
1 + 2

Output:

  'lib = ".../lib/R/library"' is not writable
Warning in install.packages("Hmisc") :
Warning: Error in install.packages: unable to install packages

This could be solved by specifying all required packages (ie "Hmisc") during the deployment of the app. However, since I don't know all the required packages before the deployment of the app, I need to find a way to install and load packages AFTER the deployment. How could I do that?

As per my above comment: we need to add a writable libPath on app or session start.

This can be done by placing the following line of code in the global (app start) or server part (session start) of the app:

.libPaths(c(tempdir(), .libPaths()))

PS: tempdir() can be replaced with any other writable directory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM