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. Beginne R Advance d Hoxo_m If I have seen further

    it is by standing on the shoulders of Giants. -- Sir Isaac Newton, 1676
  2. "a" != "b" # is A in B? ブール演算⼦ Boolean

    Algebra [1] TRUE 1 %in% 10:100 # is A in B? [1] FALSE
  3. George Boole 1815 - 1864 A Class-Room Introduc2on to Logic

    h7ps://niyamaklogic.wordpress.com/c ategory/laws-of-thoughts/ Mathematician Philosopher &
  4. ブール演算⼦ 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
  5. 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/
  6. 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
  7. 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
  8. R Markdown ① Make a new .Rmd file ② Write

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

    something (for human) using markdown & R ③ Knit it!!
  10. Markdown A simple markup language that uses plain text formatting

    syntax plain text with markdown .html .pdf .docx
  11. ```r library(palmerpenguins) penguins[1, ] ``` ``` # A tibble: 1

    × 8 species island bill_…¹ bill_…² flipp…³ body_…⁴ sex year <fct> <fct> <dbl> <dbl> <int> <int> <fct> <int> 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
  12. Write .md .md .r write code test code write document

    copy code paste code copy results paste results write document write document
  13. Write .Rmd .Rmd set document write document write .r write

    document write document test .r write .r test .r
  14. 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
  15. 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
  16. 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.)
  17. 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.)
  18. Knit .Rmd in English .Rmd → .html .Rmd → .pdf

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

    .Rmd → .docx Easy!! Great!! Darkness Darknes(
  20. As soon as Anakin came before him on Coruscant, Yoda

    could sense darkness in his soul. --- The Phantom Menace
  21. 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 ---
  22. 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
  23. Chunk options ```{r chunk1, ...} ``` Language Name Options echo

    eval include results fig.width / fig.height collapse warning message
  24. 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
  25. 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+↩
  26. .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
  27. R Markdown ① Make a new .Rmd file ② Write

    something (for human) using markdown & R ③ Knit it!!
  28. 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.)
  29. Chunk options ```{r chunk1, ...} ``` Language Name Options echo

    eval include results fig.width / fig.height collapse warning message
  30. 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