Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using knitr/rmarkdown to produce outputs in multiple natural languages (from a data.frame)

I am trying to produce a document in rmarkdown that can produce outputs in multiple (natural) languages, it should extract the text in one of the translations from a data.frame.

The data.frame should contain a column for each language and translations of the same text on each row, e.g.

EN <- c('title', 'author', 'a sentence')
FR <- c('titre', 'auteur', 'une phrase')
translation <- data.frame(EN, FR, stringsAsFactors = FALSE)

It should be possible to format the text extracted, e.g.

# [desired code here]

Should produce and rmarkdown title.

EDIT: Ideally we would be able to specify the language in the YAML front-matter

like image 469
Bastiaan Quast Avatar asked Oct 04 '17 07:10

Bastiaan Quast


People also ask

What is the function of knitr in creating markdown document?

knitr is an engine for dynamic report generation with R. It is a package in the programming language R that enables integration of R code into LaTeX, LyX, HTML, Markdown, AsciiDoc, and reStructuredText documents. The purpose of knitr is to allow reproducible research in R through the means of literate programming.

How do you use the knitr function in R?

If you are using RStudio, then the “Knit” button (Ctrl+Shift+K) will render the document and display a preview of it.

How do I show output in R Markdown?

If you prefer to use the console by default for all your R Markdown documents (restoring the behavior in previous versions of RStudio), you can make Chunk Output in Console the default: Tools -> Options -> R Markdown -> Show output inline for all R Markdown documents .

What are the three types of sections in an R Markdown document select three options?

The following sections dive into the three components of an R Markdown document in more details: the markdown text, the code chunks, and the YAML header.


1 Answers

You can do this as follows: (please see the ps for more latex-options)

Why? The YAML header is evaluated line by line :) - see here

---
params:
  lang: EN
lang: "`r switch(params$lang, DE = 'de-DE', EN = 'en-US')`"
output: pdf_document
toc: 1
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
lswitch <- function(lang, ...){
  switch(lang, ..., stop("No translation privided"))
}
```

# `r lswitch(params$lang, DE = "Dies ist der Titel", EN = "this is the title")`

`r lswitch(params$lang, 
DE = "Die folgende Abbildung zeigt die Funktion $f(x) = x^2$", 
EN = "The following plot shows the function $f(x) = x^2$"
)
`

```{r plot1_cap, include=FALSE}
plot1_cap <- lswitch(params$lang, DE = "Tolle Abbildung", EN = "Great plot")
```
```{r plot1, fig.cap= plot1_cap}
plot(seq(-5, 5, length.out = 50), seq(-5, 5, length.out = 50)^2, 
     type = "l", xlab = "x", ylab = "f(x)")
```

# `r lswitch(params$lang, DE = "Zweiter Titel", EN = "Second Title")`

`r lswitch(params$lang, 
DE = "Zweiter Abschnitt", 
EN = "Second paragraph"
)
`

This results in

enter image description here

If you change the yaml-header to

---
params:
  lang: DE
lang: "`r switch(params$lang, DE = 'de-DE', EN = 'en-US')`"
output: pdf_document
toc: 1
---

you get

enter image description here

PS: More Latex options

If you want to use a specific language package you can add the following in your YAML header: (see https://stackoverflow.com/a/29163496/3301344)

---
header-includes:
  - \usepackage[ngerman]{babel}
---

For e.g. using chinese have a look at: http://felixfan.github.io/RMarkdown-Chinese-PDF/

like image 84
Rentrop Avatar answered Sep 29 '22 14:09

Rentrop