Skip to content

Commit

Permalink
Merge pull request #4 from Oefenweb/add-functionality-for-installing-…
Browse files Browse the repository at this point in the history
…and-removing-r-packages

Add functionality for installing and removing R packages
  • Loading branch information
tersmitten committed Sep 2, 2015
2 parents 25a3acf + 1964464 commit 684ff0c
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 4 deletions.
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,22 @@ Set up the latest version of R in Ubuntu systems.

#### Requirements

None
* `littler` (will be installed)

#### Variables

* `r_cran mirror`: [default: `http://cran.rstudio.com/`]: Your favorite [CRAN mirror](http://cran.r-project.org/mirrors.html)
* `r_install_dev`: [default: `false`]: Whether or not install the `r-base-dev` package
* `r_install`: [default: `[]`]: Additional packages to install (e.g. `r-recommended`, `littler`)
* `r_install`: [default: `[]`]: Additional packages to install (e.g. `r-recommended`)

* `r_packages_lib`: [default: `/usr/local/lib/R/site-library`]: The (default) library directory to install packages to
* `r_packages_repos`: [default: `"{{ r_cran_mirror }}"`]: The (default) URL to install packages from

* `r_packages`: [default: `[]`]: Packages to install or remove
* `r_packages.{n}.name`: [required]: The name of the package
* `r_packages.{n}.state`: [optional, default: `present`]: The state of the package
* `r_packages.{n}.lib`: [optional, default: `r_packages_lib`]: The library directory to install the package to
* `r_packages.{n}.repos`: [optional, default: `r_packages_repos`]: The URL to install the package from

## Dependencies

Expand Down
4 changes: 4 additions & 0 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@
r_cran_mirror: http://cran.rstudio.com/
r_install_dev: false
r_install: []

r_packages_repos: "{{ r_cran_mirror }}"
r_packages_lib: /usr/local/lib/R/site-library
r_packages: []
6 changes: 6 additions & 0 deletions tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,9 @@
- configuration
- r
- r-install

- include: packages.yml
tags:
- configuration
- r
- r-packages
32 changes: 32 additions & 0 deletions tasks/packages.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# tasks file for r
---
- name: packages | copy littler scripts
template:
src: "usr/local/bin/{{ item.src }}"
dest: "/usr/local/bin/{{ item.dest }}"
owner: root
group: root
mode: 0755
with_items:
- src: R-install-package.j2
dest: R-install-package
- src: R-remove-package.j2
dest: R-remove-package

- name: packages | install
command: "R-install-package {{ item.name }}{% if item.lib is defined %} {{ item.lib }}{% endif %}{% if item.repos is defined %} {{ item.repos }}{% endif %}"
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
when: item.state is undefined or item.state == 'present'
tags:
- r-packages-install

- name: packages | remove
command: "R-remove-package {{ item.name }}{% if item.lib is defined %} {{ item.lib }}{% endif %}"
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
when: item.state is defined and item.state == 'absent'
tags:
- r-packages-remove
Empty file removed templates/empty
Empty file.
17 changes: 17 additions & 0 deletions templates/usr/local/bin/R-install-package.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env r

if (is.null(argv) | length(argv) < 1) {
cat("Usage: R-install-package package [lib] [repos]\n");
q(status = 1);
}

package = argv[1]
lib = ifelse(is.na(argv[2]), '{{ r_packages_lib }}', argv[2]);
repos = ifelse(is.na(argv[3]), '{{ r_packages_repos }}', argv[3]);

if (!(package %in% installed.packages(lib.loc = lib)[,'Package'])) {
withCallingHandlers(install.packages(package, lib, repos), warning = stop);
cat("changed\n");
} else {
cat("unchanged\n");
}
16 changes: 16 additions & 0 deletions templates/usr/local/bin/R-remove-package.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env r

if (is.null(argv) | length(argv) < 1) {
cat("Usage: R-remove-package package [lib]\n");
q(status = 1);
}

package = argv[1]
lib = ifelse(is.na(argv[2]), '{{ r_packages_lib }}', argv[2]);

if (package %in% installed.packages(lib.loc = lib)[,'Package']) {
withCallingHandlers(remove.packages(package, lib), warning = stop);
cat("changed\n");
} else {
cat("unchanged\n");
}
14 changes: 12 additions & 2 deletions tests/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,15 @@
roles:
- ../../
vars:
r_install:
- littler
r_packages:
- name: DBI
state: present

- name: DBI
repos: 'http://cran-mirror.cs.uu.nl/'
lib: /tmp

- name: colorspace

- name: foreign
state: absent
1 change: 1 addition & 0 deletions vars/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ r_repository:
url: "{{ r_cran_mirror }}/bin/linux/ubuntu {{ ansible_distribution_release }}/"
r_dependencies:
- r-base
- littler
- "{{ 'r-base-dev' if r_install_dev else '' }}"

0 comments on commit 684ff0c

Please sign in to comment.