All CollectionsAuthorsAuthoring techniques
Using R for variable definitions
Using R for variable definitions

Learn how to define variables using the statistical programming language R

Updated over a week ago

In this article, you learned how to create and edit variables. It was mentioned that variables are written in PHP, but this programming language doesn’t suffice for statistical operations. Instead, we want to use the statistical programming language R. Documentation on this programming language can be found online.

To explain how defining variables using R works, we start with an easy example. Say we want to make an exercise where students have to add two random integers together. Using only PHP, the Variables tab of this exercise would look something like this:

### Using sw_R() for variable definitions

If, instead, we want to use R, we need to follow some steps. First, we create a variable that will be an R code block. For the example where we add two integers, this can look as follows:

The function `sw_R` evaluates its argument as R code. We use the three print statements at the bottom of the R code to make our variables show up in the Preview array. Technically, the function returns `stdout` of the Rscript interpretation of the input, but what is important for authors to know is that this function will output any values (numerical or otherwise) that are ‘printed to the console’ in the R code using the print() function.

After writing our R code block, we need to export the elements from the output array of `\$code` to SOWISO variables that we can use in the exercise. To do this, we make new variables that have Definition `\$code[i]` for all i in the array, as in the image below:

The variables you just created can be used in, for instance, the “Texts”, “Positive feedback”, “Negative feedback” and “Solution” tabs of the exercise.

### Using an IDE

Although the use of an Integrated Development Environment (IDE) is not strictly

necessary to make use of `sw_R()`, it is highly recommended. Especially as your code becomes longer and/or more complicated, things such as syntax highlighting, access to the R manual, and being able to execute your code line by line will become valuable tools. Once you're satisfied with the code written in the IDE, it can be copied to the SOWISO platform to be executed within an `sw_R()` block. Don’t forget to put the R code between single quotation marks.

An alternative to copy-pasting your R code into the SOWISO platform after writing it in an IDE is to instead save the relevant code as an R script with the .R file extension. This becomes increasingly useful as the relevant code becomes longer and/or more complex, as the SOWISO platform is generally not a great place to write, edit, and test such code. Once your code is ready to be used in the exercise, complete the following steps:

• Save the code as an .R script.

• Upload the code to the relevant exercise (bottom of the General tab).

• Copy the url address displayed below the file.
e.g. `‘/images/uploads/exercises/12345/Script.R’`

• Load and execute the .R script within an `sw_R()` block by using the `source()` function. Don’t forget to use the complete URL when referencing the file (including ‘https://cloud.sowiso.nl’)
e.g. `source("https://cloud.sowiso.nl/images/uploads/exercises/12345/Script.R")`

The procedure above can also be used to load an .R script containing (custom)

functions.

### Packages

R packages are extensions to the R statistical programming language and contain code, data, and documentation in a standardized format. The following packages are available for use within an `sw_R() `block:

`askpass, assertthat, aws.s3, aws.signature, backports, base, base64enc, BH, boot, callr, class, cli, cluster, codetools, colorspace, compiler, crayon, curl, datasets, desc, digest, dplyr, ellipsis, evaluate, fansi, farver, foreign, gapminder, ggplot2, glue, graphics, grDevices, grid, gtable, httr, isoband, jsonlite, KernSmooth, labeling, lattice, lifecycle, logging, magrittr, MASS, Matrix, methods, mgcv, mime, munsell, mvtnorm, NCmisc, nlme, nnet, openssl, parallel, pillar, pkgbuild, pkgconfig, pkgload, plogr, praise, prettyunits, processx, proftools, ps, purrr, R6, RColorBrewer, Rcpp, reader, rlang, rpart, rprojroot, rstudioapi, scales, spatial, splines, stats, stats4, sys, tcltk, testthat, tibble, tidyselect, tools, utf8, utils, vctrs, viridisLite, withr, xml2.`

Available packages still need to be loaded with the help of the `library()` function.

It is in general not possible to install more packages, but there is the possibility to strip functions that we need from non-installed packages, as explained in the next section. If there is a package that you would really like to use, that is not installed, and that is useful for other users as well, you can request this package by sending an email to support@sowiso.com.

### Stripping functions from packages

To strip a function from a package that is not installed yet, complete the following steps:

• Open an IDE that supports the R programming language.

• Install the relevant package with the `install.packages()` function.
e.g. `install.packages('car')`

• Load the package with the `library() `function.
e.g. `library(car)`

• Use the `methods()` function to return a list of the methods (subcomponents) that make up the function.

• e.g. `methods(leveneTest)`

• Use the `getAnywhere()` function to display a method’s source code in the IDE’s console.
e.g. `getAnywhere(leveneTest.default)`

• Use the source code to create your own function definition.

e.g. `sw_leveneTest <- function (y, group, center = median, ...){}`

This process might need to be repeated a couple of times, depending on the complexity and interdependence of the function you wish to strip.

### Exporting plots and datasets

For a description of how to export plots and/or datasets (such as .csv or .Rdata files) see the relevant section of the authoring manual. E.g. it is possible, for instance for data analysis exercises, to upload a large dataset to an exercise, generate a random smaller sample of that dataset and offer it to the student with a download link. The student can then perform operations on that random sample in order to solve the exercise. If you want to know more about how this works, send an email to support@sowiso.com.