rstudio / rticles

LaTeX Journal Article Templates for R Markdown
https://pkgs.rstudio.com/rticles/
1.47k stars 518 forks source link

How to load additional packages with R::rticles? #371

Closed iamgyang closed 3 years ago

iamgyang commented 3 years ago

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? I have posted in Tex stack exchange here

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.

session info: xfun::session_info() R version 4.0.3 (2020-10-10) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 19042), RStudio 1.2.5001

Locale: LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252
LC_MONETARY=English_United States.1252 LC_NUMERIC=C
LC_TIME=English_United States.1252

Package version: abind_1.4-5 askpass_1.1 assertthat_0.2.1
backports_1.2.0 base64enc_0.1-3 bdsmatrix_1.3-4
bestglm_0.37.3 betareg_3.1-3 BH_1.75.0.0
bitops_1.0-6 boot_1.3.25 brio_1.1.1
broom_0.7.3 callr_3.5.1 car_3.0-10
carData_3.0-4 caret_6.0-86 cellranger_1.1.0
checkmate_2.0.0 class_7.3-17 cli_2.2.0
clipr_0.7.1 cluster_2.1.0 codetools_0.2-16
collapse_1.5.1 colorspace_2.0-0 commonmark_1.7
compiler_4.0.3 conquer_1.0.2 countrycode_1.2.0
cowplot_1.1.1 cpp11_0.2.5 crayon_1.3.4
curl_4.3 data.table_1.13.6 DBI_1.1.1
desc_1.2.0 diffobj_0.3.3 digest_0.6.27
dplyr_1.0.3 dummies_1.5.6 ellipsis_0.3.1
evaluate_0.14 fansi_0.4.2 farver_2.0.3
flexmix_2.3-17 forcats_0.5.0 foreach_1.5.1
foreign_0.8-80 Formula_1.2-4 fst_0.9.4
furrr_0.2.1 future_1.21.0 gbRd_0.4-11
generics_0.1.0 ggplot2_3.3.3 ggrepel_0.9.1
ggthemes_4.2.4 glmnet_4.1 globals_0.14.0
glue_1.4.2 gower_0.2.2 graphics_4.0.3
grDevices_4.0.3 grid_4.0.3 gridExtra_2.3
grpreg_3.3.0 gtable_0.3.0 gvlma_1.0.0.3
h2o_3.32.0.1 haven_2.3.1 highr_0.8
Hmisc_4.4-2 hms_1.0.0 htmlTable_2.1.0
htmltools_0.5.1.1 htmlwidgets_1.5.3 httr_1.4.2
huxtable_5.1.1 ipred_0.9-9 isoband_0.2.3
iterators_1.0.13 jpeg_0.1-8.1 jsonlite_1.7.2
jtools_2.1.2 kableExtra_1.3.1 KernSmooth_2.23.17
knitr_1.30 labeling_0.4.2 LambertW_0.6.6
lamW_2.0.0 lattice_0.20-41 latticeExtra_0.6-29
lava_1.6.8.1 leaps_3.1 lifecycle_0.2.0
listenv_0.8.0 lme4_1.1.26 lmtest_0.9-38
lubridate_1.7.9.2 magrittr_2.0.1 maptools_1.0.2
margins_0.3.26 markdown_1.1 MASS_7.3-53
Matrix_1.2-18 MatrixModels_0.4-1 matrixStats_0.57.0
maxLik_1.4-6 memoise_1.1.0 methods_4.0.3
mgcv_1.8.33 mime_0.9 minqa_1.2.4
miscTools_0.6-26 modeldata_0.1.0 ModelMetrics_1.2.2.2
modeltools_0.2-23 multcomp_1.4-15 munsell_0.5.0
mvtnorm_1.1-1 nlme_3.1-149 nloptr_1.2.2.2
nnet_7.3-14 numDeriv_2016.8.1.1 openssl_1.4.3
openxlsx_4.2.3 pander_0.6.3 parallel_4.0.3
parallelly_1.23.0 pbapply_1.4.3 pbkrtest_0.5.0.1
pillar_1.4.7 pkgbuild_1.2.0 pkgconfig_2.0.3
pkgload_1.1.0 plm_2.4-0 pls_2.7-3
plyr_1.8.6 png_0.1-7 polspline_1.1.19
praise_1.0.0 prediction_0.3.14 prettyunits_1.1.1
pROC_1.17.0.1 processx_3.4.5 prodlim_2019.11.13
progress_1.2.2 ps_1.5.0 purrr_0.3.4
quantmod_0.4.18 quantreg_5.83 R.methodsS3_1.8.1
R.oo_1.24.0 R.utils_2.10.1 R6_2.5.0
rbibutils_2.0 RColorBrewer_1.1-2 Rcpp_1.0.6
RcppArmadillo_0.10.1.2.2 RcppEigen_0.3.3.9.1 RcppParallel_5.0.2
RCurl_1.98-1.2 Rdpack_2.1 readr_1.4.0
readxl_1.3.1 recipes_0.1.15 rematch_1.0.1
rematch2_2.1.2 reshape2_1.4.4 ResourceSelection_0.3-5 rio_0.5.16 rlang_0.4.10 rmarkdown_2.6
rms_6.1-0 rpart_4.1-15 rprojroot_2.0.2
rsample_0.0.8 rstudioapi_0.13 rticles_0.18
rvest_0.3.6 sandwich_3.0-0 scales_1.1.1
selectr_0.4.2 shape_1.4.5 slider_0.1.5
sp_1.4.5 SparseM_1.78 splines_4.0.3
SQUAREM_2021.1 stargazer_5.2.2 statmod_1.4.35
stats_4.0.3 stats4_4.0.3 stringi_1.5.3
stringr_1.4.0 survival_3.2-7 sys_3.4
testthat_3.0.1 TH.data_1.0-10 tibble_3.0.5
tidyr_1.1.2 tidyselect_1.1.0 timeDate_3043.102
tinytex_0.29 tools_4.0.3 TTR_0.24.2
utf8_1.1.4 utils_4.0.3 vctrs_0.3.6
viridis_0.5.1 viridisLite_0.3.0 waldo_0.2.3
warp_0.2.0 wbstats_1.0.4 webshot_0.5.2
withr_2.4.0 xfun_0.20 xml2_1.3.2
xtable_1.8-4 xts_0.12.1 yaml_2.2.1
zip_2.1.1 zoo_1.8-8


By filing an issue to this repo, I promise that

I understand that my issue may be closed if I don't fulfill my promises.

github-actions[bot] commented 3 years ago

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.