Search code examples
rr-markdownpapaja

wrapping text from apa_table() in r markdown?


*Edited to simplify example

I'm trying to use apa_table() to include a table of inclusion exclusion criteria for a review paper. I have already made the table in excel but I'm trying to move into using Rstudio and Rmarkdown for all my academic writing.

The table looks like this in excel

Inclusion Exclusion Rationale
1. Publication Peer reviewed Journal Sources other than peer reviewed journals (i.e. professional publications, grey literature) To ensure the quality of...
2. Article type Research/Empirical articles presenting methods and results suitable for analysis and synthesis Articles describing interventions without presenting research findings (i.e. theraputuc manuals) To evaluate evidence for a given...
3. Language Articles published or available in English Articles unavailable in English No access to translation service
4. Program type Program that explicitely target... Interventions that do not include.... To ensure the review focuses on...

I'm much more comfortable in python and with the reticulate I figured I could just pass a Pandas dataframe to R and make the table, but when I do the knitted document shows the table like this:

knitted from code

I have also just tried to use markdown text and the result is... closer

from mdtext

the code I've used is

df <- data.frame(inclusion <- c('Peer reviewed Journal articles', 
    'Research/Empirical articles presenting methods and results suitable for analysis and synthesis',
    'Articles published or available in English',
    'Programs that explicitly include...'),
exclusion <- c('Sources other than peer reviewed journals (i.e. professional publications)',
    'Articles describing interventions without presenting research findings (i.e. theraputuc manuals)',
    'Articles unavailable in English',
    'Interventions that do not include a...'),
rationale <- c('To ensure the quality of included...',
    'To evaluate only empirical evidence for a given program/intervention rather than...',
    'No access to translation service',
    'To ensure the review focuses on the outcomes of hero based interventions')
)

rownames(df) <- c('1. Publication type',
                  '2. Article type',
                  '3. Language',
                  '4. Program type')

colnames(df) <- c('Inclusion', 'Exclusion', 'Rationale')

library(papaja)
apa_table(
  df
  , caption = "Inclusion/Exclusion Criteria"
  , note = NULL
)

I have also read through this page on the apa_table() arguments and haven't had any luck. Any help would be appreciated.


Solution

  • In the papaja manual, there is a helpful trick for cases like this: Fixed-width columns

    apa_table(
      df
      , caption = "Inclusion/Exclusion Criteria"
      , align = c("p{3cm}", rep("p{3.6cm}", ncol(df)))
      , font_size = "footnotesize"
    )
    

    The trick is to specify a fixed width for each column via argument align. Here, I specify 3cm for the first column (your row names), and 3.6cm each for each proper column (the p means vertically aligned at the top of each table cell, but there are other options). Just to make the whole thing even more beautiful, I use a smaller font by specifying font_size = "footnotesize".

    The output in a PDF document then looks like the following: enter image description here