1

I have a R markdown file that contains a Latex table-blueprint. In that table I want to put data from dataframes within R. Using 'r [R code]' I can do that. This is However only helpful for a static table.

Is there a way to iterate through a dataframe unsing this Notation or some variation thereof?

I baisically want to copy data from a dataframe df with a varying number of rows into a Latex table.

Sth like this: 

```
\begin{table}
[Latex code]

For (y in 1:n){
'r df[j, 1]` & `r df[j, 2]`}

[Latex code]
\end{table}
```

Is that possible? This is a code example of the currect static version vs how i want it to be:

/// R script that creates data
```
data <- data.frame (c(10,20,30), c("first", "second", "third"))
colnames(data) <- c("Col1", "Col2")
save (data, file = "data.RData")
```

/// in R markdown:
  
```  
title: "dynamicMarkdown"
output: pdf_document
header-includes:
- \usepackage{booktabs}
- \usepackage[utf8]{inputenc}
- \usepackage{graphicx}
- \usepackage[ngerman]{babel}
- \usepackage{lastpage}
- \usepackage{hyperref}
- \usepackage{fancyhdr}
- \usepackage{placeins}
---
  
<!-- load data -->
```{r echo = FALSE}
dfx <-load("data.RData")
df <- data
```
```
\begin{center}
\end{center}
$\ \\$
$\ \\$
$\ \\$
$\ \\$
$\ \\$
$\ \\$
\begin{table}[!h]
\centering
\begin{tabular}{ |c|c|c|c|} 
\hline
$\textbf{Col1}$ & $\textbf{Col2}$ \\ 
\midrule[1pt]
\specialrule{2pt}{-2pt}{-2pt} 
\specialrule{2pt}{0pt}{0.5pt}
`r df[[1,1]]` & `r df[[1,2]]` \\ \hline
`r df[[2,1]]` & `r df[[2,2]]` \\ \hline 
`r df[[3,1]]` & `r df[[3,2]]` \\ \hline
\end{tabular}
\end{table}
```



/// I want to do it this way:

```
title: "dynamicMarkdown"
output: pdf_document
header-includes:
- \usepackage{booktabs}
- \usepackage[utf8]{inputenc}
- \usepackage{graphicx}
- \usepackage[ngerman]{babel}
- \usepackage{lastpage}
- \usepackage{hyperref}
- \usepackage{fancyhdr}
- \usepackage{placeins}
---
  
<!-- load data -->
```{r echo = FALSE}
dfx <-load("data.RData")
df <- data
```

```
\begin{center}
\end{center}
$\ \\$
$\ \\$
$\ \\$
$\ \\$
$\ \\$
$\ \\$
\begin{table}[!h]
\centering
\begin{tabular}{ |c|c|c|c|} 
\hline
$\textbf{Col1}$ & $\textbf{Col2}$ \\ 
\midrule[1pt]
\specialrule{2pt}{-2pt}{-2pt} 
\specialrule{2pt}{0pt}{0.5pt}
j = 1
for ( j in 1:nrows(df)){
`r df[[j,1]]` & `r df[[j,2]]` \\ \hline}
\end{tabular}
\end{table}  
```
stefanR
  • 79
  • 1
  • 7
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Apr 13 '21 at 02:17
  • Could you look https://stackoverflow.com/questions/9274013/generating-latex-output-from-r-data-frame ? – crestor Apr 13 '21 at 04:28
  • 3
    What about using knitr::kable() to print the data frame within the markdown file? https://stackoverflow.com/a/62052575/7239826 – jsv Apr 13 '21 at 04:49
  • Ok that might work. Is it possible to customize kable to a high degree? I have very specifc regulations on how the table should look like – stefanR Apr 14 '21 at 07:05

0 Answers0