A package to aid in reproducible research in R
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Brian Albert Monroe 3546ebbabd
local: Remove internal environment for pkgs
1 year ago
R local: Remove internal environment for pkgs 1 year ago
man local: Remove internal environment for pkgs 1 year ago
.Rbuildignore First commit 2 years ago
.gitignore First commit 2 years ago
DESCRIPTION local: Remove internal environment for pkgs 1 year ago
NAMESPACE MVP: This as is goes a long way to making my idea of rep research 2 years ago
README.md RENAME: Change the name of this project from rresearch to repres 2 years ago

README.md

repres

A package to aid in the development of (rep)roducible (res)earch in R

Doing reproducible research in R can be difficult if you want the analyses to be able to be replicated years later. Packages change and can break functions that the research initially relied on. My solution is to install all packages needed for an analysis into a local R library and make a local R repo using these downloaded packages. When a project is "done", both of these directories should be committed and shipped with the project. To reproduce an analysis, a future user will just need the correct version of R, or at least a version of R with backwards-compatible base functions, which rarely change.

A "setup.R" file with the below code is currently used to setup the project. The more code that can be removed from this block and put into the repres package, the better.

# Where are we keeping the R packages, this should be in the same git repo
# as the project in general, and even better, in the code folder
R_library <- "R_library"
local_lib <- file.path(R_library, "lib")
local_src <- file.path(R_library, "src", "contrib")
dir.create(local_lib, showWarnings = FALSE, recursive = TRUE)
dir.create(local_src, showWarnings = FALSE, recursive = TRUE)

# Try and load my reproducible research package. It would be great to not need this.
tryCatch(
  # Maybe it's there and we can load it and we're done
  library(repres, lib.loc = local_lib),
  error = function(e) {
    # Maybe it's been shipped with this code and we can install it locally
    tryCatch({
      local_rres <- file.path("R_library", "src", "contrib", "repres_0.3.0.tar.gz")
      install.packages(pkgs = local_rres, lib = local_lib, repo = NULL)
      library(repres, lib.loc = local_lib)
    },
    error = function(f) {
      # Last try is to get it from my package repo
      local_lib <- file.path("R_library", "lib")
      install.packages("repres", repos = "https://bamonroe.com/drat", lib = local_lib)
      library(repres, lib.loc = local_lib)
    })
  }
)

# The key to reproducing results in R is to make packages local
set_local_lib(R_library)

# Add a repo to fetch packages from initially
add_repo("https://bamonroe.com/drat")

# Fetch the packages, install them into the local library, copy the source
# code into the local repo, and indicate that these are to be loaded.
# "update" should be set to TRUE while working on a project, and set to FALSE when
# the project is complete.
get_packages(c("dplyr",
               "tidyr",
               "ctools",
               "rcmle",
               "rcgen",
               "rcguts",
               "here",
               "ggplot2",
               "MASS",
               "nlWaldTest"),
             load_lib = TRUE,
             update = TRUE)

# Actually load the packages
# Having "strict" set to TRUE only allows packages to be loaded from the local R_library
# created by "repres". This ensures that the code can be run with the given packages.
# Having "strict" set to FALSE allows packages to be loaded from other library trees.
load_packages(strict = TRUE)

# get_packages, but use the loaded packages namespace for pkglist, and
# make the local repo
final_libs()