Reputation: 17418
I have a very simple script:
library(datasets)
library(caret)
data(iris)
index <- createDataPartition(iris$Species, p=0.80, list=FALSE)
testset <- iris[-index,]
trainset <- iris[index,]
model = train(Species ~ .,
data=trainset,
method="rpart",
trControl = trainControl(method = "cv"))
As you can probably tell, the script fits an rpart classification model to the iris dataset. So I know that my docker image needs at least caret (or maybe just rpart as docker is used to score the model?). I think R also loads some other packages based alone on the caret statement. So to try to identify which packages I required for my docker image, I thought I do:
x <- .packages(TRUE)
x
Unfortunately, this gives me a (too?) long list of packages but maybe they are all required to run this script (I understand caret builds on top of many packages). What is best practice to obtain the definite list of required packages please?
PS:
BTw is datasets part of R base?
Upvotes: 0
Views: 162
Reputation: 851
My suggestion would be to create a new R Project and use the renv
package for dependency management. See the documentation on how to set it up. You can even select the option to "Use renv with this project" when creating a new project from within RStudio.
Before building your Docker image, run renv::snapshot()
one last time, which saves the required dependencies to your renv.lock
file.
Then, in your Dockerfile, install renv
, copy the renv.lock
file into the container, and renv::restore()
the dependencies when building the image. Since renv
is intended for interactive use, you have to set some special arguments for unsupervised installation -- take this as boilerplate starting point:
# Copy renv.lock file
COPY ./renv.lock /renv.lock
# Install R packages
RUN R -e "install.packages('renv')"
RUN R -e "renv::consent(provided = TRUE)"
RUN R -e "renv::restore(prompt = FALSE)"
You can also check out the dedicated documentation on using renv with Docker.
This should install all necessary dependencies needed by your script.
Upvotes: 2