Skip to main content
  • Home
  • Development
  • Documentation
  • Donate
  • Operational login
  • Browse the archive

swh logo
SoftwareHeritage
Software
Heritage
Archive
Features
  • Search

  • Downloads

  • Save code now

  • Add forge now

  • Help

  • 5e6d569
  • /
  • reproduceInR.R
Raw File Download

To reference or cite the objects present in the Software Heritage archive, permalinks based on SoftWare Hash IDentifiers (SWHIDs) must be used.
Select below a type of object currently browsed in order to display its associated SWHID and permalink.

  • content
  • directory
content badge
swh:1:cnt:a101ef71d7298b6485396c6c6870543a85b06e49
directory badge
swh:1:dir:5e6d569d111f61e263d07affc073d497fce06720

This interface enables to generate software citations, provided that the root directory of browsed objects contains a citation.cff or codemeta.json file.
Select below a type of object currently browsed in order to generate citations for them.

  • content
  • directory
(requires biblatex-software package)
Generating citation ...
(requires biblatex-software package)
Generating citation ...
reproduceInR.R
rootFn <- c('loadGEO', 'loadPreloaded', 'createES')
complexArgumentLimitBytes <- 400

#' Reproduce session in R code
#'
#' @param sessionName String, OCPU session name
#' @param leaf Boolean, is it leaf (default = F)
#' @param step Integer, step of recursion (default = 0)
#' @param savedEnv Environment, where to store complex arguments (default = new.env())
#'
#' @return JSON with R code
#' @importFrom utils object.size
#' @examples
#' \dontrun{
#'   setwd(tempdir())
#'   reproduceInR('x039f1672026678');
#' }
reproduceInR <- function (sessionName, leaf = T, step = 0, savedEnv = new.env()) {
    ocpuRoot <- strsplit(getwd(), 'ocpu-temp')[[1]][1]
    sessionPath <- paste(ocpuRoot, 'ocpu-store', sessionName, sep=.Platform$file.sep)
    RDataPath <- paste(sessionPath, '.RData', sep=.Platform$file.sep)
    REvalPath <- paste(sessionPath, '.REval', sep=.Platform$file.sep)

    rds <- readRDS(REvalPath)
    env <- new.env()
    loadedVariables <- load(RDataPath, envir = env)
    parsed <- parse(text=rds[[1]]$src)
    fn <- parsed[[1]][[1]]
    complexArguments <- "";
    for(i in 2:length(parsed[[1]])) {
        argumentName <- toString(parsed[[1]][[i]])
        if (argumentName %in% loadedVariables) {
            argumentValue <- env[[argumentName]]
            if (object.size(argumentValue) > complexArgumentLimitBytes) {
                rawArgument <- paste(argumentName, '_', step, sep='')
                savedEnv[[rawArgument]] <- argumentValue
            } else {
                rawArgument <- paste(deparse(argumentValue), collapse = '\n')
            }

            complexArgument <- paste(argumentName, ' <- ', rawArgument)
            complexArguments <- paste(complexArguments, complexArgument, sep='\n')
        }
    }

    if ('es' %in% names(parsed[[1]])) {
        parsedFnCall <- parsed[[1]]
        parsedFnCall$es <- parsedFnCall$es[[3]]
        rawFnCall <- paste(deparse(parsedFnCall), collapse = '')
        myHistory <- paste(complexArguments, rawFnCall, sep="\n")
    } else {
        myHistory <- paste(complexArguments, rds[[1]]$src, sep='\n')
    }

    if (!is.element(toString(fn), rootFn)) {
        parent <- parsed[[1]]$es[[2]]
        parentVar <- parsed[[1]]$es[[3]]
        parentHistory <- reproduceInR(parent, leaf = F, step = step + 1, savedEnv = savedEnv)
        myHistory <- paste(parentHistory, myHistory, sep='\n')
    }

    if (leaf) {
        assign('env', savedEnv, envir = parent.frame())
        myHistory <- paste('library(phantasus)', 'load(\'~/Downloads/env.rda\') # Please note', myHistory, sep = '\n')
        return(jsonlite::toJSON(myHistory))
    }
    myHistory
}

back to top

Software Heritage — Copyright (C) 2015–2026, The Software Heritage developers. License: GNU AGPLv3+.
The source code of Software Heritage itself is available on our development forge.
The source code files archived by Software Heritage are available under their own copyright and licenses.
Terms of use: Archive access, API— Content policy— Contact— JavaScript license information— Web API