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

  • f270c57
  • /
  • R
  • /
  • reproduceInR.R
Raw File Download
Permalinks

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 Iframe embedding
swh:1:cnt:2cd4c2516bdeb4a0413cb710a73bd1c917bf5b5a
directory badge Iframe embedding
swh:1:dir:6dd4b3f64d06ebfde201beadc02277047cc9ff0a
Citations

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
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
Generate software citation in BibTex format (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
#'
#' @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
}

Software Heritage — Copyright (C) 2015–2025, 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— Contact— JavaScript license information— Web API

back to top