Joachim Schork
Joachim Schork

Reputation: 2147

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 (i.e. "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?

Upvotes: 0

Views: 458

Answers (1)

ismirsehregal
ismirsehregal

Reputation: 33442

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 and a repository should be provided to install.packages' repos parameter as the R session isn't interactive().

Upvotes: 2

Related Questions