Oefenweb / ansible-r

Ansible role to set up (the latest version of) R in Ubuntu systems
MIT License
42 stars 27 forks source link

Multiple R installs #22

Closed lucascj closed 6 years ago

lucascj commented 6 years ago

Is it possible to support multiple R installations?

For example, say you have the following on your machine and want to choose which one you install packages to. /opt/R/3.1.0 /opt/R/3.2.0

tersmitten commented 6 years ago

No, as this uses regular APT repositories. If you want to install multiple versions, you'll need to compile R from source

lucascj commented 6 years ago

I meant just to manage packages. Perhaps an option to point to the desired Rscript? For example /opt/R/3.4.3/bin/Rscript vs default (/usr/bin/Rscript).

tersmitten commented 6 years ago

I think there's no need to to adjust R-install-package as it uses #!/usr/bin/env Rscript as a shebang. It might be needed to pass some environment variables to the task (e.g. PATH) to make sure that env Rscript resolved correctly.

Can you do some investigation @lucascj?

brooklynbagel commented 6 years ago

Considering you can specify the library path to install to with r_packages_lib it might be possible to install packages for multiple version of R. I'm not sure about the compatibility of R packages built under a different version than the one it's running on though.

lucascj commented 6 years ago

I think a combination of the proposed solutions definitely makes the most sense. Use the r_packages_lib variable to set the correct install path and also call on the correct Rscript so that it builds with the correct version.

I'm having a bit of trouble passing the correct environment variable so that it calls the correct Rscript. Any recommendations on how to do this within a playbook? It always seems to call the default install at /usr/bin/Rscript.

tersmitten commented 6 years ago

Can you give this a try?

diff --git a/defaults/main.yml b/defaults/main.yml
index 884b74a..8bbbf86 100644
--- a/defaults/main.yml
+++ b/defaults/main.yml
@@ -6,6 +6,7 @@ r_bioclite_url: https://bioconductor.org/biocLite.R
 r_install_dev: false
 r_install:
   - littler
+r_environment: {}

 r_packages_type: cran
 r_packages_repos: "{{ r_cran_mirror }}"
diff --git a/tasks/packages.yml b/tasks/packages.yml
index 4601c0f..3056ed0 100644
--- a/tasks/packages.yml
+++ b/tasks/packages.yml
@@ -28,6 +28,7 @@
       {{ item.type | default(r_packages_type) }}
       {{ item.lib | default(r_packages_lib) }}
       {% if item.repos is defined %}{{ item.repos }}{% endif %}
+  environment: "{{ r_environment }}"
   register: _r_install_package
   changed_when: "_r_install_package.stdout_lines[-1] is defined and _r_install_package.stdout_lines[-1] == 'changed'"
   with_items: "{{ r_packages_from_github | ternary(r_preset_package_remotes + r_packages, r_packages) }}"
@@ -40,6 +41,7 @@
     R-remove-package
       {{ item.name }}
       {% if item.lib is defined %}{{ item.lib }}{% endif %}
+  environment: "{{ r_environment }}"
   register: _r_remove_package
   changed_when: "_r_remove_package.stdout_lines[-1] is defined and _r_remove_package.stdout_lines[-1] == 'changed'"
   with_items: "{{ r_packages }}"
@@ -52,6 +54,7 @@
     R-update-package
       {{ item.name }}
       {% if item.lib is defined %}{{ item.lib }}{% endif %}
+  environment: "{{ r_environment }}"
   register: _r_update_package
   changed_when: "_r_update_package.stdout_lines[-1] is defined and _r_update_package.stdout_lines[-1] == 'changed'"
   with_items: "{{ r_packages }}"
diff --git a/tests/vagrant.yml b/tests/vagrant.yml
index e16d982..b0a86b1 100644
--- a/tests/vagrant.yml
+++ b/tests/vagrant.yml
@@ -5,3 +5,6 @@
   become: true
   roles:
     - ../../
+  vars:
+    r_environment:
+      PATH: "/opt/R/3.4.3/bin:{{ ansible_env.PATH }}"
lucascj commented 6 years ago

Sorry I was away on vacation over the holidays.

This works! I also replaced your install task with my own that downloads a specified version and installs from source to a specific location. Now I have a full role to install and manage multiple local R installations.

Thanks for your help.