I am trying to figure out how to create a loop that inserts some text into the rmarkdown file, and then produces the graph or table that corresponds to that header. The following is how I picture it working:
for(i in 1:max(month)){ ### `r month.name[i]` Air quaility ```{r, echo=FALSE} plot(airquality[airquality$Month == 5,]) ``` }
This ofcourse just prints the for loop as text, if i surround the for loop with r`` I would just get an error.
I want the code to produce an rmd file that looks like this:
Plot
plot
and so on and so forth. any ideas? I cannot use latex because I at my work they do not let us download exe files, and I do not know how to use latex anyways. I want to produce a word document.
You can insert an R code chunk either using the RStudio toolbar (the Insert button) or the keyboard shortcut Ctrl + Alt + I ( Cmd + Option + I on macOS).
You can quickly insert chunks into your R Markdown file with the keyboard shortcut Cmd + Option + I (Windows Ctrl + Alt + I).
If you are using RStudio, then the “Knit” button (Ctrl+Shift+K) will render the document and display a preview of it. Note that both methods use the same mechanism; RStudio's “Knit” button calls rmarkdown::render() under the hood.
You can embed the markdown inside the loop using cat()
.
Note: you will need to set results="asis"
for the text to be rendered as markdown. Note well: you will need two spaces in front of the \n
new line character to get knitr to properly render the markdown in the presence of a plot out.
# Monthly Air Quality Graphs ```{r pressure,fig.width=6,echo=FALSE,message=FALSE,results="asis"} attach(airquality) for(i in unique(Month)) { cat(" \n###", month.name[i], "Air Quaility \n") #print(plot(airquality[airquality$Month == i,])) plot(airquality[airquality$Month == i,]) cat(" \n") } ```
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With