Upgrade to Pro — share decks privately, control downloads, hide ads and more …

TokyoR#102_RMarkdown

kilometer
October 22, 2022

 TokyoR#102_RMarkdown

第102回Tokyo.Rの初心者セッション用スライドです。

kilometer

October 22, 2022
Tweet

More Decks by kilometer

Other Decks in Technology

Transcript

  1. #102
    @kilometer00
    2022.10.22
    R Markdown for beginners

    View full-size slide

  2. Who!?
    名前: 三村 @kilometer
    職業: ポスドク (こうがくはくし)
    専⾨: ⾏動神経科学(霊⻑類)
    脳イメージング
    医療システム⼯学
    R歴: ~ 10年ぐらい
    流⾏: 南極探検隊

    View full-size slide

  3. 宣伝!!
    (書籍の翻訳に参加しました。)
    第五刷!!

    View full-size slide

  4. BeginneR Session

    View full-size slide

  5. Beginne
    R
    Advance
    d
    Hoxo_m
    If I have seen further it is by standing on the
    shoulders of Giants.
    -- Sir Isaac Newton, 1676

    View full-size slide

  6. Before A'er
    BeginneR Session
    BeginneR BeginneR

    View full-size slide

  7. "a" != "b"
    # is A in B?
    ブール演算⼦ Boolean Algebra
    [1] TRUE
    1 %in% 10:100
    # is A in B?
    [1] FALSE

    View full-size slide

  8. George Boole
    1815 - 1864
    A Class-Room Introduc2on to Logic
    h7ps://niyamaklogic.wordpress.com/c
    ategory/laws-of-thoughts/
    Mathematician
    Philosopher
    &

    View full-size slide

  9. ブール演算⼦ Boolean Algebra
    A == B A != B
    George Boole
    1815 - 1864
    A | B A & B
    A %in% B
    # equal to # not equal to
    # or # and
    # is A in B?
    wikipedia

    View full-size slide

  10. AI Programmer !?
    import sample data
    scatter plot speed and dist as x and y axes, respectively
    import pandas as pd
    import matplotlib.pyplot as plt
    data = pd.read_csv('sample_data.csv')
    plt.scatter(data['speed'], data['dist’])
    plt.show()
    library(tidyverse)
    df <- read_csv("sample_data.csv")
    df %>%
    ggplot(aes(x = speed, y = dist)) +
    geom_point()
    https://aiprogrammer.hashlab.jp/

    View full-size slide

  11. Programing
    Write
    Run
    Read
    Think
    Write
    Run
    Read
    Think
    Communicate
    Share

    View full-size slide

  12. #102
    @kilometer00
    2022.10.22
    R Markdown for beginners

    View full-size slide

  13. Program documentation
    code
    # comment
    Input Output
    document
    (for PC)
    (for human)

    View full-size slide

  14. Let us change our traditional attitude to the construction
    of programs: Instead of imagining that our main task is to
    instruct a computer what to do, let us concentrate rather
    on explaining to humans what we want the computer to
    do.
    — Donald E. Knuth, Literate Programming, 1984
    プログラム構築に関する伝統的な態度を切り替えてみましょう。
    コンピュータに何をするべきかを指図するのが主な仕事だと考えるのではなく、
    むしろコンピュータに何をして欲しいかを⼈間に説明することに集中しよう。
    — ドナルド・E・クヌース, ⽂芸的プログラミング, 1984
    Literate Programming

    View full-size slide

  15. Literate Programming
    code
    document
    # comment
    Input Output

    View full-size slide

  16. Literate Programming in R
    Sweave
    knitr
    rmarkdown
    Quato?
    2012
    2002
    2014
    WAB → Noweb

    2021
    R Markdown: The Definitive Guide
    Yihui Xie, J. J. Allaire, Garrett Grolemund, 2022
    https://bookdown.org/yihui/rmarkdown/#ref-xie2015
    Yihui Xie
    @xieyihui

    View full-size slide

  17. R Markdown
    ① Make a new .Rmd file
    ② Write something (for human)
    ③ Knit it!!

    View full-size slide

  18. R Markdown
    ① Make a new .Rmd file
    ② Write something (for human)
    using markdown & R
    ③ Knit it!!

    View full-size slide

  19. Markdown
    A simple markup language that
    uses plain text formatting syntax
    plain text with
    markdown
    .html
    .pdf
    .docx

    View full-size slide

  20. Markup Language
    Plain text Rich text
    HTML
    = Hyper Text Markup Language Web browser

    View full-size slide

  21. Markup Language
    Plain text Rich text
    Markdown HTML

    View full-size slide

  22. Markup Language
    Plain text Rich text
    Markdown HTML
    Pandoc
    pandoc -f markdown -t html hoge.md > hoge.html

    View full-size slide

  23. Markdown
    Plain text Rich text
    *This* This
    **This** This
    ***This***
    ~~This~~ This
    This This
    This

    View full-size slide

  24. Markdown
    Plain text Rich text

    View full-size slide

  25. Markdown
    Plain text Rich text

    View full-size slide

  26. Markdown
    Plain text Rich text

    View full-size slide

  27. ```r
    library(palmerpenguins)
    penguins[1, ]
    ```
    ```
    # A tibble: 1 × 8
    species island bill_…¹ bill_…² flipp…³ body_…⁴ sex year

    1 Adelie Torger… 39.1 18.7 181 3750 male 2007
    # … with abbreviated variable names ¹bill_length_mm,
    # ²bill_depth_mm, ³flipper_length_mm, ⁴body_mass_g
    ```
    Markdown
    Plain text Rich text

    View full-size slide

  28. Write .md
    .md .r
    write code
    test code
    write document
    copy code
    paste code
    copy results
    paste results
    write document
    write document

    View full-size slide

  29. Write .Rmd
    .Rmd
    set document
    write document
    write .r
    write document
    write document
    test .r
    write .r
    test .r

    View full-size slide

  30. .Rmd .md
    .html
    .pdf
    .docx
    knitr Pandoc

    View full-size slide

  31. Write .Rmd
    .Rmd
    set document
    write document
    write .r
    write document
    write document
    test .r
    write .r
    test .r
    Peaceful world
    ・No copy & paste
    ・No window switching

    View full-size slide

  32. Write .Rmd
    .Rmd
    set document
    write document
    write .r
    write document
    write document
    test .r
    write .r
    test .r
    Peaceful world
    ・No copy & paste
    ・No window switching
    You MUST write
    docs about the R code
    You JUST write docs about
    your analysis with R code

    View full-size slide

  33. Write .Rmd
    .Rmd
    set document
    write document
    write .r
    write document
    write document
    test .r
    write .r
    test .r
    ---
    title: "Hoge"
    output:
    html_document: default
    ---
    YAML header
    (YAML Ain't a Markup Language.)

    View full-size slide

  34. Write .Rmd
    .Rmd
    set document
    write document
    write .r
    write document
    write document
    test .r
    write .r
    test .r
    ---
    title: "Hoge"
    output:
    html_document:
    keep_md: TRUE
    ---
    YAML header
    (YAML Ain't a Markup Language.)

    View full-size slide

  35. Knit .Rmd
    in English
    .Rmd → .html
    .Rmd → .pdf
    .Rmd → .docx
    Easy!! Great!!
    Comfortable! Good!!
    Okay, if you need.

    View full-size slide

  36. Knit .Rmd
    in Japanese
    .Rmd → .html
    .Rmd → .pdf
    .Rmd → .docx
    Easy!! Great!!
    Darkness
    Darknes(

    View full-size slide

  37. As soon as Anakin came before him on Coruscant,
    Yoda could sense darkness in his soul.
    --- The Phantom Menace

    View full-size slide

  38. Write .Rmd
    .Rmd
    set document
    write document
    write .r
    write document
    write document
    test .r
    write .r
    test .r
    YAML header
    for Japanese PDF
    (in my env.)
    ---
    title: "segments"
    output:
    pdf_document:
    latex_engine: xelatex
    documentclass: bxjsarticle
    classoption: xelatex,ja=standard
    mainfont: Hiragino Kaku Gothic Pro
    ---

    View full-size slide

  39. Write .Rmd
    .Rmd
    set document
    write document
    write .r
    write document
    write document
    test .r
    write .r
    test .r
    Code Chunk 1
    Code Chunk 2
    ```{r, chunk1}
    ```
    ```{r, chunk2}
    Chunk name

    View full-size slide

  40. Chunk options
    ```{r chunk1, ...}
    ```
    Language Name Options
    echo
    eval
    include
    results
    fig.width / fig.height
    collapse
    warning
    message

    View full-size slide

  41. Chunk options
    ```{r chunk1, options...}
    ```
    ```{r}
    #| chunk1
    #| options...
    ```
    =

    View full-size slide

  42. Write .Rmd
    .Rmd
    set document
    write document
    write .r
    write document
    write document
    test .r
    write .r
    test .r
    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(
    echo = TRUE
    )
    ```
    Global setting of chank options

    View full-size slide

  43. Write .Rmd
    .Rmd
    set document
    write document
    write .r
    write document
    write document
    test .r
    write .r
    test .r
    Code Chunk 1
    ```{r, chunk1}
    ```
    Run this chunk: ⌘+Shift+↩

    View full-size slide

  44. .Rmd
    set document
    write document
    write .r
    write document
    write document
    test .r
    write .py
    test .py
    ```{r pyenv, include=FALSE}
    reticulate::use_python(
    python = ".../bin/python",
    required = TRUE
    )
    ```
    Write python in .Rmd

    View full-size slide

  45. Write python in .Rmd

    View full-size slide

  46. Write python in .Rmd
    https://speakerdeck.com/kilometer

    View full-size slide

  47. In summary ...

    View full-size slide

  48. R Markdown
    ① Make a new .Rmd file
    ② Write something (for human)
    using markdown & R
    ③ Knit it!!

    View full-size slide

  49. .Rmd .md
    .html
    .pdf
    .docx
    knitr Pandoc

    View full-size slide

  50. Write .Rmd
    .Rmd
    set document
    write document
    write .r
    write document
    write document
    test .r
    write .r
    test .r
    ---
    title: "Hoge"
    output:
    html_document: default
    ---
    YAML header
    (YAML Ain't a Markup Language.)

    View full-size slide

  51. Chunk options
    ```{r chunk1, ...}
    ```
    Language Name Options
    echo
    eval
    include
    results
    fig.width / fig.height
    collapse
    warning
    message

    View full-size slide

  52. Literate Programming
    code
    document
    # comment
    Input Output

    View full-size slide

  53. Let us change our traditional attitude to the construction
    of programs: Instead of imagining that our main task is to
    instruct a computer what to do, let us concentrate rather
    on explaining to humans what we want the computer to
    do.
    — Donald E. Knuth, Literate Programming, 1984
    プログラム構築に関する伝統的な態度を切り替えてみましょう。
    コンピュータに何をするべきかを指図するのが主な仕事だと考えるのではなく、
    むしろコンピュータに何をして欲しいかを⼈間に説明することに集中しよう。
    — ドナルド・E・クヌース, ⽂芸的プログラミング, 1984
    Literate Programming

    View full-size slide