Inserting r code into text that is not hardcoded in Rmarkdown file

Viewed 94

I am creating a document with Rmarkdown. The document will have lots of different versions and all of the text for the different versions has been written in an Excel spreadsheet, which is then read into the Rmarkdown file. Inside the text, which sometimes differs between reports, there are keywords in square brackets which need to be replaced with r code. I am having trouble getting the rcode to evaluate inside the text and print out properly in the Rmarkdown output.

# Text like that in the Excel spreadsheet
report_text <- ("There are [NumberFruit] fruits in the fruitbowl. [HighestPercent]% of the fruit are [HighestPercentType].")

#Extract variables within the square brackets
variables <- str_extract_all(report_text, "\\[[A-Z,a-z]+\\]")

# Define all varaibles - the variables are the same in each report. The data in the actual report differs and is defined from a dataframe. 
for (i in unlist(variables)){
  if(grepl("NumberFruit", i)){
    NumberFruit <- 10
    
  } else if(grepl("HighestPercent", i)){
    HighestPercent <- 56
    
  } else if(grepl("HighestPercentType", i)){
    HighestPercentType <- "apples"
    
  } else if (length(unlist(variables)) > 3){
    stop("Additional VARIABLE:",i, "has not been assigned")
  }
}

Once the variables have been defined I would normally use something like below, but as the text isn't hardcoded into the Rmarkdown file this approach isn't possible.

final_text <- paste0("There are ",NumberFruit, "  fruits in the fruitbowl. ",HighestPercent, "% are ",HighestPercentType, ".")

So I have tried formatting the text as per the paste option above, but this does not produce the desired output.

report_text2 <- gsub('\\[', '",',(gsub('\\]', ', "', report_text)))

#Also tried
report_text2 <- paste0(gsub('\\[', '",',(gsub('\\]', ', "', report_text))))

I then use r final text in the Rmarkdown text to create the text in the report. Both versions of the above code have the same result shown below.

Current Rmarkdown output: There are ",NumberFruit," fruits in the fruitbowl. ",HighestPercent, "% of the fruit are ",HighestPercentType, "."

Desired Rmarkdown output: There are 10 fruits in the fruitbowl. 56% of the fruit are apples.

I have googled for clues on what else to try but have not been able to find anything and am a bit stuck on where to go from here. Any advice on how to get this working would be greatly appreciated. I do not normally deal with text strings and feel like I am missing something fundamental here.

1 Answers

If we need to interpolate the variables created in RMD file, an option is with glue

library(glue)
library(stringr)
final_text <- glue::glue(str_replace_all(str_replace_all(report_text,
         "\\[", "{"),  "\\]", "}"))

Full code in RMD file

---
title: "test"
author: "akrun"
date: "16/03/2021"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## R Markdown


```{r test1}
report_text <- "There are [NumberFruit] fruits in the fruitbowl. [HighestPercent]% of the fruit are [HighestPercentType]."
NumberFruit <- 10
HighestPercent <- 56
HighestPercentType <- "apples"
```

```{r test2}

library(stringr)
final_text <- glue::glue(str_replace_all(str_replace_all(report_text, "\\[", "{"), "\\]", "}"))

```

`r final_text`

-output

enter image description here

Related