options(tinytex.verbose = TRUE)
eval = TRUE
links = FALSE
count = length(params$functions)
if (count == 0) { eval = FALSE }
if (params$format == "HTML") { links = TRUE }
setDownloadURI = function(object, filename = stop("'filename' must be specified"), textHTML = "Download", fileext = ".rds", envir = parent.frame()){
  require(base64enc,quietly = TRUE)
  divname = paste(base::sample(LETTERS),collapse="")
  tf = tempfile(pattern=filename, fileext = fileext)
  saveRDS(object, file=tf)
  linkFile = paste(filename,fileext,sep="")
  filenameWithExt = paste(filename, "-", Sys.time(), fileext, sep="")

  uri = dataURI(file = tf, mime = "application/octet-stream", encoding = "base64")
  cat("<a style='text-decoration: none' id='",divname,"'></a>
    <script>
    var a = document.createElement('a');
    var div = document.getElementById('",divname,"');
    div.appendChild(a);
    a.setAttribute('href', '",uri,"');
    a.innerHTML = '",textHTML,"' + ' ",linkFile,"';
    if (typeof a.download != 'undefined') {
      a.setAttribute('download', '",filenameWithExt,"');
    }else{
      a.setAttribute('onclick', 'confirm(\"Your browser does not support the download HTML5 attribute. You must rename the file to ",filenameWithExt," after downloading it (or use Chrome/Firefox/Opera). \")');
    }
    </script>",
    sep="")
}
paramsAsText <- function(params) {
  parametersAsText = list()
  for (i in 1:length(params)) {
    n = names(params)[[i]]
    p = params[[i]]
    if (is(p, "loxcode_experiment") | is(p, "loxcode_sample")) {
      parametersAsText = list.append(parametersAsText, paste(n, "=", p@name))
    }
    else if (is.character(p)) {
      parametersAsText = list.append(parametersAsText, paste(n, "=", p))
    }
    else if (is.numeric(p)) {
      parametersAsText = list.append(parametersAsText, paste(n, "=", paste(p, ",")))
    }
    else if (rapportools::is.boolean(p)) {
      parametersAsText = list.append(parametersAsText, paste(n, "=", p))
    }
  }
  return(paste(parametersAsText, collapse=", "))
}
for (i in 1:count) {
  cat("\n###", "Diagram", i, "  \n")
  cat(params$annotations[[i]], "  \n")
  cat("Function: ", params$types[[i]], "  \n")
  cat("Parameters: ", paramsAsText(params$inputs[[i]]))
  cat("  \n")
  g <- do.call(params$functions[[i]], params$inputs[[i]])
  print(g)
  cat("  \n")
}
if (params$format == "HTML") {
  cat("\n####", "Links to download the loxcode experiments.")
}
for (i in 1:count) {
  setDownloadURI(params$loxcodes[[i]], paste0("Lox", i))
}


jngwehi/loxcodeR documentation built on March 17, 2020, 5:32 p.m.