count: false class: title-slide, center, middle # Using PrismJS with {`xaringan`} via `prismr` .pull-left[
[`prismr` (Github repo)](https://github.com/adamoshen/prismr) ] .pull-right[
[@adamoshen](https://github.com/adamoshen)
[@adamoshen](https://twitter.com/adamoshen) ] --- class: language-r # PrismJS works with `fansi` knit hooks ```r library(tidyverse) ``` <PRE class="fansi fansi-message"><CODE>## -- <span style='font-weight: bold;'>Attaching packages</span> --------------------------------------- tidyverse 1.3.1 -- </CODE></PRE><PRE class="fansi fansi-message"><CODE>## <span style='color: #00BB00;'>v</span> <span style='color: #0000BB;'>ggplot2</span> 3.3.6 <span style='color: #00BB00;'>v</span> <span style='color: #0000BB;'>purrr </span> 0.3.4 ## <span style='color: #00BB00;'>v</span> <span style='color: #0000BB;'>tibble </span> 3.1.6 <span style='color: #00BB00;'>v</span> <span style='color: #0000BB;'>dplyr </span> 1.0.9 ## <span style='color: #00BB00;'>v</span> <span style='color: #0000BB;'>tidyr </span> 1.2.0 <span style='color: #00BB00;'>v</span> <span style='color: #0000BB;'>stringr</span> 1.4.0 ## <span style='color: #00BB00;'>v</span> <span style='color: #0000BB;'>readr </span> 2.1.2 <span style='color: #00BB00;'>v</span> <span style='color: #0000BB;'>forcats</span> 0.5.1 </CODE></PRE><PRE class="fansi fansi-message"><CODE>## -- <span style='font-weight: bold;'>Conflicts</span> ------------------------------------------ tidyverse_conflicts() -- ## <span style='color: #BB0000;'>x</span> <span style='color: #0000BB;'>dplyr</span>::<span style='color: #00BB00;'>filter()</span> masks <span style='color: #0000BB;'>stats</span>::filter() ## <span style='color: #BB0000;'>x</span> <span style='color: #0000BB;'>dplyr</span>::<span style='color: #00BB00;'>lag()</span> masks <span style='color: #0000BB;'>stats</span>::lag() </CODE></PRE> --- class: language-r # PrismJS works with `fansi` knit hooks ```r tidyverse_logo() ``` <PRE class="fansi fansi-output"><CODE>## <span style='color: #BB0000;'>*</span><span style='color: #0000BB;'> __ _ __ </span><span style='color: #BBBB00;'>.</span><span style='color: #0000BB;'> </span><span style='color: #00BB00;'>o</span><span style='color: #0000BB;'> </span><span style='color: #BB00BB;'>*</span><span style='color: #0000BB;'> </span><span style='color: #00BBBB;'>.</span><span style='color: #0000BB;'> </span> ## <span style='color: #0000BB;'> / /_(_)__/ /_ ___ _____ _______ ___ </span> ## <span style='color: #0000BB;'>/ __/ / _ / // / |/ / -_) __(_-</ -_)</span> ## <span style='color: #0000BB;'>\__/_/\_,_/\_, /|___/\__/_/ /___/\__/ </span> ## <span style='color: #0000BB;'> </span><span style='color: #BBBB00;'>*</span><span style='color: #0000BB;'> </span><span style='color: #00BB00;'>.</span><span style='color: #0000BB;'> /___/ </span><span style='color: #BBBBBB;'>o</span><span style='color: #0000BB;'> </span><span style='color: #BB00BB;'>.</span><span style='color: #0000BB;'> </span><span style='color: #00BBBB;'>*</span><span style='color: #0000BB;'> </span> </CODE></PRE> --- class: language-r # Example code and output ```r new_penguins <- palmerpenguins::penguins %>% drop_na() %>% mutate(bill_length_mean1 = mean(bill_length_mm)) %>% group_by(species, island) %>% mutate(bill_length_mean2 = mean(bill_length_mm)) %>% ungroup() %>% select(species, island, contains("bill_length")) new_penguins ``` <PRE class="fansi fansi-output"><CODE>## <span style='color: #555555;'># A tibble: 333 x 5</span> ## species island bill_length_mm bill_length_mean1 bill_length_mean2 ## <span style='color: #555555; font-style: italic;'><fct></span> <span style='color: #555555; font-style: italic;'><fct></span> <span style='color: #555555; font-style: italic;'><dbl></span> <span style='color: #555555; font-style: italic;'><dbl></span> <span style='color: #555555; font-style: italic;'><dbl></span> ## <span style='color: #555555;'> 1</span> Adelie Torgersen 39.1 44.0 39.0 ## <span style='color: #555555;'> 2</span> Adelie Torgersen 39.5 44.0 39.0 ## <span style='color: #555555;'> 3</span> Adelie Torgersen 40.3 44.0 39.0 ## <span style='color: #555555;'> 4</span> Adelie Torgersen 36.7 44.0 39.0 ## <span style='color: #555555;'> 5</span> Adelie Torgersen 39.3 44.0 39.0 ## <span style='color: #555555;'> 6</span> Adelie Torgersen 38.9 44.0 39.0 ## <span style='color: #555555;'> 7</span> Adelie Torgersen 39.2 44.0 39.0 ## <span style='color: #555555;'> 8</span> Adelie Torgersen 41.1 44.0 39.0 ## <span style='color: #555555;'> 9</span> Adelie Torgersen 38.6 44.0 39.0 ## <span style='color: #555555;'>10</span> Adelie Torgersen 34.6 44.0 39.0 ## <span style='color: #555555;'># ... with 323 more rows</span> </CODE></PRE> --- # Highlighting code inline without defining a slide class You can also highlight inline code using the usual dot-classes, with the class being "language-r". So I thought I'd use .language-r[`dplyr::group_by()`] followed by .language-r[`dplyr::mutate()`]. Of course, afterward I would also .language-r[`dplyr::ungroup()`]. You can also use the class "language-none" to disable highlighting of inline code. So I thought I'd use .language-none[`dplyr::group_by()`] followed by .language-none[`dplyr::mutate()`]. Of course, afterward I would also .language-none[`dplyr::ungroup()`]. --- # You may be interested in using the `layout` feature Recall that you can define slide classes for all subsequent slides using `layout`. As usual, you can also combine classes together. ``` class: center, inverse, language-r layout: true ``` Read more about slide layouts [
](https://github.com/yihui/xaringan/wiki/Slide-layouts). --- class: language-r # Limitations One known limitation is that adding comments will highlight the remainder of the chunk as a comment [
](https://github.com/adamoshen/prismr/issues/2). ```r 1 + 2 # Everything past this point will be highlighted as a comment mean(c(1, 4, 5, 6)) data(starwars, package="dplyr") ``` Solutions: - Only include comments on the last line, or adjacent to the last line of code - Don't include comments in your code chunk - Start a new code chunk after commenting - Use plain text on the slide to explain your code - Line highlighting still works though (see next slide) --- class: language-r # This is something complicated .pull-left[ ```r ggplot( * mpg, aes(x = displ, y = hwy, colour = class) ) + geom_point() ``` ] .pull-right[ Construct a plot using the .language-none[`mpg`] data set. ] -- .pull-left[ ```r ggplot( mpg, * aes(x = displ, y = hwy, colour = class) ) + geom_point() ``` ] .pull-right[ The x-axis will represent engine displacement. The y-axis will represent highway miles per gallon. Geoms will be coloured by type of vehicle ] -- .pull-left[ ```r ggplot( mpg, aes(x = displ, y = hwy, colour = class) ) + * geom_point() ``` ] .pull-right[ Draw points on the canvas. ]