3

I mostly code in R, and have been using knitr Rnw for a while, mostly following the best practices outline in 14mech14's YouTube videos, with a parent rnw document, child rnw document, and a document for my R code and all the tables / figures.

I recently started experimenting with Rmd for my journal articles using the rticles package, but just ran into the issue of having to load new packages into the Rmd document. I have seen Yihui's document, but it doesn't seem to work if I have

output: rticles::aea_article
extra_dependencies: ["rotating"]

How do I solve this issue? (Moreover, if I have colleagues that work in STATA, how do I enable them to work on the paper as well? Should I just write everything in Latex itself and export?)

Here is my .Rmd file:

---
title: "BLAH"
month: "`r format(Sys.Date(), '%m')`"
year: "`r format(Sys.Date(), '%Y')`"
author:
  - name: Myself
    firstname: Nobody
    surname: Owens
    email: nobody.owens@blob.edu
    affiliation: some institute of technology
acknowledgements: |
  Thank you to my humble research assistant for his research assistance.
abstract: |
  This is the abstract.

output: rticles::aea_article
---

```{r setup, include=FALSE}
# this R code imports my data from the .R file: (of course for this online example I don't have a file to show)

knitr::opts_chunk$set(echo = FALSE, warning = FALSE)

# IMPORT PACKAGES-------------
list.of.packages <- c("dplyr", "data.table","ggplot2", "kableExtra", "tinytex", "stargazer")
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[, "Package"])]
if (length(new.packages)) install.packages(new.packages)
for (package in list.of.packages) {library(eval((package)), character.only = TRUE)}

# LOAD THE DATA FROM THE .R FILE---------
# load(".RData")
```
Blah blah blah...

This works, because it's a small table.

```{r, echo = FALSE, warning = FALSE}
# some table with kableExtra package:
# just an example...
a <- data.frame(a = c("orcs","orcs","elves", "elves", "elves"), b = c(1,2,3,4,5), c = c(1,2,3,4,5))

a %>% kbl(
  .,
  booktabs = TRUE,
  linesep = "",
  caption = "Model Summary Statistics",
  label = "meansd"
) %>%
  kableExtra::column_spec(column = 2:3, width = "0.5in") %>%
  kableExtra::collapse_rows(columns = 1,
                            valign = "middle",
                            longtable_clean_cut = FALSE) %>%
  kable_styling(
    font_size = 4,
    latex_options =
      c("hold_position",
        "condensed")
  )
```

But what if I have a big one...?


\begin{table}[!htbp]\centering
\caption{My Regressions}
\label{tab:nobodyowensregressions}
```{r, results = "asis"}

# this is some code from my original .R file that gets saved into the .RData file:

panelfit <- list()
for (i_ in c(1:10)){
panelfit[[i_]] <- lm(b~c, data = a)
}

model.smmrys.tex <- 
  R.utils::captureOutput(
    stargazer(
      panelfit,
      type="latex",
      font.size = "footnotesize",
      style = "aer",
      float = FALSE,
      column.sep.width = "-12pt",
      df = FALSE,
      dep.var.labels = "",
      dep.var.caption = ""
    )
  )

cat(model.smmrys.tex, sep = "\n")

```
\end{table}

Ideally, I'd like to be able to do something like \begin{sidewaysfigure}, \end{sidewaysfigure} before and after the table. Landscape doesn't work here because AEA is twosided. Do I just have to edit the base .Tex file? (I'm fine with that, but was hoping for an Rmd solution).

Note: You have to have the AEA cls file from the rticles package in the same directory as this Rmd document in order for this to run.

Note2: I have posted on the rticles Github page

I just found out this question has already been asked here

6
  • When I use R in my research I usually just export the figures and tables that I want to include in a paper to LaTeX using xtable or ggplot2, and write the paper itself fully in LaTeX. But I guess this is opinion based, so I'll vote to close. – Marijn Jan 30 at 11:51
  • 1
    Note: if it indeed gets closed then one way to reopen it is to remove the "what is your workflow" part and focus on the specific practical issue of how to use additional packages using rticles. – Marijn Jan 30 at 11:53
  • Thanks, just did that. How do I reopen? – Daycent Jan 31 at 23:05
  • @maxIRimp Editing to get an objective question was the best thing to do. The only thing now is to wait for enough people to vote to reopen. – Teepeemm Jan 31 at 23:16
  • Got it, thank you! – Daycent Feb 1 at 4:21
1

This is what I ended up doing. I found out my question was asked here.

---
title: "BLAH"
month: "`r format(Sys.Date(), '%m')`"
year: "`r format(Sys.Date(), '%Y')`"
author:
  - name: Myself
    firstname: Nobody
    surname: Owens
    email: nobody.owens@blob.edu
    affiliation: some institute of technology
acknowledgements: |
  Thank you to my humble research assistant for his research assistance.
abstract: |
  This is the abstract.

output: rticles::aea_article

header-includes: 
 - \usepackage{graphicx} # THIS REALLY IS THE KEY HERE
 - \usepackage{rotating}
 - \usepackage{pdflscape}
 - \usepackage{dpfloat}
 - \usepackage{biblatex}
---

```{r setup, include=FALSE}
# this R code imports my data from the .R file: (of course for this online example I don't have a file to show)

knitr::opts_chunk$set(echo = FALSE, warning = FALSE)

# IMPORT PACKAGES-------------
list.of.packages <- c("dplyr", "data.table","ggplot2", "kableExtra", "tinytex", "stargazer", "R.utils")
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[, "Package"])]
if (length(new.packages)) install.packages(new.packages)
for (package in list.of.packages) {library(eval((package)), character.only = TRUE)}

# LOAD THE DATA FROM THE .R FILE---------
# load(".RData")
```
Blah blah blah...

This works, because it's a small table.

```{r, echo = FALSE, warning = FALSE}
# some table with kableExtra package:
# just an example...
a <- data.frame(a = c("orcs","orcs","elves", "elves", "elves"), b = c(1,2,3,4,5), c = c(1,2,3,4,5))

a %>% kbl(
  .,
  booktabs = TRUE,
  linesep = "",
  caption = "Model Summary Statistics",
  label = "meansd"
) %>%
  kableExtra::column_spec(column = 2:3, width = "0.5in") %>%
  kableExtra::collapse_rows(columns = 1,
                            valign = "middle",
                            longtable_clean_cut = FALSE) %>%
  kable_styling(
    font_size = 4,
    latex_options =
      c("hold_position",
        "condensed")
  )
```

But what if I have a big one...?


\begin{sidewaystable} # AND THEN YOU CAN EDIT THE RMD FILE LIKE SO
\caption{My Regressions}
\label{tab:nobodyowensregressions}
```{r, results = "asis"}

# this is some code from my original .R file that gets saved into the .RData file:

panelfit <- list()
for (i_ in c(1:10)){
panelfit[[i_]] <- lm(b~c, data = a)
}

model.smmrys.tex <- 
  R.utils::captureOutput(
    stargazer(
      panelfit,
      type="latex",
      font.size = "footnotesize",
      style = "aer",
      float = FALSE,
      column.sep.width = "-12pt",
      df = FALSE,
      dep.var.labels = "",
      dep.var.caption = ""
    )
  )

cat(model.smmrys.tex, sep = "\n")

```
\end{sidewaystable}
3
  • 1
    It's great that you found an answer, but link-only answers on a different site aren't really that helpful to others. And there are numerous answers to that question. Could you summarize what you did to get things working and add it as an answer here. Thanks! – Alan Munn Feb 8 at 16:26
  • thanks, just edited! – Daycent Feb 8 at 17:00
  • +1 thanks a lot! – Alan Munn Feb 8 at 17:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.