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 Slide

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

    View Slide

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

    View Slide

  4. View Slide

  5. BeginneR Session

    View Slide

  6. BeginneR

    View Slide

  7. 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 Slide

  8. Before A'er
    BeginneR Session
    BeginneR BeginneR

    View Slide

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

    View Slide

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

    View Slide

  11. ブール演算⼦ 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 Slide

  12. Programing

    View Slide

  13. 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 df %>%
    ggplot(aes(x = speed, y = dist)) +
    geom_point()
    https://aiprogrammer.hashlab.jp/

    View Slide

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

    View Slide

  15. #102
    @kilometer00
    2022.10.22
    R Markdown for beginners

    View Slide

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

    View Slide

  17. 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 Slide

  18. Literate Programming
    code
    document
    # comment
    Input Output

    View Slide

  19. 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 Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  24. Markup Language
    Plain text Rich text
    Markdown HTML

    View Slide

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

    View Slide

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

    View Slide

  27. Markdown
    Plain text Rich text

    View Slide

  28. Markdown
    Plain text Rich text

    View Slide

  29. Markdown
    Plain text Rich text

    View Slide

  30. ```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 Slide

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

    View Slide

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

    View Slide

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

    View Slide

  34. 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 Slide

  35. 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 Slide

  36. 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 Slide

  37. 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 Slide

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

    View Slide

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

    View Slide

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

    View Slide

  41. 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 Slide

  42. 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 Slide

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

    View Slide

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

    View Slide

  45. 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 Slide

  46. 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 Slide

  47. .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 Slide

  48. Write python in .Rmd

    View Slide

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

    View Slide

  50. In summary ...

    View Slide

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

    View Slide

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

    View Slide

  53. 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 Slide

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

    View Slide

  55. Literate Programming
    code
    document
    # comment
    Input Output

    View Slide

  56. 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 Slide

  57. Enjoy!!

    View Slide