14  R code

14.1 File formats

  • As per the study definition best practice, outputs from the cohortextractor should be provided as .rds files

  • Use the readr package to read and write .rds files

    object <- readr::read_rds(file.path("output", "mydata.rds"))
    readr::write_rds(object, 
                     file.path("output", "mydata.rds"),
                     compress = "gz")
  • All .rds files should all be classified as highly_sensitive in actions, as they cannot be opened on the results server and therefore cannot be reviewed or released.
  • readr::write_rds() does not compress files by default. It is recommended that you compress .rds files using the argument compress = "gz".
  • Use file.path() to construct file paths that are reproducible across operating systems.

14.2 Supported packages

  • The R packages (and their version number) which are installed in the OpenSAFELY R Docker container are listed here
  • To request a new package, which should be on CRAN, open a new issue here, including the link to its CRAN webpage

14.3 RStudio project

  • Define your repo as an RStudio project in RStudio as follows
    • File | New Project…

    • Existing Directory

    • Then find your repo

    • This will create a .Rproj file at the top level of your repo

    • Commit the file into your repo

    • Now you can double click the .Rproj file to open the repo in RStudio, so you can work on your R scripts more conveniently