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 :)
Related
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')
```
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>
I am preparing R markdown document and I would like to display postgre sql codes nicely in R markdown document of course the codes should only be displayed not runned. How can I do that? . Below I put some code examples.
Thank you.
title: "POSTGRE sql "
output:
html_document: default
pdf_document: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r1, eval=F}
with data as (select * from (values
('03-05-2019'::date,'{"color": true,"view": [181] ,"school":
[805,812,852,856,857]}'::jsonb),
('06-08-2019'::date,'{"color": false,"view": [184,185],"school":
[805,855,859]}'::jsonb),
('04-07-2019'::date,'{"color": true,"view": [184,185,189],"school":
[855,859]}'::jsonb)
) as v(published_date,attributes))
```
In the header add this to your output
output:
html_document:
highlight: pygments
and in the body use this line
{r, engine = 'sql', eval = FALSE}
I am not sure the major differences in SQL and PostgreSQL, but the syntax highlighting does work.
Here is a link to show other languages you can use with pygments
http://pygments.org/languages/
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>>
```
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").