1

*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.

KevOMalley743
  • 551
  • 3
  • 20
  • 1
    Are you using PDF output format? If so, you may be able to use LaTeX alignment settings. For paragraph alignment you might be able to use `p{4cm}` or similar. There are other possibilities described here: https://en.wikibooks.org/wiki/LaTeX/Tables#Text_wrapping_in_tables . I'd demonstrate, but you haven't included anything reproducible... – user2554330 Jul 27 '21 at 09:37
  • I've tried to edit it as you, would this make it possible for you to demonstrate what you mean? – KevOMalley743 Jul 27 '21 at 11:11
  • 1
    Sorry, I can't understand what you wrote. You should simplify the example: dump all the irrelevant Python stuff, and just create a dataframe in R that contains some text you want formatted. Put it all in a simple R Markdown document that anyone can run. – user2554330 Jul 27 '21 at 11:13
  • I've converted it to R now, thanks for the advice. – KevOMalley743 Jul 27 '21 at 11:28
  • 1
    That's not R Markdown. When I put it in an R Markdown document it fails to run. You don't say where the `apa_table` function came from (I guessed the `papaja` package, but it's not on CRAN, so which version should I install?), when I install one from Github, it fails because of missing LaTeX environments, etc. I'm voting to close. – user2554330 Jul 27 '21 at 12:00
  • Well, thanks again for your input. It did give me the idea to just run the `apa_table()` function and play with the LaTex output. Sorry I'm not skilled enough with R to be able to ask the question clearly. – KevOMalley743 Jul 27 '21 at 13:52

2 Answers2

2

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

Marius Barth
  • 596
  • 2
  • 9
  • 1
    you are a hero of the highest order. Thank you. – KevOMalley743 Jul 27 '21 at 21:53
  • Could you point me to a better explainer of the align syntax? What if, for example, I want to set the first column to be 3cm, the second to be 1 cm and centered and the rest at 3.6? I'm having some trouble parsing out the syntax to get more control over the table. – KevOMalley743 Aug 12 '21 at 07:49
0

Answering just in case this helps someone who was in my position.

Based on feedback from comments above I have found a solution that works for what I need. I'm sure someone more competent with R and LaTex could have produced a much better solution than this, but this has worked for me.

I first ran the apa_table() function to take a look at the output


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')

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

this produces the output

"\n\n\n\begin{table}[tbp]\n\n\begin{center}\n\begin{threeparttable}\n\n\caption{test caption}\n\n\begin{tabular}{llll}\n\toprule\n & \multicolumn{1}{c}{inclusion....c..Peer.reviewed.Journal.articles....Research.Empirical.articles.presenting.methods.and.results.suitable.for.analysis.and.synthesis...} & \multicolumn{1}{c}{exclusion....c..Sources.other.than.peer.reviewed.journals..i.e..professional.publications....} & \multicolumn{1}{c}{rationale....c..To.ensure.the.quality.of.included.......To.evaluate.only.empirical.evidence.for.a.given.program.intervention.rather.than......}\\\n\midrule\n1. Publication type & Peer reviewed Journal articles & Sources other than peer reviewed journals (i.e. professional publications) & To ensure the quality of included...\\\n2. 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 only empirical evidence for a given program/intervention rather than...\\\n3. Language & Articles published or available in English & Articles unavailable in English & No access to translation service\\\n4. Program type & Programs that explicitly include... & Interventions that do not include a... & To ensure the review focuses on the outcomes of hero based interventions\\\n\bottomrule\n\end{tabular}\n\n\end{threeparttable}\n\end{center}\n\n\end{table}\n\n\n"

I then used this helpful page along with the resources provided in the comments on the original question to edit that wall of text in to a tex rmarkdown chunk

\begin{table}[tbp]
\begin{center}
\begin{threeparttable}
\caption{test caption}
\begin{tabular}{p{4cm} p{4cm} p{4cm} p{4cm}}
\hline
\toprule & \multicolumn{1}{c}{Inclusion} & \multicolumn{1}{c}{Exclusion} & \multicolumn{1}{c}{Rationale}\\
\hline
\midrule 1. Publication type & Peer reviewed Journal articles & Sources other than peer reviewed journals (i.e. professional publications) & To ensure the quality of included...\\
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 only empirical evidence for a given program/intervention rather than...\\
3. Language & Articles published or available in English & Articles unavailable in English & No access to translation service\\
4. Program type & Programs that explicitly include... & Interventions that do not include a... & To ensure the review focuses on the outcomes of hero based interventions\\
\bottomrule
\hline
\end{tabular}
\end{threeparttable}
\end{center}
\end{table}

Basically, this was just removing the unicode from the string output to make it into LaTex code and then editing the latex code to get the desired output.

I'm sure this answer is just as bad as the question but hopefully it helps someone out there.

[LaTex edite chunk1

KevOMalley743
  • 551
  • 3
  • 20