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

11

Extras

1 / 12

RMarkdown to R and back again

1

2 / 12

RMarkdown to R and back again

When you have RMarkdown scripts you want to make to R

knitr::purl("01-bakeoff-report.Rmd",
documentation = 1)
3 / 12

documentation = An integer specifying the level of documentation to add to the tangled script. 0 means to output pure code, discarding all text chunks); 1 (the default) means to add the chunk headers to the code; 2 means to add all text chunks to code as roxygen comments. roxygen is used in packages

RMarkdown to R and back again

When you have RMarkdown scripts you want to make to R

knitr::purl("01-bakeoff-report.Rmd",
documentation = 1)

When you have R script you want to make to RMarkdown

❗ Note this will over-write scripts if they have the same name.

knitr::spin("spin-bakeoff-report.R",
knit = FALSE,
format = "Rmd")
3 / 12

documentation = An integer specifying the level of documentation to add to the tangled script. 0 means to output pure code, discarding all text chunks); 1 (the default) means to add the chunk headers to the code; 2 means to add all text chunks to code as roxygen comments. roxygen is used in packages

Special hash and quote used from royxgen. Shortcuts need to be added manually but can be search and replaced from an R document.

https://github.com/yihui/knitr/blob/master/inst/examples/knitr-spin.R

Also shortcut for comment and uncomment is the same Ctrl + Shift + c

Knitting many documents

2

4 / 12

Knitting many documents

List the Rmd files:

files <- list.files(pattern = "[.]Rmd$")

Or list the Rmd files in a folder:

files <- list.files(path = "03-images/",
pattern = "[.]Rmd$")

Use for loop code to run all the files:

purrr::map(
.x = files,
.f = ~ rmarkdown::render(glue::glue(.x))
)

loop in a subfolder

purrr::map(
.x = files,
.f = ~ rmarkdown::render(glue::glue("03-images/", .x))
)
5 / 12

Source files

3

6 / 12

Source files

Instead of using import data code in the RMarkdown it can be "sourced" from another script.

source("R/source_file_csv.R")
7 / 12

To show this use the 01-bakeoff-report.Rmd and replace the import-data section with this code. The params$series don't need to be removed as this will still work.

Using SQL chunks

4

8 / 12

Using SQL chunks

Screenshot of the add chunk short menu from the Editor bar

9 / 12

Using SQL chunks

Show and tell for this SQL code in an RMarkdown

  • Open file sql-chunks.Rmd.

  • This file will knit but will just return code - there is no connection.

  • Add the code echo = FALSE to the first chunk under Default connection.

SQL chunks options are supported the same options as R and can also be set globally.

10 / 12

SQL Summary points

11 / 12

SQL Summary points

✔️ It's not just R RMarkdown combines text and other code (like SQL)

11 / 12

SQL Summary points

✔️ It's not just R RMarkdown combines text and other code (like SQL)

✔️ Temporary tables - but in R Creating tables of data in R is like creating temporary tables in SQL

11 / 12

SQL Summary points

✔️ It's not just R RMarkdown combines text and other code (like SQL)

✔️ Temporary tables - but in R Creating tables of data in R is like creating temporary tables in SQL

✔️ Use SQL power R uses RAM so make use of the power of SQL servers

11 / 12

SQL Summary points

✔️ It's not just R RMarkdown combines text and other code (like SQL)

✔️ Temporary tables - but in R Creating tables of data in R is like creating temporary tables in SQL

✔️ Use SQL power R uses RAM so make use of the power of SQL servers

Each temporary table is an object Unlike in SQL you cannot create a temporary table and then refer to it in later code. You have to use CTEs

11 / 12

SQL Summary points

✔️ It's not just R RMarkdown combines text and other code (like SQL)

✔️ Temporary tables - but in R Creating tables of data in R is like creating temporary tables in SQL

✔️ Use SQL power R uses RAM so make use of the power of SQL servers

Each temporary table is an object Unlike in SQL you cannot create a temporary table and then refer to it in later code. You have to use CTEs

nvarchar(max) Some tables in SQL use nvarchar(max), varchar(max) or binary which cause your code to fail.
The solution is to do a SELECT statement with the column at the end

11 / 12

SQL Summary points

✔️ It's not just R RMarkdown combines text and other code (like SQL)

✔️ Temporary tables - but in R Creating tables of data in R is like creating temporary tables in SQL

✔️ Use SQL power R uses RAM so make use of the power of SQL servers

Each temporary table is an object Unlike in SQL you cannot create a temporary table and then refer to it in later code. You have to use CTEs

nvarchar(max) Some tables in SQL use nvarchar(max), varchar(max) or binary which cause your code to fail.
The solution is to do a SELECT statement with the column at the end

Debugging is hard Errors don't come through from SQL so can be hard to debug from RStudio

11 / 12

SQL Summary points

✔️ It's not just R RMarkdown combines text and other code (like SQL)

✔️ Temporary tables - but in R Creating tables of data in R is like creating temporary tables in SQL

✔️ Use SQL power R uses RAM so make use of the power of SQL servers

Each temporary table is an object Unlike in SQL you cannot create a temporary table and then refer to it in later code. You have to use CTEs

nvarchar(max) Some tables in SQL use nvarchar(max), varchar(max) or binary which cause your code to fail.
The solution is to do a SELECT statement with the column at the end

Debugging is hard Errors don't come through from SQL so can be hard to debug from RStudio

Case sensitivity Microsoft SQL is not case sensitive and R is.

11 / 12

And that's the end...

Special thanks to Alison Hill, Emi Tanaka and Desirée De Leon for the original slides and content

More special thanks to NHS-R Community and Hansel Palencia for the RMarkdown workshop run at the 2021 Conference

12 / 12

RMarkdown to R and back again

1

2 / 12
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
Esc Back to slideshow