find_strings: Find R character strings in input text

Description Usage Arguments Value Note See Also Examples

Description

Finds R character strings in vector x. The input is interpreted as consecutive lines of R code. A function may be supplied for mutating each string found.

Usage

1
find_strings(x, mutate_func = NULL, ...)

Arguments

x

input text. A character vector.

mutate_func

optional function for mutating every string found. The function must accept a string as its first argument and return a string (or something coercible to a string). The function operates on the contents of the string, i.e., the delimiters (quotes) will remain unchanged.

...

optional arguments to mutate_func.

Value

If no function is specified (the default), the location of each R string is returned in a data.frame. The columns are "row_first", "first", "row_last", and "last". These point to the row and column numbers of the first and last character of each string (contents only, i.e., delimiters excluded). The numbering is based on one item of x representing one line of code, i.e., no newlines in x. Empty strings have a "last" character one smaller than the "first".

If mutate_func is specified, the main function returns a list with items "text" and "ranges" described in string_mutate. In "text", every R string in x has been run through the mutation function, possibly in multiple parts.

Note

The mutation function, if specified, should preferably operate on a character-by-character basis. See string_mutate.

See Also

u_escape can be used as mutate_func.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
x <- c("### A sample R script",
       "foo <- function(x) {",
       "    ## Comments are 'ignored'",
       "    paste(\"\\\"foo\", x, '#bar\\\\', \"'baz'\") #, \"more\")",
       "}")
find_strings(x)
x_upper <- find_strings(x, mutate_func = toupper)
cat(x_upper[["text"]], sep = "\n")
x_nchar <- find_strings(x, mutate_func = nchar)
cat(x_nchar[["text"]], sep = "\n")

mvkorpel/uniscape documentation built on May 27, 2019, 11:55 a.m.