我主要用 R 编写代码,并且已经使用 knitr Rnw 一段时间了,主要遵循 14mech14 的 YouTube 中概述的最佳实践视频,其中包含一个父 rnw 文档、一个子 rnw 文档以及一个包含我的 R 代码和所有表格/图形的文档。
我最近开始尝试使用 Rmd 来处理我的期刊文章,使用 rticles 包,但遇到了必须将新包加载到 Rmd 文档中的问题。我看过 Yihui 的文档,但如果我有
output: rticles::aea_article
extra_dependencies: ["rotating"]
我该如何解决这个问题?(此外,如果我的同事在 STATA 中工作,我该如何让他们也处理这篇论文?我应该在 Latex 中编写所有内容并导出吗?)
这是我的.Rmd 文件:
---
title: "BLAH"
month: "`r format(Sys.Date(), '%m')`"
year: "`r format(Sys.Date(), '%Y')`"
author:
- name: Myself
firstname: Nobody
surname: Owens
email: [email protected]
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}
理想情况下,我希望能够在表格前后执行 \begin{sidewaysfigure}、\end{sidewaysfigure} 之类的操作。横向布局在这里不起作用,因为 AEA 是双面的。我是否只需要编辑基本 .Tex 文件?(我对此没有异议,但希望有一个 Rmd 解决方案)。
注意:为了运行此功能,您必须将 rticles 包中的 AEA cls 文件放在与此 Rmd 文档相同的目录中。
注2:我已在文章中发布Github 页面
我刚刚发现这个问题已经被问过了这里
答案1
这就是我最终做的事情。我发现我的问题被问到了这里。
---
title: "BLAH"
month: "`r format(Sys.Date(), '%m')`"
year: "`r format(Sys.Date(), '%Y')`"
author:
- name: Myself
firstname: Nobody
surname: Owens
email: [email protected]
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}