R/generate_instruction_overview.R

Defines functions generate_instruction_overview

Documented in generate_instruction_overview

#' @rdname overview_generation
#' @export
generate_instruction_overview <- function(dctSpecDf,
                                          type,
                                          headingLevel = 3,
                                          hyperlink_UCIDs = "Markdown",
                                          urlPrefix = "#",
                                          sortDecreasing = FALSE) {

  typeInstr <- paste0(type, "_instruction");

  ### Switch order of type and activity
  title <-
    gsub("([^_]*)_([^_]*)",
         "\\2 \\1",
         type);

  ### Writing words in full
  title <- gsub("dev", "developing", title);
  title <- gsub("code", "coding", title);
  title <- gsub("measure", "measurement instruments", title);
  title <- gsub("manipulate", "manipulations", title);
  title <- gsub("aspect", "aspects", title);

  res <-
    c(paste0(repStr("#", headingLevel), " Instruction for ", title),
      format(Sys.time(), '*This overview was generated on %Y-%m-%d at %H:%M:%S %Z (GMT%z)*'),
      "",
      apply(dctSpecDf[order(dctSpecDf$label), ],
            1,
            function(spec) {
              if (grepl("_code", type)) {
                extraInfo <-
                  c(paste0("*Use the following code: **`dct:", spec['dct_id'], "`***"),
                    "");
              } else {
                extraInfo <-
                  "";
              }
              res <-
                ifelse(is.null(spec[typeInstr]) || is.na(spec[typeInstr]) || (nchar(spec[typeInstr])==0),
                       "*(Not specified)*",
                       spec[typeInstr]);
              titleBit <- paste(repStr("#", headingLevel+1), " ", spec['label']);

              ### Replace links to DCTs with hyperlinks
              if (hyperlink_UCIDs == "Markdown") {
                res <- hyperlink_ucids(res,
                                       urlPrefix = urlPrefix);
              } else if (hyperlink_UCIDs == "HTML") {
                res <- hyperlink_ucids(res,
                                       replacement = paste0('<a href="',
                                                            urlPrefix,
                                                            '\\1">dct:\\1</a>'));
              }

              return(c(titleBit,
                       extraInfo,
                       res,
                       ""));
            }));

  return(res);
}

Try the psyverse package in your browser

Any scripts or data that you put into this service are public.

psyverse documentation built on March 7, 2023, 8:31 p.m.