1

I have a docker container with R 3.6.3 installed. I would need certain packages preloaded and execute a set of R scripts each time a R session is initialized inside the container. Is there a work around for this? I tried keeping a ".Rprofile" (with R package import lines and R scripts) at home directory and expected to have them whenever I start a R session.

COPY /.Rprofile ./home/docker/.Rprofile
COPY /Rprofile.site ./usr/local/lib/R/etc/Rprofile.site
  • 1
    Is it to be used interactively or as a script? – Paul Stafford Allen Jan 17 '23 at 13:01
  • 1
    I would have expected that the `.Rprofile` is the way to go, see e.g. in this answer: https://stackoverflow.com/a/10300829/12647315 Does this work for you? If not, could you provide an MRE of the dockerfile/.Rprofile? – starja Jan 17 '23 at 13:02
  • 2
    You said you tried it: so what was the result of that? It should work. – Konrad Rudolph Jan 17 '23 at 13:20
  • @PaulStaffordAllen: as a script. some lines of codes which I would prefer to be executed each time the R session is activated in the docker container. Usually in windows .Rprofile does the job. – Rakesh Poduval Jan 17 '23 at 15:26
  • @KonradRudolph I tried simple examples - like creating a .Rprofile 'print("hello") ' and puttin it in expected locations inside the docker - /etc/.Rprofile `COPY /.Rprofile ./home/docker/.Rprofile` , `COPY /Rprofile.site ./usr/local/lib/R/etc/Rprofile.site` – Rakesh Poduval Jan 17 '23 at 15:29
  • 1
    Forgive me if I am misunderstanding, but can't you just add those lines to the start of the script that the instance runs when activated? I'm looking over this blog in particular: https://www.statworx.com/en/content-hub/blog/running-your-r-script-in-docker/ – Paul Stafford Allen Jan 17 '23 at 15:57
  • @PaulStaffordAllen: That is the current work around I have at the moment. But I would prefer to have complete control within R. I have around 10-12 lines of code which I need to be executed each time R instance is started – Rakesh Poduval Jan 20 '23 at 14:17

1 Answers1

1

Yes. You can add a line to the Dockerfile to install an R package with a RUN R -e "install.packages()" command. See the 4th line of this Dockerfile.

FROM rocker/shiny-verse
WORKDIR /app
COPY local_app app_in_docker
RUN R -e "install.packages(c('janitor'))"
CMD ["R", "-e", "shiny::runApp('app_in_docker', host = '0.0.0.0', port = 80)"]
Arthur
  • 1,248
  • 8
  • 14
  • Rocker images provide the [`install2.r` utility](https://rocker-project.org/use/extending.html#install2.r) to install pacakges, which makes this code a bit easier. Also: no need to put `c(…)` around a single value. – Konrad Rudolph Jan 17 '23 at 13:22