Can knitr dynamically output narrative text based on the R code in each fragment?

I'm thinking of developing a .rmd file that can dynamically write some snippets of narratives in the output file (.html, .pdf, ...) based on the previous result of R. Simply put, how I want it to work:

```{r,echo=F,include=FALSE} x=1; ``` ##if x=1 then output text below text1 ##if x=2 then output text below text2 ..... 
+5
r knitr r-markdown
source share
3 answers

The above answer is all good and good, but difficult. Use inline code as suggested in the comment.

 The mean of x is `r mean(x)` which is very high... 

Edit: Since it was chosen as the answer. I will clarify. You can use if () or ifelse () or switch (). I like to switch the most, faster and cleaner code. However, I'm not sure how to use the else statement on the switch.

 ```{r} x <- 1 ``` x is `r if(x==1){"equal to one"} else {"not equal to one"}` which is great... x is `r ifelse(x==1, "equal to one", ifelse(x==2, "equal to two", "not equal to one or two"))` which is great... x is `r switch(x, "1" = "equal to one", "2" = "equal to two")` which is great... 
0
source share

When knitr processes a document, the document is divided into two input categories: plain text and code fragments. Plain text remains unchanged and transferred to the output file. Therefore, if plain text should be included dynamically, it should be inside the fragment.

Code fragments are evaluated according to their options . In the current scenario, the most important parameters are:

  • eval
  • echo
  • results
  • engine

eval determines whether a piece is evaluated; eval = FALSE skips the piece. echo determines whether the source code of the fragment is displayed. results determines how the output of the piece is processed. By default ( results = "markup ), the output hook is used to apply document type markup for output. results = "asis" means that all output is included in the" as-is "output file without any changes.

With these three options, you can do the following:

 This is some text ... ```{r, echo = FALSE, eval = FALSE, results = "asis"} cat("... with a secret inside ...") ``` ```{r, echo = FALSE, eval = TRUE, results = "asis"} cat("... that is dynamically generated.") ``` 

Output:

 This is some text ... ... that is dynamically generated. 

Please note that the first fragment is not evaluated, because eval = FALSE .

However, cumbersome cat() can use a lot of text from the R. engine fragment to overcome this. In addition to R, there are other engines that can be used to evaluate chunks, including the engine (currently undocumented?) asis . This engine is very simple. From the knitr NEWS file :

added a new engine called asis to record the contents of a fragment without processing it; it also takes into account the chunk echo and eval parameters - if one of them is FALSE , the fragment will be hidden; this allows you to write text conditionally

Combining the asis engine with the following syntactic sugar ( source )

for language engines, the syntax for the headers of R Markdown code blocks can now be ```{lang, option=value}` (for example, ```{python} ```{Rcpp} ), which is equivalent ```{Rcpp} ```{r, engine='lang', option=value}

example above:

 This is some text ... ```{asis, echo = FALSE} ... with a secret inside ... ``` ```{asis, echo = TRUE} ... that is dynamically generated. ``` 
+4
source share

The following will print a header and execute a piece of code after that header based on a variable that sets condition (x).

 ```{r,} x<- FALSE Title <- ifelse(x, "My header","") ``` ## `r Title` ```{r, echo=FALSE} if(x) {print(1:5)} ``` 
0
source share

All Articles