【问题标题】:wrapping text from apa_table() in r markdown?在 r markdown 中包装来自 apa_table() 的文本?
【发布时间】:2021-10-03 02:58:28
【问题描述】:

*为简化示例而编辑

我正在尝试使用 apa_table() 来包含一份包含排除标准的表格,用于审查论文。我已经在 excel 中制作了表格,但我正在尝试将 Rstudio 和 Rmarkdown 用于我所有的学术写作。

表格在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...

我在 python 和网状结构中感觉更舒服,我想我可以将 Pandas 数据框传递给 R 并制作表格,但是当我这样做时,针织文档会显示如下表格:

我也刚刚尝试过使用markdown文本,结果……更接近了

我使用的代码是

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
)

我还阅读了this page on the apa_table() arguments 并且没有任何运气。任何帮助将不胜感激。

【问题讨论】:

  • 您使用的是 PDF 输出格式吗?如果是这样,您也许可以使用 LaTeX 对齐设置。对于段落对齐,您可能可以使用 p{4cm} 或类似的。这里描述了其他可能性:en.wikibooks.org/wiki/LaTeX/Tables#Text_wrapping_in_tables。我会演示,但您没有包含任何可重现的内容...
  • 我试着像你一样编辑它,这能让你证明你的意思吗?
  • 对不起,我看不懂你写的。您应该简化示例:转储所有不相关的 Python 内容,然后在 R 中创建一个数据框,其中包含一些您想要格式化的文本。将所有内容放在一个任何人都可以运行的简单 R Markdown 文档中。
  • 我现在已经把它转换成R了,谢谢你的建议。
  • 那不是 R Markdown。当我将它放入 R Markdown 文档时,它无法运行。你没有说apa_table 函数来自哪里(我猜是papaja 包,但它不在CRAN 上,所以我应该安装哪个版本?),当我从Github 安装一个时,由于缺少LaTeX 而失败环境等。我投票关闭。

标签: r r-markdown papaja


【解决方案1】:

papaja manual 中,有一个对此类情况有用的技巧:Fixed-width columns

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

诀窍是通过参数align 为每列指定一个固定宽度。在这里,我为第一列(您的行名)指定 3 厘米,为每个正确的列指定 3.6 厘米(p 表示在每个表格单元格的顶部垂直对齐,但有other options)。只是为了让整个事情变得更漂亮,我通过指定font_size = "footnotesize" 使用较小的字体。

PDF 文档中的输出如下所示:

【讨论】:

  • 你是最高级别的英雄。谢谢。
  • 你能给我指出一个更好的对齐语法解释器吗?例如,如果我想将第一列设置为 3 厘米,第二列设置为 1 厘米并居中,其余的设置为 3.6,该怎么办?我在解析语法以更好地控制表格时遇到了一些麻烦。
【解决方案2】:

回答以防万一这有助于我的职位。

根据上述 cmets 的反馈,我找到了适合我需要的解决方案。我敢肯定,对 R 和 LaTex 更胜任的人可以提出比这更好的解决方案,但这对我有用。

我先运行apa_table()函数来看看输出


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
)

这会产生输出

"\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..ie .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. 出版物类型和同行评审期刊文章和非同行评审期刊的来源(即专业出版物) & 确保收录的质量...只要给定计划/干预的经验证据,而不是...\\\n3。以英语发表或提供的语言和文章以及以英语不可用的文章 & 无法使用翻译服务\\\n4.计划类型 & 明确包含...的计划 & 不包含...的干预 & 确保审查重点关注基于英雄的干预的结果\\\n\bottomrule\n\end{tabular}\n\n \end{threeparttable}\n\end{center}\n\n\end{table}\n\n\n"

然后,我使用this helpful page 以及原始问题的 cmets 中提供的资源将那堵文字墙编辑为 tex rmarkdown 块

\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}

基本上,这只是从字符串输出中删除 unicode 以使其成为 LaTex 代码,然后编辑 Latex 代码以获得所需的输出。

我确信这个答案和问题一样糟糕,但希望它可以帮助到那里的人。

[

【讨论】:

    猜你喜欢
    • 2018-04-30
    • 1970-01-01
    • 2017-07-09
    • 2023-03-20
    • 2016-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多