Printing the contents of a character variable in Rmarkdown output knitted to a Word document

I have scraped text and have placed it into a tibble with one character variable "value" and one observation of this variable ""this is a character variable".

I want to print the contents of character_variable$value as text in the output of an Rmarkdown document that is knitted to a Word document.

This is similar in essence to printing the contents of numerical variables in a line of text. For an example, see: https://www.earthdatascience.org/courses/earth-analytics/multispectral-remote-sensing-modis/add-variables-to-rmarkdown-report/

However, the code snippets used in this and similar examples don't appear to be applicable to character variables.

What I would like to see in the Word document output:

This is the text I have scraped: this is a character variable.

 
# Text scraped from somewhere and placed into the variable "value" in the tibble "character_variable"
character_variable <- ("this is a character variable")
character_variable <- as_tibble(character_variable)

character_variable

# value
#  <chr>
#  this is a character variable				
# 1 row

#code snippet that appears to be applicable to numerical variables but not character variables:
text  'This is the text I have scraped:  `r character_variable`.'

Does anyone have any experience with this or insight in how to accomplish it?

Thanks in advance.

I'm confused, it seems like you already have the answer to your question

text  'This is the text I have scraped:  `r character_variable$value`.'

Is not this your desired output?

@andresrcs, Thanks for your response.

I think I didn't explain what I want very well.

My final desired output is this sentence: This is the text I have scraped: this is a character variable.

I want this output in a Word document knitted frrom the Rmarkdown script.

The section "this is a character variable" is the contents of the variable "value" in the tibble, character_value.

This snippet of code returns the error listed below the snippet.


# Text scraped from somewhere and placed into the variable "character_variable"
character_variable <- ("this is a character variable")
character_variable <- as_tibble(character_variable)

text  'This is the text I have scraped:  `r character_variable$value`.'

Error: unexpected string constant in "text 'This is the text I have scraped: r character_variable$value.'"

I hope this clarifies my problem.

Thanks again for your help.

Your problem is that you are putting text inside the r code chunk, just take it out.

---
title: "Untitled"
output: word_document
---

```{r}
library(tibble)
# Text scraped from somewhere and placed into the variable "character_variable"
character_variable <- "this is a character variable"
character_variable <- as_tibble(character_variable)
```
This is the text I have scraped: `r character_variable$value`.
1 Like

@andresrcs, thanks!

Your solution fixed two errors: Removing the sentence from the r code chunk and adding the variable ($value) to the r character_variable$value.

I really appreciate your help with this.

Linda

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.