I'd like to output my own YAML header in RMarkdown with Markdown output. The reason behind this is the dynamic creation of MD files from a given source.
To attempt to add it with results='asis' won't work as it will process the output for links, i.e: add <> to the links:
---
title: "test"
output:
md_document:
variant: markdown
toc: false
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
```{r testyaml, echo=FALSE, results='asis'}
h <- "
+++\n
showonlyimage = false\n
draft = false\n
image = \"http://adsf.com\" \n
date = \"2016-11-05T18:25:22+05:30\"\n
title = \"mytitle\"\n
weight = 0\n
+++\n"
cat(h, sep = " ", fill = FALSE, collapse = '\n')
Output:
+++
showonlyimage = false
draft = false
image = "<http://adsf.com>"
date = "2016-11-05T18:25:22+05:30"
title = "mytitle"
weight = 0
+++
(Notice the <> wrapping http://adsf.com).
Set preserve_yaml: true, like this:
output:
md_document:
variant: markdown
preserve_yaml: true
Related
I am trying to make a RMarkdown report using bookdown::html_document2 to create numbered Fig. 1, Fig. 2, ... across the whole document. Here, I am using both the R-generated and the external figures. I have found that using include_graphics() will help to generate a proper Fig. X numbers, also including in numbering the external figures.
To get the script to work, I am declaring the root.dir = rprojroot::find_rstudio_root_file('C:/myRproject')) while my external figures are located within C:/myRproject/inImg. But in this case, R cannot find my external images anymore? Why is this and how can I properly claim the paths for my R Markdown input, and for external figures? Thank you!
Example:
---
title: "My awesome title"
author: "me"
date: "`r Sys.Date()`"
output:
bookdown::html_document2:
toc: true
toc_depth: 3
knit: (function(input, ...) {
rmarkdown::render(
input,
output_dir = "../outReports",
output_file = file.path("../outReports", glue::glue('test_{Sys.Date()}'
)))
})
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, tidy = TRUE, tidy.opts = list(comment = FALSE), message = F, warning = F)
knitr::opts_chunk$set(fig.width=6, fig.height=3)
library(knitr)
library(png)
```
```{css, echo=FALSE}
p.caption {
font-size: 0.8em;
}
```
```{r setup-root}
knitr::opts_knit$set(root.dir = rprojroot::find_rstudio_root_file('C:/myRproject'))
```
```{r read-libs, eval = TRUE, echo=FALSE, include = FALSE, warning = FALSE, error = FALSE }
# rm(list=ls())
getwd()
#### Source paths and functions -----------------------------------------------
source('myPaths.R') # already this one I can't find within the directory?
# Read pictures as part of teh R chunks
library(knitr)
library(png)
# Read Input data -------------------------------------------------------------------
#getwd()
load(file = "outData/dat1.Rdata")
```
## Including Plots
You can also embed plots, for example:
```{r, out.width = "50%", fig.cap = 'Add fig from internet'}
include_graphics("https://upload.wikimedia.org/wikipedia/commons/thumb/2/2e/MC_Drei-Finger-Faultier.jpg/330px-MC_Drei-Finger-Faultier.jpg")
```
```{r add-extern-plot2, fig.cap = 'my numbered caption'}
# All defaults
img1_path <- "C:/myRproject/inImg/my_extern_fig.png"
img1 <- readPNG(img1_path, native = TRUE, info = TRUE)
attr(img1, "info")
include_graphics(img1_path)
```
I want to rotate table output by 90 degrees on pdf. I am using markdown to generate a report and kable to display the tables in a loop. If possible, I would like to continue using kable since there are lot of other things which are dependent on it that I haven't included in this MWE.
This is a simple example using iris dataset. I tried using landscape function from this post Rotate a table from R markdown in pdf
---
output: pdf_document
header-includes:
\usepackage{lscape}
\usepackage{pdfpages}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
Report page -
```{r results='asis'}
library(knitr)
library(kableExtra)
for (i in 1:3) {
print(landscape(kable_styling(
kable(iris[i:(i+5), ], format = "latex", align = "c", booktabs = TRUE,
longtable = TRUE, row.names = FALSE), latex_options = c("striped"), full_width = T)))
}
```
But this only rotates the page number keeping the table as it is.
I am actually looking for a solution which provides me the output in this way -
To clarify, all the pages with table data in it (3 for this example) should be rotated whereas rest of them should remain as it is. Also, I need longtable = TRUE in kable since in my actual example I am printing lot of rows.
Use package rotating
I added a simple example for you.
---
title: "test"
header-includes: \usepackage[figuresright]{rotating}
#or \usepackage[figuresleft]{rotating}
output:
pdf_document:
latex_engine: xelatex
---
```{r setup, include = FALSE}
library(flextable)
ft <- flextable(head(mtcars))
```
\begin{sidewaysfigure}
`r ft`
\end{sidewaysfigure}
```
Further you can modify it for your tasks ;)
I found another way using rotatebox.
---
output: pdf_document
header-includes:
\usepackage{lscape}
\usepackage{pdfpages}
\usepackage{graphicx}
\usepackage[figuresright]{rotating}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
Report page -
```{r results='asis', warning=FALSE, message=FALSE}
library(knitr)
library(kableExtra)
for (i in 1:3) {
cat('\\rotatebox{90}{')
print(kable(iris[i:(i+5), ], format = "latex", align = "c", booktabs = TRUE,
row.names = FALSE))
cat('}')
cat("\n\\newpage\n")
}
```
I am new to R Markdown and am trying to write a simple script to generate an HTML notebook. (Dictionary comes from Collections library.)
d <- dict(list(SubHeader1 = 'eqn-1', SubHeader2 = 'eqn-2a'))
for (i in d$keys()){
header = i
note = d$get(i)
render(input = "template.rmd",
output_file = "test1.html",
params = list(header = header, note = note))
}
Here is my "template.rmd":
---
title: "Untitled"
author: "user"
date: "1/1/1900"
output:
html_document:
toc: true
toc_float: true
params:
header: NA
note: NA
---
cat("##", params$header, "\n")
params$note
Here is the desired output:
But the code gives this output:
In the render function you can pass the values as named list using the params argument.
d <- list(SubHeader1 = 'eqn-1', SubHeader2 = 'eqn-2a')
rmarkdown::render(input = "template.rmd",
output_file = "test1.html",
params = list(data = d))
In the RMD file declare and initialise those variables.
---
title: "Untitled"
author: "user"
date: "1/1/1900"
output:
html_document:
toc: true
toc_float: true
params:
data: NA
---
```{r, echo=FALSE, message=FALSE, warning=FALSE}
for(i in seq_along(params$data)) {
cat(names(params$data)[i], '\n')
cat(params$data[[i]], '\n')
}
HOW get rendered openxml chunks inside cells of tables in RMD to DOCx ?
I just get same openxml text (mdt) inside tables instead of content ..
any suggestions please?
xml_test.RMD:
Updated Test RMD code :
---
output:
word_document:
reference_docx: ./template.docx
keep_md: yes
md_extensions: +raw_tex
editor_options:
chunk_output_type: console
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
```{r, setuplib, include=FALSE}
#library(tidyverse)
library(knitr)
#library(rmarkdown)
#library(dplyr)
#library(stringr)
library(huxtable)
library(kableExtra)
#library(ftExtra)
```
```{r, xml-test, echo=FALSE, collapse = FALSE}
# XML CONTENT
text_xml <- "<w:p><w:r><w:t>Example text.</w:t></w:r></w:p>"
sanitize(text_xml, type = "rtf")
knitr::asis_output(" \n")
sanitize(text_xml, type = "html")
knitr::asis_output(" \n")
#sanitize(text_xml, type = "latex")
#knitr::asis_output(" \n")
#to_ht <- c(knitr::asis_output(mdt), knitr::asis_output(mdt))
to_ht <- c(text_xml)
#ht <- huxtable::as_hux(to_ht, add_colnames = TRUE, add_rownames = TRUE, escape_contents=FALSE, autoformat = FALSE)
ht <- huxtable::as_hux(text_xml, add_colnames = FALSE, add_rownames = FALSE, escape_contents=FALSE, autoformat = FALSE)
#markdown(ht) <- TRUE
#ht <- set_markdown(ht)
# BAD XML OUTPUT INSIDE CELLS
theme_grey(ht)
knitr::asis_output(" \n")
#knitr::asis_output(ht)
knitr::asis_output(" \n")
# NORMAL XML OUTPUT HERE:
knitr::asis_output(as.character(text_xml))
```
I found as_hux() has incorrect sanitize text in: " <w:p><w:r><w:t>Example text.</w:t></w:r></w:p>
"
instead of RAW or WORD in md:
"<w:p><w:r><w:t>Example text.</w:t></w:r></w:p>"
as result I have this issue with wrong output in WORD huxtable cell
QUESTION: how i can avoid this conversion in MD file ?
When I embed a table generated by using knitr::kable,
I want to adjust the width of the table in beamer_presentation's slide.
Like this picture, some columns don't show in slide when executing the following codes.
Could you tell me how to solve this problem without changing column's names?
---
title: "test"
author: "test"
date: "`r Sys.time()`"
output:
beamer_presentation:
latex_engine: xelatex
fontsize: 6pt
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
knitr::opts_chunk$set(warning = FALSE)
knitr::opts_chunk$set(include = FALSE)
library(tidyverse)
library(kableExtra)
library(knitr)
```
## Table
```{r test, results = "asis",include=TRUE}
test_tbl = tibble(
example_name1 = c("a", "b"),
example_name2 = c("aa", "bb"),
example_name3 = c("aaa", "bbb"),
example_name4 = c("aaaa", "bbbb"),
example_name5 = c("aaaaa", "bbbbb")
)
knitr::kable(test_tbl, format = "markdown")
```
The table is too large to fit on one slide. By adjusting the font size with kableExtra the size of the table can be changed. To do this, set the argument format = "latex" in kable() and then pipe kable_styling().
knitr::kable(test_tbl, format = "latex") %>%
kableExtra::kable_styling(font_size = 7)
It is useful to include LaTeX packages like booktabs to format tables nicely with kable or kableExtra. This is not as flexible as working with LaTeX directly but often leads to quite good results.
First of all, include booktabs in the YAML header.
---
title: "test"
author: "test"
date: "`r Sys.time()`"
header-includes:
- \usepackage{booktabs}
output: beamer_presentation
---
Then set booktabs = TRUE in kable().
knitr::kable(test_tbl, format = "latex", booktabs = T) %>%
kableExtra::kable_styling(font_size = 7)