Difference between R MarkDown and R NoteBook Difference between R MarkDown and R NoteBook r r

Difference between R MarkDown and R NoteBook


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.


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.


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.