且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

R Markdown PowerPoint幻灯片自定义

更新时间:2023-02-14 10:55:37

您可以通过创建具有创建的自定义主幻灯片布局的参考 PowerPoint文件来调整生成的PowerPoint文件的布局。



这将显示以下视图:





单击插入版式(功能区的左端附近)以创建新的幻灯片版式,它将成为我们的边对边图片版式。然后单击插入占位符下拉列表,然后选择图片。然后单击并拖动以在我们刚刚创建的幻灯片版式中添加一个边缘到边缘的图片占位符。您还可以将幻灯片标题框上移,并使用 Home 菜单将其设置为左对齐。现在,新的幻灯片布局看起来像这样:





单击幻灯片母版(功能区的左端),然后单击关闭母版(功能区的右端)。然后将文件另存为 my_template.pptx



现在编译以下文档:

  --- 
标题:无标题
作者: 2018年4月
日期: 4/9 / 2019
输出:
powerpoint_presentation:
reference_doc:template.pptx
---

```{r setup,include = FALSE}
knitr :: opts_chunk $ set(echo = FALSE)
```

##滑动并绘图

```{r压力,图.asp = 0.618}
par(mar = c(4,4,0.1,0.1))
曲线图(压力)
```

这就是幻灯片的外观:





这不是我们想要的。让我们尝试使用不同的宽高比 fig.asp = 0.5





那更好,尽管分辨率很差。因此,让我们设置 dpi 块参数,以使块头现在为:

 ```{r pressure,fig.asp = 0.5,dpi = 300} 

这为我们提供了以下PowerPoint幻灯片,其中的线条和文字更加清晰:




---
title: "Untitled"
author: "April 2018"
date: "4/9/2019"
output: powerpoint_presentation
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```

## Slide with Plot

```{r pressure, fig.width=30, fig.asp=0.618, out.width="200%"}
plot(pressure)
```

I'm reading R Studio's guide to creating PowerPoint presentations in R Markdown. I'd like to be able to customize my slides to include two things:

  1. Stretch the image size to the full width of the slide
  2. Move the title up a bit and left align it

After doing some internet searches I just can't determine how to do this properly. Maybe you know how. Here's what I'm looking for (image below). Adjusting the output width doesn't seem to help much.

You can adjust the layout of the resulting powerpoint file by creating a "reference" powerpoint file that has custom master slide layouts that you create. The Templates section of the RStudio document you linked to explains how to do this.

The basic idea is to open a new powerpoint file and customize the master slide styles, or use an existing powerpoint document or template that either already has the styles you want or that can be tweaked to get what you want. Save that customized file in your RStudio project folder (or to some other path that you can reference in the YAML header) and then reference it the YAML header. If this reference document is called my_template.pptx, then the YAML header would look like this:

---
title: "Untitled"
author: "April 2018"
date: "4/9/2019"
output: 
  powerpoint_presentation:
    reference_doc: my_template.pptx
---

For your case, I knitted the document you provided to file called test.pptx and used that as my starting document to create the desired template. After opening that document, I opened the "slide master":

This brings up the following view:

Click on Insert Layout (near left end of ribbon) to create a new slide layout, which will become our edge-to-edge picture layout. Then click on the Insert Placeholder dropdown and select Picture. Then click-drag to add an edge-to-edge picture placeholder in the slide layout we just created. You can also move the slide title box higher and, using the Home menu, set it to be left-justified. The new slide layout now looks like this:

Click Slide Master (left end of ribbon) and then click Close Master (on the right end of the ribbon). Then save the file as my_template.pptx.

Now compile the following document:

---
title: "Untitled"
author: "April 2018"
date: "4/9/2019"
output: 
  powerpoint_presentation:
    reference_doc: template.pptx
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```

## Slide with Plot

```{r pressure, fig.asp=0.618}
par(mar=c(4,4,0.1,0.1))
plot(pressure)
```

And this is what the powerpoint slide looks like:

That's not quite what we wanted. Let's try a different aspect ratio of fig.asp=0.5:

That's better, although the resolution is poor. So let's set the dpi chunk argument so that the chunk header is now the following:

```{r pressure, fig.asp=0.5, dpi=300}

which gives us the following powerpoint slide with much sharper lines and text: