本文介绍了在 R Markdown 中插入图片/表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想在 R Markdown 中插入一个表格和一张图片.在普通的 Word 文档中,我可以轻松插入一个表格(5 行 x 2 列),对于图片,只需复制和粘贴即可.

So I want to insert a table AND a picture into R Markdown. In regular word document I can just easily insert a table (5 rows by 2 columns), and for the picture just copy and paste.

  1. 如何插入一个 5 行 x 2 列的表格(并在其中输入内容)(并根据第二列比第一列宽来调整表格)?

  1. How do I insert a 5 row by 2 column table (and also type stuff into them)(and also adjust the table in terms of the 2nd column being wider than the first)?

如何插入图片?根据我的理解,我应该先将图片保存为png,然后将其引用到我的文档中.另外,我想根据报告自动调整图片,比如不占用多于一页或少于一页.(有没有办法让我根据自己的喜好调整图片的大小)?

How do I insert a picture? From my understanding, I should first save the picture as a png, then reference it into my document. Also, I want to automatically adjust the picture to the report, like not taking up more than a page, or less than a page.(Is there a way for me to adjust the size of the picture to my liking)?

如果有人知道关于 R Markdown 的任何很酷的/格式化的东西,你也可以告诉我吗?我知道 # 是一个段落的大标题,而 ** ** 会加粗.我所知道的就这些!

If anyone knows anything cool/formatting about R Markdown could you also let me know? I know that # makes a big title for a paragraph, and ** ** bolds things. Thats about all I know though!

推荐答案

一些站点为表格和图像提供了合理的备忘单或 HOWTO.在我的列表中最重要的是:

Several sites provide reasonable cheat sheets or HOWTOs for tables and images. Top on my list are:

RStudio 的 RMarkdown,更多详情请见 基础(包括表格)和重写 pandoc 的降价.

RStudio's RMarkdown, more details in basics (including tables) and a rewrite of pandoc's markdown.

图片使用起来非常简单,但不提供调整图片以适应页面的功能(请参阅下面的更新).要调整图像属性(大小、分辨率、颜色、边框等),您需要某种形式的图像编辑器.我发现我可以使用 ImageMagickGIMPInkScape,全部免费且开源.

Pictures are very simple to use but do not offer the ability to adjust the image to fit the page (see Update, below). To adjust the image properties (size, resolution, colors, border, etc), you'll need some form of image editor. I find I can do everything I need with one of ImageMagick, GIMP, or InkScape, all free and open source.

要添加图片,请使用:

![Caption for the picture.](/path/to/image.png)

我知道 pandoc 支持 PNG 和 JPG,它们应该可以满足您的大部分需求.

I know pandoc supports PNG and JPG, which should meet most of your needs.

如果您在 R 中创建图像(例如,绘图),您确实可以控制图像大小.这可以直接在命令中完成以创建图像,或者如果您使用的是 knitr(强烈推荐......查看chunk options,特别是在Plots下).

You do have control over image size if you are creating it in R (e.g., a plot). This can be done either directly in the command to create the image or, even better, via options if you are using knitr (highly recommended ... check out chunk options, specifically under Plots).

我强烈建议仔细阅读这些教程;Markdown 非常方便,并且具有大多数人不经常使用的许多功能,但一旦他们学会了就会非常喜欢.(SO 不一定是在这些教程中直接回答问题的最佳场所.)

I strongly recommend perusing these tutorials; markdown is very handy and has many features most people don't use on a regular basis but really like once they learn it. (SO is not necessarily the best place to ask questions that are answered very directly in these tutorials.)

前段时间,pandoc 为图像加入了link_attributes"(显然是在 2015 年,提交 jgm/pandoc#244cd56).调整图像大小"可以直接完成.例如:

Some time ago, pandoc incorporated "link_attributes" for images (apparently in 2015, with commit jgm/pandoc#244cd56). "Resizing images" can be done directly. For example:

![unchanged image](foo.jpg)
![much-smaller image](foo.jpg){#id .class width=30 height=20px}
![half-size image](foo.jpg){#id .class width=50% height=50%}

尺寸可以不带单位(假设为像素),也可以带"pxcmmm、in、inch%"(参考:https://pandoc.org/MANUAL.html,搜索 link_attributes).

The dimensions can be provided with no units (pixels assumed), or with "px, cm, mm, in, inch and %" (ref: https://pandoc.org/MANUAL.html, search for link_attributes).

(我不确定 CommonMark 是否已经实现了这一点,尽管有一个 冗长的讨论.)

(I'm not certain that CommonMark has implemented this, though there was a lengthy discussion.)

这篇关于在 R Markdown 中插入图片/表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 03:40