如何使RStudio自动编译R Markdown小插图? [英] How to get RStudio to automatically compile R Markdown Vignettes?

查看:122
本文介绍了如何使RStudio自动编译R Markdown小插图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用R Markdown编写R Package vignettes.我正在使用R Studio的程序包创作工具.

I am trying to write R Package vignettes using R Markdown. I am using R Studio's package authoring tools.

我的R大于3.0版.

我在vignettes文件夹中有一个.Rmd文件,该文件的顶部包含以下文本:

I have an .Rmd file in the vignettes folder that contains the following text at the top:

<!--
%\VignetteEngine{knitr::knitr}
%\VignetteIndexEntry{An Introduction to the bootcorrelations package}
-->

我的DESCRIPTION文件中包含以下内容:

I have the following in my DESCRIPTION file:

VignetteBuilder: knitr
Suggests: knitr

当我在RStudio中清理并生成或重新加载程序包时,会显示小插图源,但不会显示HTML(即inst/man中没有HTML文件).

When I clean and build or build and reload the package in RStudio, the vignette source is displayed, but not the HTML (i.e., there's no HTML file in inst/man).

如何让RStudio从R Markdown插图自动创建HTML?

我已阅读 Yihui在Markdown上的R Package Vignettes上的帖子,它建议使用makefile,但是最近的有关knitr晕影的文档建议不再需要makefile.

I've read through Yihui's post on R Package Vignettes with Markdown and it suggests using a makefile, but this more recent documentation about knitr vignettes suggests that the makefile is no longer required.

我还意识到我可以使用以下命令手动创建HTML小插图:

I also realise that I could manually create the HTML vignette with a command like this:

library(knitr)
knit(input='vignettes/foo.Rmd', output='inst/doc/foo.md')
library(markdown)
markdownToHTML('inst/doc/foo.md', 'inst/doc/foo.html')


可复制的示例:


A reproducible example:

Vectorize(dir.create)(c("test", "test/R", "test/man", "test/vignettes"))

cat(
  'Package: test
Title: Test pkg
Description: Investigate how to auto-compile markdown vignettes
Version: 0.0-1
Date: 2015-03-15
Author: Jeromy Anglim
Maintainer: Jeromy Anglim <a@b.com>
Suggests: knitr
License: Unlimited
VignetteBuilder: knitr',
  file = "test/DESCRIPTION"
)

cat(
  '---
title: "Introduction"
author: "Jeromy Anglim"
date: "`r Sys.Date()`"
output: html_document
---

<!--
%\\VignetteEngine{knitr::rmarkdown}
%\\VignetteIndexEntry{Introduction}
-->

# Introduction

A sample vignette!

```{r}
1 + 1
```',
  file = "test/vignettes/intro.Rmd"
)

cat(
  "#' Nothing
#' This function is only needed so that roxygen generates a NAMESPACE file.
#' @export
nothing <- function() 0",
  file = "test/R/nothing.R"
)

library(roxygen2)
library(devtools)

roxygenise("test")
build("test")

推荐答案

更新:横向考虑,至少有三个选择.

Update: Thinking laterally, there are at least three options.

正如@Hadley指出的那样,从devtools程序包运行build_vignettes()将会构建小插图,并将其放置在程序包的inst/man目录中.

As @Hadley points out, running build_vignettes() from the devtools package will build vignettes and place them in the inst/man directory of the package.

devtools::build_vignettes()

构建小插图意味着您可以在inst/man中获得三个版本:

Building the vignette means you get three versions in inst/man:

  1. Rmd源
  2. 编织或编织的HTML小插图
  3. 以及代码块中的R代码

这与RStudio中的build and reload命令无关,但是它是该任务的单行解决方案.而且可以轻松地将其合并到makefile.

This is not tied to the build and reload command in RStudio, but it is a one line solution to the task. And it could easily be incorporated into a makefile.

正如@TylerRinker指出的那样,您只能在Rstudio中使用Knit HTML.这会将rmd小插图的md和HTML编织版本都添加到vignettes目录中.

As @TylerRinker notes you can just use Knit HTML in Rstudio. This will add both md and HTML knitted versions of the rmd vignette to the vignettes directory.

这也不与构建过程相关,但正如@TylerRinker指出的那样,通常您不希望将小插图与主构建过程相关联.尽管 http://htmlpreview.github,它也可以为您提供一个md文件,这是在github上显示小插图的不错选择. com/是在github上显示HTML小插图的选项.

This also is not tied to the build process but as @TylerRinker points out, often you don't want the vignettes tied to the main build process. This also has the benefit of giving you an md file which can be a nice option for displaying the vignette on github, although http://htmlpreview.github.com/ is an option for displaying HTML vignette on github.

Build - Build Source Package对应于R CMD build.运行时,会创建一个压缩的tar.gz文件,该文件在inst/doc目录中包含原始rmd小插图文件的Rmd,R和HTML文件.

Build - Build Source Package in RStudio corresponds to R CMD build. When run a compressed tar.gz file is created which contains in the inst/doc directory the Rmd, R and HTML files for the original rmd vignette file.

有关编写程序包小插图的官方文档中对此进行了描述指示您使用R CMD build生成PDF和HTML小插图.

This is described in the official documentation on writing package vignettes which instructs you to use R CMD build to generate PDF and HTML vignettes.

因此有可能

  1. 构建源
  2. 解压缩tar.gz文件
  3. 浏览并打开生成的文件

这篇关于如何使RStudio自动编译R Markdown小插图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆