These functions add codes to one or more sources that were read with one of the loading_sources functions.

code_source(
  input,
  codes,
  indices = NULL,
  output = NULL,
  preventOverwriting = rock::opts$get("preventOverwriting"),
  encoding = rock::opts$get("encoding"),
  silent = rock::opts$get("silent")
)

code_sources(
  input,
  codes,
  output = NULL,
  indices = NULL,
  outputPrefix = "",
  outputSuffix = "_coded",
  recursive = TRUE,
  filenameRegex = ".*",
  preventOverwriting = rock::opts$get("preventOverwriting"),
  encoding = rock::opts$get("encoding"),
  silent = rock::opts$get("silent")
)

Arguments

input

The source, or list of sources, as produced by one of the loading_sources functions.

codes

A named character vector, where each element is the code to be added to the matching utterance, and the corresponding name is either an utterance identifier (in which case the utterance with that identifier will be coded with that code), a code (in which case all utterances with that code will be coded with the new code as well), a digit (in which case the utterance at that line number in the source will be coded with that code), or a regular expression, in which case all utterances matching that regular expression will be coded with that source. If specifying an utterance ID or code, make sure that the code delimiters are included (normally, two square brackets).

indices

If input is a source as loaded by loading_sources, indices can be used to pass a logical vector of the same length as input that indicates to which utterance the code in codes should be applied. Note that if indices is provided, only the first element of codes is used, and its name is ignored.

silent

Whether to be chatty or quiet.

Value

Invisibly, the coded source object.

Examples

### Get path to example source examplePath <- system.file("extdata", package="rock"); ### Get a path to one example file exampleFile <- file.path(examplePath, "example-1.rock"); ### Parse single example source loadedExample <- rock::load_source(exampleFile); ### Show line 71 cat(loadedExample[71]);
#> The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for
### Specify the rules to code all utterances ### containing "Ipsum" with the code 'ipsum' and ### all utterances containing the code codeSpecs <- c("(?i)ipsum" = "ipsum", "BC|AD|\\d\\d\\d\\ds" = "timeRef"); ### Apply rules codedExample <- code_source(loadedExample, codeSpecs);
#> Warning: the condition has length > 1 and only the first element will be used
#> Warning: the condition has length > 1 and only the first element will be used
### Show line 71 cat(codedExample[71]);
#> The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for [[ipsum]] [[timeRef]]
### Also add code "foo" to utterances with code 'ipsum' moreCodedExample <- code_source(codedExample, c("[[ipsum]]" = "foo"));
#> Warning: the condition has length > 1 and only the first element will be used
#> Warning: the condition has length > 1 and only the first element will be used
### Show line 71 cat(moreCodedExample[71]);
#> The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for [[ipsum]] [[timeRef]] [[foo]]
### Use the 'indices' argument to add the code 'bar' to ### line 71 overCodedExample <- code_source(moreCodedExample, "bar", indices=71);
#> Warning: the condition has length > 1 and only the first element will be used
#> Warning: the condition has length > 1 and only the first element will be used
cat(overCodedExample[71]);