创建 R Markdown 文档Create R Markdown documents

R Markdown 是一种文档格式,可将 R 中的分析转化为高质量的文档、报告、演示文稿和仪表板。R Markdown is a document format that turns analysis in R into high-quality documents, reports, presentations, and dashboards.

针对 Visual Studio 的 R 工具 (RTVS) 提供了 R Markdown 项模板、编辑器支持(包括编辑器中适用于 R 代码的 IntelliSense)、文件生成功能和实时预览。R Tools for Visual Studio (RTVS) provides a R Markdown item template, editor support (including IntelliSense for R code within the editor), file generation capabilities, and live preview.

使用 R MarkdownUsing R Markdown

  1. 关闭 Visual Studio。Close Visual Studio.

  2. (仅一次)从 pandoc.org 安装 pandoc(One time only) Install pandoc from pandoc.org.

  3. 重启 Visual Studio,它应该获取 pandoc 安装程序。Restart Visual Studio, which should pick up the pandoc installation.

  4. 安装 knitrrmarkdown 包,可从交互窗口执行安装操作:Install the knitr and rmarkdown packages, which you can do from the interactive window:

    install.packages("knitr")
    install.packages("rmarkdown")
    
    
  5. 要创建新的 R Markdown 文件,请使用“文件” > “新建” > “文件”菜单命令,并在列表中选择”R” > ”R Markdown”。Create a new R Markdown file using the File > New > File menu command and selecting R > R Markdown from the list. 访问项目的上下文,在解决方案资源管理器中右键单击项目,选择“添加 R Markdown”(或“添加” > “新项目”,再在列表中选择”R Markdown”)。In the context of a project, right-click the project in Solution Explorer and select Add R Markdown (or Add > New Item and selecting R Markdown from the list).

  6. 新文件的默认内容如下:The default contents of the new file are as follows:

    ---
    title: "Untitled"
    output: html_document
    ---
    
    This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and Microsoft Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
    
    When you click the **R Tools | Publish | Preview** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
    
    ```{r}
    summary(cars)
    ```
    
    You can also embed plots, for example:
    
    ```{r, echo=FALSE}
    plot(cars)
    ```
    
    Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
    
    

预览版Previews

Visual Studio 2017 版本 15.5 以及更高版本自动提供 R Markdown 的实时预览。Visual Studio 2017 version 15.5 and later automatically provide live preview for R Markdown. 要启用编辑器与预览之间的自动同步,请选择“R 工具” > “Markdown” > “自动同步”(Ctrl+Shift+Y)。To turn on automatic sync between the editor and the preview, select R Tools > Markdown > Automatic Sync (Ctrl+Shift+Y). 如果没有使用自动同步,可使用“R 工具” > “Markdown” > “重载 R Markdown 预览”来刷新预览。If you aren't using automatic sync, you can refresh the preview using R Tools > Markdown > Reload R Markdown Preview.

还可以通过在编辑器中右键单击并选择一个“预览”命令,在 HTML、PDF 和 Microsoft Word 格式中预览文件。You can also preview the file in HTML, PDF, and Microsoft Word formats by right-clicking in the editor and selecting one of the Preview commands. 还可通过“R 工具” > “Markdown”菜单获取相同命令。The same commands are also available on the R Tools > Markdown menu. (在早期版本的 Visual Studio 中,这些命令位于“R 工具” > “发布”菜单。)(In earlier versions of Visual Studio these commands are found on the R Tools > Publish menu.)

R Markdown 实时预览和其他预览菜单命令