简体   繁体   中英

determine required packages for docker image in R

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?

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.

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