From 635b784fba87d0c6757ead908a106aa738be3086 Mon Sep 17 00:00:00 2001 From: "Hylke C. Donker" Date: Thu, 17 Feb 2022 11:50:55 +0100 Subject: [PATCH] Added example snippets of the presentation. --- Dockerfile | 19 +++++++++++++++++++ example/example.R | 4 ++++ example/test/test_linear_transformation.R | 12 ++++++++++++ readme.md | 12 ++++++++++++ 4 files changed, 47 insertions(+) create mode 100644 Dockerfile create mode 100644 example/example.R create mode 100644 example/test/test_linear_transformation.R create mode 100644 readme.md diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..75cd9f4 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,19 @@ +FROM r-base:4.1.2 + +# Install several CRAN packages using package manager. +RUN apt update \ + && apt install --assume-yes \ + r-cran-dplyr=1.0.7-2 \ + r-bioc-deseq2=1.34.0+dfsg-1 \ + r-cran-biocmanager=1.30.16+dfsg-3 \ + r-cran-devtools=2.4.3-1 + +# Example how to install specific package using R. +RUN R -e "BiocManager::install('apeglm', dependencies=TRUE, version='3.14')" + +# Make a directory in the Docker image and copy scripts to it. +WORKDIR /analysis/ +COPY ./example/ /analysis/ + +# Run unit tests. +CMD R --quiet -e 'testthat::test_dir("test")' \ No newline at end of file diff --git a/example/example.R b/example/example.R new file mode 100644 index 0000000..51221df --- /dev/null +++ b/example/example.R @@ -0,0 +1,4 @@ +# Example function to be tested. +linear_transformation <- function(x) { + return (2*x + 1) +} \ No newline at end of file diff --git a/example/test/test_linear_transformation.R b/example/test/test_linear_transformation.R new file mode 100644 index 0000000..f6e5348 --- /dev/null +++ b/example/test/test_linear_transformation.R @@ -0,0 +1,12 @@ +source("../example.R", chdir=TRUE) +library(testthat) + +test_that("Real numbers are transformed to odd numbers.", { + expect_equal(linear_transformation(3) %% 2, 1) + expect_equal(linear_transformation(4) %% 2, 1) +}) + +test_that("Positive numbers remain positive.", { + expect_true(linear_transformation(3) > 0) + expect_true(linear_transformation(10) > 0) +}) diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..d79fcbb --- /dev/null +++ b/readme.md @@ -0,0 +1,12 @@ +# Snippets of the `Reproducible and error free scripts` presentation +Use the `Dockerfile` to build the image, which we name `r_example`: + +```Bash +docker build -t r_example . +``` + +As specified in the image's Dockerfile (`CMD` line), by default it will run the unit tests: + +```Bash +docker run r_example +```