214

I am trying to understand at a high level what the differences between R Markdown and R NoteBook. I know they are interrelated but I would like to figure out how they are related. My understanding is this:

I know R Notebooks are really R Markdown documents but I am confused about the terminologies.

RStudio new file option has both R Markdown and R NoteBook and selecting either of them opens an R Markdown file but with minor differences. So just trying to figure out why are there two options and what is the difference between the two?

enter image description here

I understand R Notebooks are newly introduced and R Markdown has been there for a while. Part of the confusion could be because I never used R Markdown before R Notebook was introduced so my related or more specific question is

What is R NoteBook doing differently than just using R MarkDown or what new capabilities is R NoteBook bringing?

All my hits on web search are pointing that R Notebook uses R Markdown but I did not find any help on what is specifically different between the two.

Heikki
  • 2,214
  • 19
  • 34
PagMax
  • 8,088
  • 8
  • 25
  • 40
  • 1
    I use knitr and LaTeX, so I choose the R Sweave option. Not having used RMarkdown, all I can say is that LaTex can do anything, but that power brings with it complexity and a learning curve. Also, there is a huge community of supporters and ample books and resources. – lawyeR May 07 '17 at 00:36
  • 1
    thanks @lawyeR. I am not worried about resources and I can find my way around R and LaTeX fairly well. I did wonder what Sweave was but I thought I will worry about it later. Thanks for your insights on that. for now I am just looking at top level differences between these various flavors of R. – PagMax May 07 '17 at 03:28
  • 9
    And while you are a great fan of reading the manual, how about reading the actual question which you downvoted and answer specific to what is asked in the question instead of dumping material from the documentation ? – PagMax May 13 '17 at 01:30

9 Answers9

101

As far as I understand and from my setup there is no coding difference. The difference is in the rendering. The file extension is the same.

When you make a new R Notebook it adds html_notebook in the output option in the header. That's the difference. You can then preview the rendering quickly without having to knit it. It also refreshes the preview every time you save. However in that preview you don't have the code output (no figures, no tables..) (at least in my setup). Without html_notebook in the output there is no button preview

enter image description here

as you can see the Preview options shows up but you can also knit it in any format you want. It will add it to the header code when you do so.

enter image description here

However if you don't have that html_notebook in your header, you can only knit your code to see what it looks like (the entire book) (please ignore the additional default option I put in with the picture)

enter image description here

and the option to preview doesn't show in the drop down menu

enter image description here

Otherwise it works the same. For some default configuration the output is also hidden by default in the code section.

Note that you can mix several output options in your header so that you can keep the preview and keep your knit options for export.

Heikki
  • 2,214
  • 19
  • 34
R. Prost
  • 1,958
  • 1
  • 16
  • 21
  • 2
    as an add-on there is a bug with UTC (taking into accounts accents and non English characters) with the notebook. It will screw up your output and your file... so better stick to markdown... This point is still being addressed (as far as I know) – R. Prost Feb 15 '18 at 09:19
  • 6
    Your answer is absolutely what I think the difference is after using it for few months. What beats me is R Notebook is everything and above what R MarkDown is. Why continue with R Markdown then? You can use R notebook and directly "knit" instead of "preview" and you have your R Markdown functionality !! – PagMax Feb 15 '18 at 13:12
  • 2
    More a problem of legacy I think, so as not to confuse people already using Rmarkdown. Plus the current version of notebook (or R) has that bug I mention above which prevents me from using it basically. My text is replaced with gibberish in the source file too depending on which way round I use (from rmardown to notebook or notebook to rmarkdown). So I am sticking to markdown until I can fix that. Especially that the notebook preview is not that useful for me, just previewing the text without the plots etc.. but it has a lot of potential. – R. Prost Feb 16 '18 at 08:20
  • 3
    When you use a r notebook you _definitely_ get all the outputs in the preview, graphs (dynamic and static), tables, etc... This answer should be amended to remove the statement saying you do not get it. R notebook is largely superior because of this, you can get all the output with recompiling every time, because the preview remembers the last output of you chunk. It enables you to write ITERATIVELY (which is always what you need) – statquant Oct 04 '20 at 09:49
88

Recently I found this post which made me clear on the R Markdown vs. R Notebook issue. http://uc-r.github.io/r_notebook

Here are a few relevant lines:

Writing an R Notebook document is no different than writing an R Markdown document. The text and code chunk syntax does not differ from what you learned in the R Markdown tutorial. The primary difference is in the interativeness of an R Notebook. Primarily that when executing chunks in an R Markdown document, all the code is sent to the console at once, but in an R Notebook, only one line at a time is sent. This allows execution to stop if a line raises an error.

Also there is this on knit vs. preview when you create a R Notebook in RStudio:

The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike Knit, Preview does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed.

Hope you find it useful.

Mani Tajaddini
  • 881
  • 6
  • 4
33

One of the most important differences is not completely clear from the above answers.

From Section 3.2.1.3 of the Bookdown book:

There is also a Restart R and Run All Chunks item in the Run menu on the editor toolbar, which gives you a fresh R session prior to running all the chunks. This is similar to the Knit button, which launches a separate R session to compile the document.

In other words, knitting creates a new environment and runs all the code there. By way of contrast, the R Notebook uses the Global Environment as is to render the HTML file. In fact, for an R Notebook, changes to the HTML file happen every time the .Rmd document is saved. The Preview button merely opens the HTML file in its current state. No code is run. Preview literally means what it says: it just shows you what has already been done.

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.

Sean Raleigh
  • 579
  • 4
  • 10
11

The difference is that in R Notebok you can write Markdown and R-code Chunks and directly execute them and see results right away. It is kind of a Notebook for yourself.

R Markdown is there to create nice looking outputs in html, pdf or a word Document. Combining Markdown and R-Code-Chunks. A short introduction and motivation can be found here.

If you want to get more infos about the the knitr Package klick here.

Uwe
  • 41,420
  • 11
  • 90
  • 134
Swolf
  • 329
  • 2
  • 7
  • 2
    Well this still does not explain how is Markdown different from Notebook. (I understand notebook uses markdown) but R markdown existed before R notebooks where introduced last year. So what new features are in R notebook which were earlier not possible by just using R markdown? – PagMax May 11 '17 at 05:27
  • 3
    @PagMax I think the difference lies in the speed in rendering your html output from your Rmarkdown file. When you want use knitr to produce a html output of your R markdown, it takes a bit of time to "knit" all your code and generate the output. To speed it up, you can view your Rmarkdown in html using Rnotebook "mode" in Rstudio by changing your output to "html_notebook", it lets you execute a re-run a chunk , then do ctrl+shift+k (pc) and the html refreshes very quickly. – chang02_23 Jul 17 '17 at 23:29
  • 1
    I think the problem the OP is having is that there is not really a huge difference between the 2. The main difference I see, which I picked up on mostly from reading the answers here, is that RNB allows you to execute single chunks of R code independently and "in line" while RMD requires you to either render or not render the entire file. Imagine if you had a huge file and just needed to check on little chart. RNB is good for that. RMD might take too long to render and is thus inconvenient. – Bryan Goggin Sep 08 '17 at 00:47
  • 1
    @BryanGoggin You can still execute the independent chunks in Rmd as well. – PagMax Feb 05 '18 at 04:42
8

Similarites: The R notebook and R Markdown document are pretty much similar. Markdown format works in both file types. Both the file formats can be used for communicating code along with results and commentary to others. One can choose to knit the created document to HTML, PDF and WORD output formats. If there is an error in the code, output will not be generated. To run the file: CTRL+SHIFT+K and to insert code chunk: CTRL+ALT+I.

R Markdown document: The Rmd document can be configured via a wizard and has options for reproducibilty document, presentation slides, shiny app etc and an option to create document from a template. If you change the code, the ouput is executed in the Rmd output. It does not offer the option to Preview the document. The YAML header has output format as: 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 header : If we change the YAML header from one file to another, it will change the type of the document. For instance, you have a R notebook that has YAML header as html_notebook. If you change the YAML header, your R notebook will be converted to R Markdown document.

If you have to choose one, go with RMarkdown document as it offers more control and updates documents as soon as you knit it.

Saurabh Jain
  • 1,600
  • 1
  • 20
  • 30
7

Here's one practical difference I found :

When you need to comment out a part of text, RMarkdown inserts # in every line of your text, whereas RNotebook neatly encapsulates text between a single pair of <!-- , ---> signs (as in html files)

One may say, what a trivial difference. However, just because of it, I now always use R Notebook (instead of RMarkdown) in RStudio for all my documents. In everything else, they seem the same (for my needs, at least, such as generating html's and pdf's)

Otherwise, my comments suddenly become headers,like below:

# This part is commented out, but will show as Heading 1 font text
IVIM
  • 2,167
  • 1
  • 15
  • 41
  • `R Markdown` also uses `` to comment textual chunks. Code chunks only are commented using `#`. My `RStudio`'s version is 1.3.1056. – Rafs Oct 14 '20 at 13:58
4

http://rmarkdown.rstudio.com/r_notebooks.html#notebook_file

"Ordinary R Markdown documents are “knit”, but notebooks are “previewed”. While the notebook preview looks similar to a rendered R Markdown document, the notebook preview does not execute any of your R code chunks"

SJ9
  • 477
  • 4
  • 13
  • 4
    I agree. The "preview" feature is the only differentiator between the two. I wonder why they hang on to Rmarkdown when Rnotebook is everything what Rmarkdown is with an additional preview feature. – PagMax Feb 05 '18 at 04:39
3

From jrnold:

R notebook files show the output inside the editor, while hiding the console. R markdown files shows the output inside the console, and does not show output inside the editor. They differ in the value of output in their YAML headers.
The YAML header for the R notebook will have the line,

---
ouptut: html_notebook
---

The YAML header for the R markdown file will have the line,

ouptut: html_document

Copying the YAML header from an R notebook to a R markdown file changes it to an R markdown file, and vice-versa. More specifically, changing the value of output to This is because the RStudio IDE when opening and the rmarkdown package when knitting uses the YAML header of a file, and in particular the value of the output key in the YAML header, to determine what type of document it is.

Yang Dai
  • 43
  • 1
  • 7
-1

An R file contains R programming language code within it, naturally.

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.

soka1228
  • 29
  • 3