user3091668
user3091668

Reputation: 2310

Replace blocks of text conditionally in R

I would like to replace blocks of text conditionally depending of internal results in a R run. Let's assume that I will import a template such as teXt:

lines <- tempfile(fileext = ".data")
cat("
We have a (positive)1 (negative)2 number.
", file=lines, sep = "\n")
teXt <- readLines(lines)

In this text we have possibilities, 1-positive and 2-negative. Assuming that I have a positive number I tried to use:

teXt  <- stringr::str_replace_all(teXt, " (negative)2", "") ## Exclude negative string possibility
teXt    <-  stringr::str_replace_all(teXt, " (positive)1", "positive") ## make positive the word in the final text

However, the replacement do not follow what I expect, i.e. did not produce the sentence "We have a positive number" and maintain the original template text. I tried to use *words* instead (words) but also did not work. I wonder if we have functions in R to perform such a task in a smarter (and workable!) way.

Upvotes: 0

Views: 83

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 389135

For your attempt to work you have to escape the special characters (opening and closing brackets)

teXt <- stringr::str_replace_all(teXt, "\\(negative\\)2", "") 
teXt <-  stringr::str_replace_all(teXt, "\\(positive\\)1", "positive") 

Assuming you have a positive number a simplified(?) approach would be to remove round brackets along with text inside it and replace it with "positive".

stringr::str_replace_all(teXt, "\\(.*\\)\\d+", "positive")
#[1] ""                               "    We have a positive number." "    "  

Or using gsub in base R

gsub("\\(.*\\)\\d+", "positive", teXt)

You can wrap the output in trimws if you want to remove leading and lagging whitespace.

Upvotes: 2

Related Questions