Commit 8457e2a0 authored by Gjalt-Jorn Peters's avatar Gjalt-Jorn Peters

Added getter and setter functions for the options

parent e6a0ca38
Pipeline #94808884 failed with stages
in 12 minutes and 34 seconds
......@@ -21,6 +21,7 @@ export(load_source)
export(load_sources)
export(merge_sources)
export(numericToBase30)
export(opts)
export(parse_source)
export(parse_source_by_coderId)
export(parse_sources)
......
......@@ -9,7 +9,7 @@
#' breaks, utterance identifiers, and full utterances. All `<span>` elements
#' except for the full utterances, which are placed in `<div>` elements.
#'
#' @return The character vector witht he replacements made.
#' @return The character vector with the replacements made.
#' @export
#'
#' @examples add_html_tags("[[cid=participant1]]
......@@ -18,18 +18,16 @@
#' ---paragraph-break---
#' And another utterance.");
add_html_tags <- function(x,
codeRegexes = c(codes = "\\[\\[([a-zA-Z0-9._>-]+)\\]\\]"),
idRegexes = c(caseId = "\\[\\[cid[=:]([a-zA-Z0-9._-]+)\\]\\]",
stanzaId = "\\[\\[sid[=:]([a-zA-Z0-9._-]+)\\]\\]"),
sectionRegexes = c(paragraphs = "---paragraph-break---",
secondary = "---<[a-zA-Z0-9]?>---"),
uidRegex = "\\[\\[uid[=:]([a-zA-Z0-9._-]+)\\]\\]",
inductiveCodingHierarchyMarker = ">",
codeClass = "code",
idClass = "identifier",
sectionClass = "sectionBreak",
uidClass = "uid",
utteranceClass = "utterance") {
codeRegexes = rock::opts$get(codeRegexes),
idRegexes = rock::opts$get(idRegexes),
sectionRegexes = rock::opts$get(sectionRegexes),
uidRegex = rock::opts$get(uidRegex),
inductiveCodingHierarchyMarker = rock::opts$get(inductiveCodingHierarchyMarker),
codeClass = rock::opts$get(codeClass),
idClass = rock::opts$get(idClass),
sectionClass = rock::opts$get(sectionClass),
uidClass = rock::opts$get(uidClass),
utteranceClass = rock::opts$get(utteranceClass)) {
res <- x;
......
......@@ -77,7 +77,7 @@ clean_source <- function(input,
"\n")),
extraReplacementsPre = NULL,
utteranceSplits = c("([\\?\\!]+\\s?|\u2026\\s?|[[:alnum:]\\s?]\\.(?!\\.\\.)\\s?)"),
utteranceMarker = "\n",
utteranceMarker = rock::opts$get(utteranceMarker),
replacementsPost = list(c("([^\\,]),([^\\s])",
"\\1, \\2")),
extraReplacementsPost = NULL,
......
......@@ -53,9 +53,10 @@ collect_coded_fragments <- function(x,
context = 0,
heading = NULL,
headingLevel = 2,
fragmentDelimiter = "\n\n-----\n\n",
fragmentDelimiter = rock::opts$get(fragmentDelimiter),
utteranceGlue = "\n\n",
sourceFormat = "\n\n**Source: `%s`**\n\n",
add_html_tags = TRUE,
rawResult = FALSE,
output = NULL,
cleanUtterances = TRUE,
......@@ -168,6 +169,10 @@ collect_coded_fragments <- function(x,
}
}
if (add_html_tags) {
res <- add_html_tags(res);
}
if (is.null(output)) {
return(res);
} else {
......
#' Options for the rock package
#'
#' The `rock::opts` object contains three functions to set, get, and reset
#' options used by the rock package. Use `rock::opts$set` to set options,
#' `rock::opts$get` to get options, or `rock::opts$reset` to reset specific or
#' all options to their default values.
#'
#' @usage rock::opts$set(...)
#' rock::opts$get(option, default=FALSE)
#' rock::opts$reset(...)
#'
#' @param ... For `rock::opts$set`, the dots can be used to specify the options
#' to set, in the format `option = value`, for example, `utteranceMarker = "\n"`. For
#' `rock::opts$reset`, a list of options to be reset can be passed.
#' @param option The name of the option to set.
#' @param default The default value to return if the option has not been manually
#' specified.
#'
#' @export
opts <- list();
opts$set <- function(...) {
dots <- list(...);
dotNames <- names(dots);
names(dots) <-
paste0("rock.", dotNames);
if (all(dotNames %in% names(opts$defaults))) {
do.call(options,
dots);
} else {
stop("Option '", option, "' is not a valid (i.e. existing) option for the rock!");
}
}
opts$get <- function(option, default=FALSE) {
option <- as.character(substitute(option));
if (!option %in% names(opts$defaults)) {
stop("Option '", option, "' is not a valid (i.e. existing) option for the rock!");
} else {
return(getOption(paste0("rock.", option),
opts$defaults[[option]]));
}
}
opts$reset <- function(...) {
dots <- list(...);
if (length(dots) == 0) {
do.call(opts$set,
opts$defaults);
} else {
dotNames <- names(dots);
names(dots) <-
paste0("rock.", dotNames);
if (all(dotNames %in% names(opts$defaults))) {
do.call(opts$set,
dots);
} else {
invalidOptions <-
!which(all(dotNames %in% names(opts$defaults)));
stop("Option(s) ", vecTxtQ(option[invalidOptions]),
"' is/are not a valid (i.e. existing) option for the rock!");
}
}
}
opts$defaults <-
list(codeRegexes = c(codes = "\\[\\[([a-zA-Z0-9._>-]+)\\]\\]"),
idRegexes = c(caseId = "\\[\\[cid[=:]([a-zA-Z0-9._-]+)\\]\\]",
stanzaId = "\\[\\[sid[=:]([a-zA-Z0-9._-]+)\\]\\]",
coderId = "\\[\\[coderId[=:]([a-zA-Z0-9._-]+)\\]\\]"),
sectionRegexes = c(paragraphs = "---paragraph-break---",
secondary = "---<[a-zA-Z0-9]?>---"),
uidRegex = "\\[\\[uid[=:]([a-zA-Z0-9._-]+)\\]\\]",
codeDelimiters = c("[[", "]]"),
utteranceMarker = "\n",
fragmentDelimiter = "\n\n-----\n\n",
inductiveCodingHierarchyMarker = ">",
codeClass = "code",
idClass = "identifier",
sectionClass = "sectionBreak",
uidClass = "uid",
utteranceClass = "utterance",
encoding = "UTF-8");
......@@ -4,12 +4,9 @@ parse_sources <- function(path,
extension = "rock|dct",
regex=NULL,
recursive=TRUE,
codeRegexes = c(codes = "\\[\\[([a-zA-Z0-9._>-]+)\\]\\]"),
idRegexes = c(caseId = "\\[\\[cid=([a-zA-Z0-9._-]+)\\]\\]",
stanzaId = "\\[\\[sid=([a-zA-Z0-9._-]+)\\]\\]",
coderId = "\\[\\[coderId=([a-zA-Z0-9._-]+)\\]\\]"),
sectionRegexes = c(paragraphs = "---paragraph-break---",
secondary = "---<[a-zA-Z0-9]?>---"),
codeRegexes = rock::opts$get(codeRegexes),
idRegexes = rock::opts$get(idRegexes),
sectionRegexes = rock::opts$get(sectionRegexes),
uidRegex = "\\[\\[uid=([a-zA-Z0-9._-]+)\\]\\]",
autoGenerateIds = c('stanzaId'),
persistentIds = c('caseId', 'coderId'),
......
......@@ -4,16 +4,16 @@
\alias{add_html_tags}
\title{Add HTML tags to a source}
\usage{
add_html_tags(x, codeRegexes = c(codes =
"\\\\[\\\\[([a-zA-Z0-9._>-]+)\\\\]\\\\]"), idRegexes = c(caseId =
"\\\\[\\\\[cid[=:]([a-zA-Z0-9._-]+)\\\\]\\\\]", stanzaId =
"\\\\[\\\\[sid[=:]([a-zA-Z0-9._-]+)\\\\]\\\\]"),
sectionRegexes = c(paragraphs = "---paragraph-break---", secondary =
"---<[a-zA-Z0-9]?>---"),
uidRegex = "\\\\[\\\\[uid[=:]([a-zA-Z0-9._-]+)\\\\]\\\\]",
inductiveCodingHierarchyMarker = ">", codeClass = "code",
idClass = "identifier", sectionClass = "sectionBreak",
uidClass = "uid", utteranceClass = "utterance")
add_html_tags(x, codeRegexes = rock::opts$get(codeRegexes),
idRegexes = rock::opts$get(idRegexes),
sectionRegexes = rock::opts$get(sectionRegexes),
uidRegex = rock::opts$get(uidRegex),
inductiveCodingHierarchyMarker = rock::opts$get(inductiveCodingHierarchyMarker),
codeClass = rock::opts$get(codeClass),
idClass = rock::opts$get(idClass),
sectionClass = rock::opts$get(sectionClass),
uidClass = rock::opts$get(uidClass),
utteranceClass = rock::opts$get(utteranceClass))
}
\arguments{
\item{x}{A character vector with the source}
......@@ -24,7 +24,7 @@ breaks, utterance identifiers, and full utterances. All \code{<span>} elements
except for the full utterances, which are placed in \code{<div>} elements.}
}
\value{
The character vector witht he replacements made.
The character vector with the replacements made.
}
\description{
This function adds HTML tags to a source to allow pretty printing/viewing.
......
......@@ -14,7 +14,7 @@ clean_source(input, output = NULL,
"\\\\1...\\\\3"), c("(\\\\s*\\\\r?\\\\n){3,}", "\\n")),
extraReplacementsPre = NULL,
utteranceSplits = c("([\\\\?\\\\!]+\\\\s?|…\\\\s?|[[:alnum:]\\\\s?]\\\\.(?!\\\\.\\\\.)\\\\s?)"),
utteranceMarker = "\\n",
utteranceMarker = rock::opts$get(utteranceMarker),
replacementsPost = list(c("([^\\\\,]),([^\\\\s])", "\\\\1, \\\\2")),
extraReplacementsPost = NULL, preventOverwriting = TRUE,
removeNewlines = FALSE, encoding = "UTF-8", silent = FALSE)
......
......@@ -5,8 +5,12 @@
\title{Create an overview of coded fragments}
\usage{
collect_coded_fragments(x, codes = ".*", context = 0, heading = NULL,
headingLevel = 2, rawResult = FALSE, output = NULL,
cleanUtterances = TRUE, silent = TRUE)
headingLevel = 2,
fragmentDelimiter = rock::opts$get(fragmentDelimiter),
utteranceGlue = "\\n\\n",
sourceFormat = "\\n\\n**Source: `\%s`**\\n\\n", add_html_tags = TRUE,
rawResult = FALSE, output = NULL, cleanUtterances = TRUE,
silent = TRUE)
}
\arguments{
\item{x}{The parsed source(s) as provided by \code{rock::parse_source}
......
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/opts.R
\docType{data}
\name{opts}
\alias{opts}
\title{Options for the rock package}
\format{An object of class \code{list} of length 4.}
\usage{
rock::opts$set(...)
rock::opts$get(option, default=FALSE)
rock::opts$reset(...)
}
\arguments{
\item{...}{For \code{rock::opts$set}, the dots can be used to specify the options
to set, in the format \code{option = value}, for example, \code{utteranceMarker = "\n"}. For
\code{rock::opts$reset}, a list of options to be reset can be passed.}
\item{option}{The name of the option to set.}
\item{default}{The default value to return if the option has not been manually
specified.}
}
\description{
The \code{rock::opts} object contains three functions to set, get, and reset
options used by the rock package. Use \code{rock::opts$set} to set options,
\code{rock::opts$get} to get options, or \code{rock::opts$reset} to reset specific or
all options to their default values.
}
\keyword{datasets}
......@@ -26,13 +26,9 @@ parse_source(text, file, codeRegexes = c(codes =
\method{print}{rockParsedSource}(x, prefix = "### ", ...)
parse_sources(path, extension = "rock|dct", regex = NULL,
recursive = TRUE, codeRegexes = c(codes =
"\\\\[\\\\[([a-zA-Z0-9._>-]+)\\\\]\\\\]"), idRegexes = c(caseId =
"\\\\[\\\\[cid=([a-zA-Z0-9._-]+)\\\\]\\\\]", stanzaId =
"\\\\[\\\\[sid=([a-zA-Z0-9._-]+)\\\\]\\\\]", coderId =
"\\\\[\\\\[coderId=([a-zA-Z0-9._-]+)\\\\]\\\\]"),
sectionRegexes = c(paragraphs = "---paragraph-break---", secondary =
"---<[a-zA-Z0-9]?>---"),
recursive = TRUE, codeRegexes = rock::opts$get(codeRegexes),
idRegexes = rock::opts$get(idRegexes),
sectionRegexes = rock::opts$get(sectionRegexes),
uidRegex = "\\\\[\\\\[uid=([a-zA-Z0-9._-]+)\\\\]\\\\]",
autoGenerateIds = c("stanzaId"), persistentIds = c("caseId",
"coderId"), noCodes = "^uid:|^uid=|^dct:|^ci:",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment