我试图在一个高水平上理解R Markdown和R NoteBook之间的区别。我知道它们是相互关联的,但我想弄清楚它们是如何相互关联的。我的理解是:

我知道R笔记本实际上是R Markdown文档,但我对术语感到困惑。

RStudio新文件选项有R Markdown和R NoteBook,选择其中任何一个打开R Markdown文件,但有微小的差异。我只是想弄清楚为什么会有两种选择这两者之间有什么区别?

我知道R笔记本电脑是新推出的,R Markdown也已经有一段时间了。部分困惑可能是因为在R笔记本推出之前我从未使用过R Markdown,所以我的相关或更具体的问题是

R NoteBook与仅仅使用R MarkDown有什么不同,或者R NoteBook带来了什么新功能?

我在网上搜索的所有结果都指向R Notebook使用了R Markdown,但我没有找到任何关于两者之间具体区别的帮助。


当前回答

相似点:R笔记本和R Markdown文档非常相似。Markdown格式适用于两种文件类型。这两种文件格式都可以用于与他人交流代码以及结果和注释。您可以选择将创建的文档编织成HTML、PDF和WORD输出格式。如果代码中有错误,则不会生成输出。运行文件:CTRL+SHIFT+K,插入代码块:CTRL+ALT+I。

R Markdown文档:Rmd文档可以通过向导进行配置,并具有可复制文档、演示幻灯片、闪亮应用程序等选项,以及从模板创建文档的选项。如果更改代码,输出将在Rmd输出中执行。它没有提供预览文档的选项。YAML头的输出格式为:output: html_document

R Notebook Document: The R nb document is launched directly and no wizard (as in the case of .RMD) appears. Along with the notebook file, an additional html file that extension *.nb.html is generated. The notebook has the option for Preview. If any code is altered or edited, the new output is not shown. The output is shown in the code editor itself. Whatever the old output was it is only rendered. No new output is generated from the code change. In order to show the code output, we need to execute the chunk and then it will appear in the output. The YAML header has output as: output: html_notebook

YAML头:如果我们将YAML头从一个文件更改到另一个文件,它将改变文档的类型。例如,您有一个R笔记本,它的YAML头名为html_notebook。如果你改变了YAML标题,你的R笔记本将被转换为R Markdown文档。

如果你必须选择一个,选择RMarkdown文档,因为它提供了更多的控制和更新文档,只要你编织它。

其他回答

从jrnold:

R笔记本文件在编辑器中显示输出,同时隐藏控制台。R markdown文件显示控制台内的输出,而不显示编辑器内的输出。它们的YAML头中的输出值不同。 R笔记本的YAML标题将有这样一行,

---
ouptut: html_notebook
---

R markdown文件的YAML头将有一行,

ouptut: html_document

将YAML头从R笔记本复制到R markdown文件将其更改为R markdown文件,反之亦然。更具体地说,将output的值更改为This是因为RStudio IDE在打开文件时使用YAML头,编织时使用rmarkdown包,特别是YAML头中的输出键的值,以确定它是什么类型的文档。

最近我发现了一篇文章,让我清楚了R Markdown和R Notebook的问题。 http://uc-r.github.io/r_notebook

以下是一些相关的句子:

编写R Notebook文档与编写R Markdown文档没有什么不同。文本和代码块的语法与您在R Markdown教程中学到的没有什么不同。主要的区别在于R笔记本的互动性。主要是当在R Markdown文档中执行块时,所有的代码都被一次性发送到控制台,但在R Notebook中,一次只发送一行。这允许在某行引发错误时停止执行。

当你在RStudio中创建R Notebook时,也有这个关于编织和预览的问题:

预览显示了编辑器内容的HTML副本。因此,与Knit不同,Preview不运行任何R代码块。相反,将显示上一次在编辑器中运行时的数据块的输出。

希望对你有用。

最重要的区别之一并不是完全清楚从上面的答案。

从Bookdown书的3.2.1.3节:

在运行菜单中还有一个“重新启动R”和“运行所有块”项 在编辑器工具栏上,它为您提供了一个新的R会话 运行所有的块。这类似于Knit按钮 启动一个单独的R会话来编译文档。

换句话说,编织创建了一个新环境,并在那里运行所有代码。相比之下,R Notebook使用全局环境来呈现HTML文件。事实上,对于R Notebook,每次保存. rmd文档时都会对HTML文件进行更改。预览按钮只是在当前状态下打开HTML文件。没有代码运行。预览的字面意思是:它只是告诉你已经做了什么。

Why does this matter? For example, if an R Notebook .Rmd file is opened, but no code chunks are run, then the HTML file will render all the markdown and input code just fine, but no output will appear. Or, suppose you define some variable x in the Console, but not in a code chunk. If you try to use x somewhere in an R Notebook, it will work just fine. Previewing the HTML document will also work just fine. On the other hand, knitting the document will generate an "unknown variable" error because knitting runs all the code in a new environment, and the variable x was never defined in the markdown file anywhere.

据我所知,从我的设置没有编码差异。 区别在于渲染。文件扩展名相同。

当你创建一个新的R Notebook时,它会在头文件的输出选项中添加html_notebook。这就是区别。然后,您可以快速预览渲染,而不必编织它。它还会在每次保存时刷新预览。然而,在预览中,你没有代码输出(没有图形,没有表格..)(至少在我的设置中)。输出中没有html_notebook,就没有按钮预览

正如你可以看到预览选项显示,但你也可以编织在任何你想要的格式。当您这样做时,它将把它添加到头代码中。

然而,如果你的标题中没有html_notebook,你只能编织你的代码来看看它看起来像什么(整本书)(请忽略我在图片中添加的额外默认选项)

下拉菜单中没有显示预览选项

否则它的工作原理是一样的。对于某些默认配置,输出在默认情况下也隐藏在代码部分中。

注意,你可以在你的标题中混合几个输出选项,这样你就可以保持预览,并保持你的针织选项用于导出。

当然,R文件中包含R编程语言代码。

R Markdown, on the other hand, is a Markdown file ("Markdown is a lightweight markup language for creating formatted text using a plain-text editor" per daringfireball.net/projects/markdown) with R code chunks embedded within it. R code chunks afford the option of running the whole chunk or a single line within RStudio. Even more important is when you go to export your R Markdown file you will see that the Markdown text shows up as text, while R code chunks are grayed and show up as a code block by default. RMarkdown is meant to make R more dynamic, especially for the purposes of creating reports. R Markdown has been used extensively by the scientific community for the purposes of generating reports.