R Studio Knitr PDF Markdown prints "asis" - r

I am generating a PDF document using Knitr. I am calling my r script from a separate file. Everything prints out fine in the PDF, but when I output a table using Stargazer, knitr generates a new page in the PDF document with "asis" printed on the page. I included a screen shot. Any ideas why this is happening?
My code chunk:
---
title: "PALS Biomarker Analysis"
author: "Daniel Parker"
date: "November 20, 2017"
output: pdf_document
header-includes:
- \usepackage{caption}
---
\captionsetup[table]{labelformat=empty}
```{r echo=FALSE}
library(knitr)
read_chunk('11-20-17 PALS Analysis V3.R')
```
```{r eighth, echo = FALSE, results="asis"}
<<linear_model_one>>
```
If I remove the results="asis" the table does not print correctly.

Try changing this code
```{r eighth, echo = FALSE, results="asis"}
<<linear_model_one>>
```
to this (remove the double quotes surrounding asis)
```{r eighth, echo = FALSE, results='asis'}
<<linear_model_one>>
```

Related

How to show the whole result of a knitted chunk at the end of it

I have a problem with knitting in R. If a chunk has something that needs to be printed, the chunk gets split in two and the result is shown under each piece of code, as you can see in the following image:
So, I want every result to be shown together at the end of the chunk. Is there a way to do that?
Something like the following:
```{r echo=T, eval=FALSE}
message("hello")
message("world")
```
\#\# hello
\#\# world
Thank you very much.
Use collapse=TRUE, documented here: https://yihui.org/knitr/options/#code-evaluation.
---
title: hello
---
```{r blockname}
message("hello")
message("world")
```
---
title: hello
---
```{r blockname, collapse = TRUE}
message("hello")
message("world")
```
Since you need to change the way R shows things (output after a command), then we need to borrow from a previous answer of mine:
---
title: hello
---
```{r blockname, echo = FALSE, include = FALSE}
message("hello")
message("world")
```
```{r showblockname, ref.label='blockname', eval=FALSE}
```
```{r blockname, echo=FALSE, collapse=TRUE}
```

R Markdown code chunk does not show math equations

Basically i want to build a random multiple choice question generator with R Markdown. For this task there need to be equations in the code chunks of the markdown.
The following works like a charm and gives the equation "greekbeta = 1"
---
title: "test"
author: "me"
output:
word_document: default
---
```{r eval=TRUE, echo=FALSE,results = "asis"}
"$\beta = 1$"
```
In contrast, this will not work when some other math symbol is used, for example:
---
title: "test"
author: "me"
output:
word_document: default
---
```{r eval=TRUE, echo=FALSE,results = "asis"}
"$\sum_{n=1}^{\infty}$"
```
After pressing knit, an error occurs (unfortunately the error message is in german, basically this: "'\s' is an unknown escape-sequence within the string starting with "$/s"").
I am very puzzled by this, especially because for example \frac{1}{2} works, but \hat{x} does not. Equations in the "normal" markdown text are no problem at all. But for my task, the equations have to be in the code chunk sections.
Does someone has a workaround for this problem? I tried using "$\hat{x}$" or even "$$\hat{x}$", but the error message is still the same.
I am using pandoc 2.11.4, R 4.1.0 and knitr 1.33
Use cat() and escape the escapes.
---
title: "test"
author: "me"
output:
word_document: default
---
```{r eval=TRUE, echo=FALSE,results = "asis"}
cat("$\\beta = 1$", '\n\n')
cat("$a^2+b^2 = c^2$", '\n\n')
cat("$\\sum_{n=1}^{\\infty}x_i$", '\n\n')
```

r - rmarkdown - yaml - Is it possible to include inline, internal document hyperlink in title?

is it possible to include an inline (internal document) hyperlink in the YAML title block of a RMarkdown document? Or is there a R package that allows this?
Below is what I am trying to do:
---
title: blah {#one1} and so on {#two2}
...
---
# Bibliography
1. Reference note to [explain](#one1) in title
2. Reference note to [explain](#two2) in title
Thank you.
The title is parsed as markdown, so you can use regular markdown link syntax in order to get the behavior you want.
---
title: 'There is a link in this title...[LINK](#anchor)'
author: "Jason Punyon"
date: "6/16/2020"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Bibliography
<p id='anchor'>Here's where I went!</p>
You could also just put the link in manually...
---
title: 'There is a link in this title...LINK'
author: "Jason Punyon"
date: "6/16/2020"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Bibliography
<p id='anchor'>Here's where I went!</p>

Can a custom knit_hooks set options before and after R chunk?

I would like to specify text (specifically a div tag) before and after some R code in a Rmd file using github_document. I can't seem to get both the before and after to work.
For example knitting the following:
---
title: "Untitled"
output: github_document
---
```{r setup, include=FALSE}
library(knitr)
knit_hooks$set(toggle = function(before, options, envir) {
if(options$toggle){
if(before) {
print('<div id="hideMe">')
} else {
print("</div>")
}
}
})
```
## GitHub Documents
```{r pressure, echo=TRUE, toggle=TRUE}
# Here's some
```
Produces:
Untitled
================
GitHub Documents
----------------
``` r
# Here's some
```
If I delete the else part:
---
title: "Untitled"
output: github_document
---
```{r setup, include=FALSE}
library(knitr)
knit_hooks$set(toggle = function(before, options, envir) {
if(options$toggle){
if(before) {
print('<div id="hideMe">')
}
}
})
```
## GitHub Documents
```{r pressure, echo=TRUE, toggle=TRUE}
# Here's some
```
It works as expected, and I get the before section:
Untitled
================
GitHub Documents
----------------
<div id="hideMe">
``` r
# Here's some
```
If I use !before in the if statement, the trailing </div> shows up. But I have been unable to get both the leading and trailing sections to show up. Any suggestions to get both before and after text to show up?
Don't print in chunk hooks, return the text that is to be added to the document:
In knitr, hooks can also be used to insert texts into the output. To do this, the hook function must return a character result. [Source]
The most likely explanation for the behavior you observed is that in R, "if returns the value of the expression evaluated" (see ?Control section "Value").

Using DiagrammeR in Word document (generated using rMarkdown)

I've been looking at the diagrammeR package (http://rich-iannone.github.io/DiagrammeR/) to generate diagrams in rMarkdown. This works great when rendering the documents in HTML; now the question I have is whether there is a possibility to output the document as MS Word document?
For example, consider this:
---
title: "Test"
author: "Test"
date: "Monday, May 18, 2015"
output: html_document
---
```{r, echo=FALSE, warning=FALSE}
if (!require("DiagrammeR")) library("DiagrammeR")
```
Check out this diagram:
```{r, echo=FALSE, results='asis'}
DiagrammeR::grViz("
digraph rmarkdown {
node [shape = box ]
'A' -> 'B'
}
")
```
Using HTML as output format works like a charm. But, when I switch to MS Word, all I get is:
Error: Functions that produce HTML output found in document targeting docx output.
Please change the output type of this document to HTML.
Any ideas would be appreciated.
Many thanks, Philipp
trelliscope is useful: https://github.com/tesseradata/trelliscope
After installing http://phantomjs.org/download.html,
You can generate word doc file by:
---
title: "Test"
author: "Test"
date: "Monday, May 18, 2015"
output: word_document
---
```{r include=FALSE}
if (!require("DiagrammeR")) library("DiagrammeR")
library(trelliscope)
```
Check out this diagram:
```{r, include=FALSE}
p = DiagrammeR::grViz("
digraph rmarkdown {
node [shape = box ]
'A' -> 'B'
}
")
widgetThumbnail(p, paste0(getwd(), "/hoge.png"))
```
![](hoge.png)
Here is the screenshot. It looks perfect :)

Resources