Coder Social home page Coder Social logo

crsh / papaja Goto Github PK

View Code? Open in Web Editor NEW
643.0 26.0 132.0 16.27 MB

papaja (Preparing APA Journal Articles) is an R package that provides document formats to produce complete APA manuscripts from RMarkdown-files (PDF and Word documents) and helper functions that facilitate reporting statistics, tables, and plots.

Home Page: https://frederikaust.com/papaja_man/

License: Other

R 38.51% TeX 10.26% Lua 1.06% Shell 0.12% JavaScript 2.05% HTML 48.01%
rmarkdown apa journal apa-guidelines psychology r manuscript reproducible-paper reproducible-research r-package

papaja's Introduction

papaja: Prepare APA Journal Articles
with R Markdown

CRAN/METACRAN Project Status: WIP - Initial development is in progress, but there has not yet been a stable, usable release suitable for the public. GitHub last commit (main) R-CMD-check codecov GitHub bug issues StackOverflow questions

papaja is an award-winning R package that facilitates creating computationally reproducible, submission-ready manuscripts which conform to the American Psychological Association (APA) manuscript guidelines (6th Edition). papaja provides

  • an R Markdown template that can be used with (or without) RStudio to create PDF documents (using the apa6 LaTeX class) or Word documents (using a .docx-reference file).
  • Functions to typeset the results from statistical analyses,
  • functions to create tables, and
  • functions to create figures in accordance with APA guidelines.

For a comprehensive introduction to papaja, see the current draft of the manual. If you have a specific question that is not answered in the manual, feel free to ask a question on Stack Overflow using the papaja tag. If you believe you have found a bug or would like to request a new feature, open an issue on Github and provide a minimal complete verifiable example.

Example

Take a look at the source file of the package vignette and the resulting PDF. The vignette also contains some basic instructions.

Installation

To use papaja you need either a recent version of RStudio or pandoc. If you want to create PDF- in addition to DOCX-documents you additionally need a TeX distribution. We recommend you use TinyTex, which can be installed from within R:

if(!requireNamespace("tinytex", quietly = TRUE)) install.packages("tinytex")

tinytex::install_tinytex()

You may also consider MikTeX for Windows, MacTeX for Mac, or TeX Live for Linux. Please refer to the papaja manual for detailed installation instructions.

papaja is available on CRAN but you can also install it from the GitHub repository:

# Install latest CRAN release
install.packages("papaja")

# Install remotes package if necessary
if(!requireNamespace("remotes", quietly = TRUE)) install.packages("remotes")

# Install the stable development version from GitHub
remotes::install_github("crsh/papaja")

Usage

Once papaja is installed, you can select the APA template when creating a new R Markdown file through the RStudio menus.

APA template selection dialog

APA template selection dialog

To add citations, specify your bibliography-file in the YAML front matter of the document (bibliography: my.bib) and start citing (for details, see pandoc manual on the citeproc extension. You may also be interested in citr, an R Studio addin to swiftly insert Markdown citations and R Studio’s visual editor, which also enables swiftly inserting citations.

Typeset analysis results

The functions apa_print() and apa_table() facilitate reporting results of your analyses. When you pass the an output object of a supported class, such as an htest- or lm-object, to apa_print(), it will return a list of character strings that you can use to report the results of your analysis.

my_lm <- lm(
  Sepal.Width ~ Sepal.Length + Petal.Width + Petal.Length
  , data = iris
)
apa_lm <- apa_print(my_lm)

apa_lm$full_result$Sepal_Length
## [1] "$b = 0.61$, 95\\% CI $[0.48, 0.73]$, $t(146) = 9.77$, $p < .001$"

papaja currently provides methods for the following object classes:

A-B D-L L-S S-Z
afex_aov default lsmobj summary.aovlist
anova emmGrid manova summary.glht
anova.lme glht merMod summary.glm
Anova.mlm glm mixed summary.lm
aov htest papaja_wsci summary.manova
aovlist list summary_emm summary.ref.grid
BFBayesFactor lm summary.Anova.mlm
BFBayesFactorTop lme summary.aov

Create tables

apa_table() may be used to produce publication-ready tables in an R Markdown document. For instance, you might want to report some condition means (with standard errors).

npk |>
  # Summarize data
  dplyr::group_by(N, P) |>
  dplyr::summarise(
    mean = mean(yield)
    , se = sd(yield) / sqrt(length(yield))
    , .groups = "drop"
  ) |>
  # Label columns 
  label_variables(
    N = "Nitrogen"
    , P = "Phosphate"
    , mean = "*M*"
    , se = "*SE*"
  ) |>
  # Print table
  apa_table(caption = "Mean pea yield (with standard errors)")

Table 1. Mean pea yield (with standard errors)

Nitrogen Phosphate M SE
0 0 51.72 1.88
0 1 52.42 2.65
1 0 59.22 2.66
1 1 56.15 2.08

This is a fairly simple example, but apa_table() may be used to generate more complex tables.

apa_table(), of course, plays nicely with the output from apa_print(). Thus, it is possible to conveniently report complete regression tables, ANOVA tables, or the output from mixed-effects models.

lm(Sepal.Width ~ Sepal.Length + Petal.Width + Petal.Length, data = iris) |>
  apa_print() |>
  apa_table(caption = "Iris regression table.")

Table 2. Iris regression table.

Predictor b 95% CI t df p
Intercept 1.04 [0.51, 1.58] 3.85 146 < .001
Sepal Length 0.61 [0.48, 0.73] 9.77 146 < .001
Petal Width 0.56 [0.32, 0.80] 4.55 146 < .001
Petal Length -0.59 [-0.71, -0.46] -9.43 146 < .001

Create figures

papaja further provides functions to create publication-ready plots. For example, you can use apa_barplot(), apa_lineplot(), and apa_beeplot() (or the general function apa_factorial_plot()) to visualize the results of factorial study designs:

apa_beeplot(
  data = stroop_data
  , dv = "response_time"
  , id = "id"
  , factors = c("congruency", "load")
  , ylim = c(0, 800)
  , dispersion = wsci # within-subjects confidence intervals
  , conf.level = .99
  , las = 1
)

Response times from a simulated Stroop experiment. Large dots represent condition means, small dots represent individual participants’ mean response time. Error bars represent 99% within-subjects confidence intervals.

Response times from a simulated Stroop experiment. Large dots represent condition means, small dots represent individual participants’ mean response time. Error bars represent 99% within-subjects confidence intervals.

If you prefer ggplot2, try theme_apa().

library("ggplot2")
library("ggforce")

p <- ggplot(
  stroop_data
  , aes(x = congruency, y = response_time, shape = load, fill = load)
) +
  geom_violin(alpha = 0.2, color = grey(0.6)) +
  geom_sina(color = grey(0.6)) +
  stat_summary(position = position_dodge2(0.95), fun.data = mean_cl_normal) +
  lims(y = c(0, max(stroop_data$response_time))) +
  scale_shape_manual(values = c(21, 22)) +
  scale_fill_grey(start = 0.6, end = 1) +
  labs(
    x = "Congruency"
    , y = "Response time"
    , shape = "Cognitive load"
    , fill = "Cognitive load"
  )

p + theme_apa()
## Warning: Computation failed in `stat_summary()`
## Caused by error in `fun.data()`:
## ! The package "Hmisc" is required.

Usage without RStudio

Don’t use RStudio? No problem. Use the rmarkdown::render function to create articles:

# Create new R Markdown file
rmarkdown::draft(
  "mymanuscript.Rmd"
  , "apa6"
  , package = "papaja"
  , create_dir = FALSE
  , edit = FALSE
)

# Render manuscript
rmarkdown::render("mymanuscript.Rmd")

Getting help

StackOverflow questions

For a comprehensive introduction to papaja, check out the current draft of the papaja manual. If you have a specific question that is not answered in the manual, feel free to ask a question on Stack Overflow using the papaja tag. If you believe you have found a bug or you want to request a new feature, open an issue on Github and provide a minimal complete verifiable example.

Citation

Please cite papaja if you use it.

Aust, F. & Barth, M. (2023). papaja: Prepare reproducible APA journal articles with R Markdown. R package version 0.1.2. Retrieved from https://github.com/crsh/papaja

For convenience, you can use cite_r() or copy the reference information returned by citation('papaja') to your BibTeX file:

@Manual{,
  title = {{papaja}: {Prepare} reproducible {APA} journal articles with {R Markdown}},
  author = {Frederik Aust and Marius Barth},
  year = {2023},
  note = {R package version 0.1.2},
  url = {https://github.com/crsh/papaja},
}

papaja in the wild

If you are interested in seeing how others are using papaja, you can find a collection of papers and the corresponding R Markdown files in the manual.

If you have published a paper that was written with papaja, please add the reference to the public Zotero group yourself or send us to me.

Computational reproducibility

To ensure mid- to long-term computational reproducibility we highly recommend conserving the software environment used to write a manuscript (e.g. R and all R packages) either in a software container or a virtual machine. This way you can be sure that your R code does not break because of updates to R or any R package. For a brief primer on containers and virtual machines see the supplementary material by Klein et al. (2018).

Docker is the most widely used containerization approach. It is open source and free to use but requires some disk space. CodeOcean is a commercial service that builds on Docker, facilitates setting up and sharing containers and lets you run computations in the cloud. See the papaja manual on how to get started using papaja with Docker or CodeOcean and our Docker workflow tailored for easy use with papaja.

Contribute

GitHub help wanted issues GitHub documentation issues

Like papaja and want to contribute? We highly appreciate any contributions to the R package or its documentation. Take a look at the open issues if you need inspiration. There are many additional analyses that we would like apa_print() to support. Any new S3/S4-methods for this function are always appreciated (e.g., factanal, fa, lavaan). For a primer on adding new apa_print()-methods, see the getting-started-vignette:

vignette("extending_apa_print", package = "papaja")

Before working on a contribution, please review our brief contributing guidelines and code of conduct.

Related R packages

By now, there are a couple of R packages that provide convenience functions to facilitate the reporting of statistics in accordance with APA guidelines.

  • apa: Format output of statistical tests in R according to APA guidelines
  • APAstats: R functions for formatting results in APA style and other stuff
  • apaTables: Create American Psychological Association (APA) Style Tables
  • rempsyc: Convenience functions for psychology
  • sigr: Concise formatting of significances in R

If you are looking for other journal article templates, you may be interested in the rticles package.

Package dependencies

papaja's People

Contributors

alexholcombe avatar conig avatar crsh avatar deboerk avatar jacob-long avatar jvcasillas avatar katrinleinweber avatar mariusbarth avatar mutlusun avatar nehemie avatar setgree avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

papaja's Issues

double comma if I leave out 'note' in template

If I do not specify a note in my .Rmd template, I erroneously get two commas on the first page of the manuscript:

Correspondence concerning this article should be addressed to Donald Duck**, ,**
Gansstr. 2, 12345 Entenhausen, Germany.

I can replicate this in the example file example.Rmd of this repository if I simply remove the note:

note: |

Currently, a development version of papaja is available at https://github.com/crsh/papaja.

I do not think that this is the desired behaviour, if I do not want to insert a note?

Add sanity checks

As of now there are no sanity checks for function inputs. In order to receive meaningful warnings and errors basic sanity checking of input would be nice.

Landscape pages

Is there any possibilty to add landscape pages (i.e., for large tables) to the document? I understood that I need the pdflscape environment in TeX to do so, but including this package in the yaml header doesn't change anything in papaja documents, while these features are accepted in standard RStudio pdfs. Is there a convenient way to load additional TeX environments?

My code:
includes:
in_header: \usepackage{pdflscape}

class: jou not working

When using class: jou I get the error:

! LaTeX Error: Something's wrong--perhaps a missing \item. See the LaTeX manual or LaTeX Companion for explanation. Type H <return> for immediate help. ... l.141 pandoc.exe: Error producing PDF from TeX source Error: pandoc document conversion failed with error 43

This happens with newest papaja, R 3.2.1 and newest RStudio 0.99.442 under Windows 10 with MikTeX and Ubuntu 14.04 with TeX Live. All documents I tried work fine with class: doc and class: man. This error is reproducible with the standard example document. Please correct me if I'm missing something here.

Hanging indents for bibliography

My understanding is that current APA style requires the bibliography to use hanging indentation (i.e. the first line is normal, all the following lines are indented, for each reference). I didn't see an option to change this in the documentation.

Character encoding of abstract and note sections in MS Word

When knitting the standard papaja template to a .docx file non-ascii characters (I tested ÄÜÖäüö߀) in the abstract and the note sections of the word document are seemingly not encoded correctly. The same characters are displayed properly when used in the title or outside the header in the normal document text. Characters are also displayed properly in the two sections when knitting a pdf file.

I am using MikTex 2.9.5105 and the latest development version of papaja. To view the .docx file I use MS Word 2013.

Confidence intervals for eta^2s

apa_print() provides confidence intervals for R² (lm) but as of now no confidence intervals are calculated for ANOVA objects.

supress NULL output of data.table plots

When printing plots from a data.table in the form DT[,plot(xy)], data.table returns NULL next to the plot. This NULL is also printed in the apa manuscript.

Example:

dat <- data.table(x=1:10,y=11:20,subj=rep(c(1,2),5))
dat[subj==1,plot(x,y)]
dat[subj==2,lines(x,y)]

apa_table

Do I need to do anything in addition to installing papaja to get the apa_table function to work? I get the following error when I try and run your code from the example document (i.e., cars descriptives):

Error in apa_table.latex(...) : could not find function "validate"

It is for this reason that I've been trying to use LaTeX code to create a table. A second problem of mine, however, is I don't know where to add \usepackage{}. For example, with the table I've tried \usepackage{adjustbox} or tabularx, as suggested by you, but I get an error.

ERROR

Fehler in rmarkdown::pdf_document(template = template, fig_caption = fig_caption, :
unbenutztes Argument (keep.tex = keep.tex)
Calls: ... create_output_format -> do.call -> ->
Ausführung angehalten

arrange_anova() throws error for summary.aov-object

When I pass an aov object from repeated-measures ANOVA to apa_print, it fails now:

Fehler in UseMethod("arrange_anova", x) :
nicht anwendbare Methode für 'arrange_anova' auf Objekt der Klasse "c('summary.aov', 'listof')" angewendet

Why did you do that?

Scaling of images in MS Word

I experienced so problems with image scaling when knitting MS Word documents. Embedded images ![](...) are scaled down and distorted (PDF) while plot generated from R are not scaled to fit the page with the custom settings. Maybe we can adjust the chunk defaults depending on the output format?

Add a blacklist to cite_r()

Currently, cite_r() has a white list of packages to cite (pkgs), but it would be probably be much more useful to have a black list of packages not to cite. This would prevent situations in which a package is later added to the analysis script but not to the call of cite_r.

Option to add significance indicators to apa_table()

It would be nice if there was an option to add significance indicators to a apa.table(), e.g. for correlation tables, by passing a matrix of the same dimensions that contains p-values (s. corr.test() from the psych-package) or by passing a matrix of the same dimensions with logicals to add indicators of something entirely different.

Reference Formattign

The references do not come out according to APA 06 (hanging indent), but rather they are indented on the first line. How would I fix this issue?

APA article created using rmarkdown::render(), not rmarkdown::draft()

Today I got papaja to run outside of RStudio in Ubuntu 14.04. It was not easy because it took me some time to install a recent version of pandoc and then call it from within R.

In the readme file of this repository you describe to use rmarkdown::draft() in order to create an APA article if you are not using RStudio [ like this: rmarkdown::draft("mymanuscript.Rmd", template = "apa6", package = "papaja") ]. This does not seem to work for me. I get this error message: The file 'mymanuscript.Rmd' already exists.

If I just use a different file name I get a weird .rmd document containing this abstract:

"abstract: In der Rede wird folgende These diskutiert: "Optozentristisch durchseucht sind Philosophie und die Wissenschaft. [...]"

It seems that draft() does not create a .pdf file, but rather tries to create a new file from a template (see http://rmarkdown.rstudio.com/developer_document_templates.html)

To create a papaja APA6 article, I needed to use the rmarkdown function render(), instead of draft(). Using rmarkdown::render(), I could replicate creating your example article as well as articles that contained changes. How to use it:

render("myExample.rmd", output_file="myExample.pdf")

tl;dr: If you are using papaja outside of RStudio, it is possible that you can only create an article by calling rmarkdown::render(), not rmarkdown::draft().

Create a doc-reference-file for MS Word

Let's see if we can create a MS Word reference-file to make Word documents look like apa6-documents when the class is set to doc. Also: Is it possible to set reference-docx in the yaml-header conditional on the selected document class?

Create classes for table output from apa_print()

If we define classes with attributes for the tables produced by apa_print() it would be possible to automatically create sensible table notes containing, e.g., R² for regression models etc.

Create unit tests

Unit tests for results of printnum(), apa.stat(), and subfunctions using testthat-package.

Explore alternate way to use apa6 document class

An alternative to delivering a hard copy of the apa6 document class, which we do right now, would be to use the version shipped with the LaTeX-distribution:


---
title: "Untitled"
author: "Author"
output: pdf_document
classoption: "man"
documentclass: "apa6"
geometry: false

---

However, this way we would need to include some additional arguments in the preamble, which could be done with includes in the yaml-header, the _output.yaml-file, or the format-function.

Rescaling of tables to fit the page

I'm having trouble with getting tables to be rescaled to fit the page. Below is the table I'm using (and I've also tried using the xtable and ztable packages as well as basic markdown tables):

\begin{table}[htbp]
\center
\caption{Original DAST-10}
\label{table1}
\begin{tabular}{cl}
\toprule
& Item
\toprule
1 & Have you used drugs other than those required for medical reasons?
2 & Do you use more than one drug at a time?
3 & Are you always able to stop using drugs when you want to? (-)
4 & Have you ever had blackouts or flashbacks as a result of drug use?
5 & Do you ever feel bad or guilty about your drug use?
6 & Does your spouse (or parents) ever complain about your involvement with drugs?
7 & Have you neglected your family because of your use of drugs?
8 & Have you engaged in illegal activities in order to obtain drugs?
9 & Have you ever experienced withdrawal symptoms (felt sick) when you stopped taking drugs?
10 & Have you had medical problems as a result of your drug use (e.g., memory loss, hepatitis, convulsions, bleeding)?
\bottomrule
\end{tabular}
\end{table}

Make apa_table() take tabular-objects

The tables packages provides a powerful interface to produce tables from the data. It would be nice if we found a way to make this work with apa.table(). As it is, tabular objects can be used to produce pretty tables in LaTeX but we would need to find a way to make it work with Markdown/Pandoc.

apa_lineplot()

Create a function that uses the same parameters as apa_barplot(), but draws nice lineplots

apa_table() doubles row names

When I use apa_table() to insert a table into a papaja::apa6_pdf document, this table will have two rows containing the row names of the matrix that I passed to apa_table() (also happens if I pass a data.frame instead). If I replicate the example from the ?apa_table() documentation, it looks like this:


Descriptives speed dist


Mean Mean 15.4 42.98

SD SD 5.29 25.77

Min Min 4 2

Max Max 25 120


This does not happen if I directly call kable() instead. I am using papaja_0.1.0.9000.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.