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}
```