+ - 0:00:00
Notes for current slide
Notes for next slide

BANL 6100: Business Analytics

R Notebooks

Mehmet Balcilar

Univeristy of New Haven

2023-08-28 (updated: 2023-09-25)

1 / 27

R Notebooks

2 / 27

Getting Work Done

Notebook Recap

notebook demo

3 / 27

Notebook Recap

  • Notebooks are R Markdown documents + new interaction model
  • Code chunks are executed individually
  • Code output appears beneath the chunk and is saved with the document
  • Combines iterative approach with reproducible result

If you've never used them before, watch the R Notebook Webinar

webinar

4 / 27

document outline

5 / 27

jump to chunk

6 / 27

prev/next chunk

7 / 27

nav-running

8 / 27

Output Management

output-management

9 / 27

Reproducibility

  • Notebooks can be less reproducible than traditional R Markdown documents.
  • Chunks can be run in any order.
  • Chunks can access the global environment.

Simulating a Knit

restart and run

10 / 27

Consistency

run chunks above

11 / 27

How R Notebooks Really Work

12 / 27

Input and Output

notebookio cluster0 Notebook HTML (.nb.html) doc R Markdown (.Rmd) rs RStudio doc->rs save Save doc->save execute Execute Chunks doc->execute cache Local Output Cache cache->rs cache->save edoc R Markdown ecache Embedded Output Cache save->edoc save->ecache execute->cache
13 / 27

Input and Output

  1. When you execute chunks in your notebook, the output is simultaneously displayed by RStudio and stored in a local cache in the .Rproj.user folder.
  2. When you save your notebook, the output and your R Markdown document are combined into a single, self-contained .nb.html file.
14 / 27

Publishing and Collaborating

15 / 27

Publishing

Two choices:

  1. Publish the notebook file directly (HTML)
  2. Render to a separate output format

Publishing: Notebook HTML

  • Self-contained
  • Compatible with any hosting service
  • No viewer required
  • Hydrates to full notebook (more on this later)
16 / 27

Publishing: Another Format

---
title: "A Monte-Carlo Analysis of Monte Carlo"
output:
html_notebook: default
pdf_document:
fig_width: 9
fig_height: 5
---
17 / 27

Understanding Multiple Formats

notebookio cluster1 Develop cluster2 Present doc R Markdown (.Rmd) execute Execute Chunks doc->execute knit Knit doc->knit cache Local Output Cache save Save cache->save nb Compiled Notebook (.nb.html) pdf PDF Publication save->nb execute->cache knit->pdf
18 / 27

Publishing: RStudio Connect

  • One-click publish inside your org
  • Fine-grained access control
  • Execute on the server
  • Schedule executions

Collaborating: Plain Files

To: analyst@contoso.com
From: customer@northwind.com
I need some help with this analysis. Could you
take a look at what I have so far?
Thanks,
Charles
[ Attachment: foo.nb.html ]
19 / 27

Collaborating: Execution in Reverse

notebookio cluster0 Notebook HTML (.nb.html) doc R Markdown (.Rmd) rs RStudio doc->rs cache Local Output Cache cache->rs edoc R Markdown open Open in RStudio edoc->open ecache Embedded Output Cache view View in Browser ecache->view open->doc open->cache
20 / 27

Collaborating: Opening Code in RStudio

download R markdown

21 / 27

Collaborating: Opening Notebook in RStudio

save as notebook

22 / 27

Collaborating: Open Inside RStudio

pre open

23 / 27

Collaborating: Open Notebook

hydrated

24 / 27

Collaborating: Version Control #1

  • Add *.nb.html to your .gitignore or similar
  • Check in only the .Rmd file
  • All diffs are plain text
  • Encourages reproducibility & independent verification of results

Collaborating: Version Control #2

  • Check in the .nb.html file and the .Rmd file
  • Diffs are noisier
  • RStudio loads outputs from .nb.html if newer
  • Outputs and inputs are versioned together
  • No need to re-execute lengthy or fragile computations
25 / 27

Versioned Output

When a notebook is opened:

  • The local cache modified time is compared to the .nb.html
  • If .nb.html is older, it is ignored
  • If .nb.html is newer, it replaces the local cache
  • No merging or conflict management is performed!
26 / 27

The End

27 / 27

R Notebooks

2 / 27
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
sToggle scribble toolbox
Esc Back to slideshow