class: center, middle, title-slide .title[ # Intro to R and R Studio ] .author[ ### Joseph Elsherbini, remixing Claus O Wilke and Scott Handley ] .date[ ### 01-09-2023 ] --- --- <br> .center[ <img src = "intro_to_r_and_rstudio_files\foundational_skills.png", width = 100%></img> ] --- ## What is R? R is a general purpose programming language that's really well suited to statistics, manipulating tabular data, and plotting. --- ## Why R? -- <img src = "intro_to_r_and_rstudio_files/Rlogo.svg", width = 25%, style = "position:absolute; top: 18%; left: 15%;"></img> <img src = "intro_to_r_and_rstudio_files/Data_science_venn.svg", width = 40%, style = "position:absolute; top: 57%; left: 8%;"></img> ??? The R logo [is distributed](https://www.r-project.org/logo/) under the terms of the Creative Commons Attribution-ShareAlike 4.0 International license ([CC-BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0/)). RStudio hex stickers [are distributed](https://github.com/rstudio/hex-stickers) under [CC0.](https://github.com/rstudio/hex-stickers/blob/master/LICENSE.md) -- <img src = "intro_to_r_and_rstudio_files/package-hexlogos.svg", width = 35%, style = "position:absolute; top: 5%; left: 55%;"></img> --- [//]: # "segment ends here" ## Obtaining R Windows, Mac or Linux OS: https://www.r-project.org .center[ <img src = "intro_to_r_and_rstudio_files/Rlogo.svg", width = 30%></img> ] --- # Running R .pull-left[ ## Command line - from the command line, just type R - interactive session, nothing is saved by default ## Rstudio - RStudio: http://www.rstudio.com - Makes working with R much easier, particularly for a new R user ] .pull-right[ ##VSCode - https://code.visualstudio.com/download - not a full IDE, but you can customize it with extensions - Guide on setting up VSCode for R programming: https://renkun.me/2019/12/11/writing-r-in-vscode-a-fresh-start/ and https://renkun.me/2022/03/06/my-recommendations-of-vs-code-extensions-for-r/ ] --- ## The first core concept: Markdown -- **Input:** .small-font[ ``` This is a sentence in Markdown, containing `code`, **bold text**, and *italics*. ``` ] -- **Output:** .small-font[ This is a sentence in Markdown, containing `code`, **bold text**, and *italics*. ] --- ## The first core concept: Markdown .pull-left[ **Input:** .small-font[ ``` ## 1 Introduction Data visualization is part art and part science. The challenge is to get the art right without getting the science wrong and vice versa. ``` ]] .pull-right[ **Output:** ## 1 Introduction Data visualization is part art and part science. The challenge is to get the art right without getting the science wrong and vice versa. ] .absolute-bottom-right[ Text taken from: C. O. Wilke, [Fundamentals of Data Visualization](https://clauswilke.com/dataviz/introduction.html) ] --- ## R Markdown combines Markdown and R code -- **Input:** .small-font[ ```` The function `rnorm()` creates normal variates. ```{R} rnorm(5) # create 5 normal variates ``` ```` ] -- **Output:** .small-font[ The function `rnorm()` creates normal variates. ```r rnorm(5) # create 5 normal variates ``` ``` [1] -0.5861735 -0.8000169 0.8848258 0.2603640 -0.9689883 ``` ] [//]: # "segment ends here" --- ## Using RStudio If you haven't already, go to https://posit.cloud/content/5195221 and "Save a Permanent Copy" Then click on "install_for_workshop.R" on the bottom right file viewer, and go to Code -> Source --- ## Using RStudio .center[ <img src = "intro_to_r_and_rstudio_files/RStudio-clean.png", width = 80%, style = "position:relative; top:-52px;"></img> ] --- ## Using RStudio .center[ <img src = "intro_to_r_and_rstudio_files/RStudio-clean-edited.png", width = 80%, style = "position:relative; top:-52px;"></img> ] --- ## Knitting an R Markdown file .center[ <img src = "intro_to_r_and_rstudio_files/RStudio-markdown.png", width = 80%, style = "position:relative; top:-52px;"></img> ] --- ## Knitting an R Markdown file .center[ <img src = "intro_to_r_and_rstudio_files/RStudio-markdown-edited.png", width = 80%, style = "position:relative; top:-52px;"></img> ] --- ## Knitting an R Markdown file .center[ <img src = "intro_to_r_and_rstudio_files/markdown-knitted.png", width = 80%, style = "position:relative; top:-52px;"></img> ] --- ## Knitting to pdf .center[ <img src = "intro_to_r_and_rstudio_files/RStudio-markdown-edited2.png", width = 80%, style = "position:relative; top:-52px;"></img> ] --- ## Knitting to pdf .center[ <img src = "intro_to_r_and_rstudio_files/markdown-knit-pdf-edited.png", width = 80%, style = "position:relative; top:-52px;"></img> ] --- ## Knitting to pdf .center[ <img src = "intro_to_r_and_rstudio_files/markdown-knitted-pdf.png", width = 80%, style = "position:relative; top:-52px;"></img> ] --- ## Knitting to pdf .center[ <img src = "intro_to_r_and_rstudio_files/markdown-knitted-pdf-edited.png", width = 80%, style = "position:relative; top:-52px;"></img> ] --- ## Further reading - [R Markdown tutorial](https://rmarkdown.rstudio.com/lesson-2.html) - [R Markdown cheatsheet](https://github.com/rstudio/cheatsheets/raw/main/rmarkdown.pdf) - [R Markdown book](https://bookdown.org/yihui/rmarkdown/) --- # Using R as a calculator The simplest thing you can do with R is arithmetic ```r # + - / * for basic arithmetic # ** for exponents, sqrt, pi, log10, and more 100 + 1 ``` ``` [1] 101 ``` --- ## What percentage of your life have you spent in higher education (after grade school) as a student or trainee? 1. Create a new R markdown file (File -> New File -> R Markdown) and delete everything after the first five line header. 2. Create a new chunk by going to Code -> Insert Chunk or hitting Ctrl + Alt + i 3. Make an arithmetic expression to calculate the percentage ```r 10 + 4 # you can add 10 - 4 # subtract 4 / 2 # divide 2 * 5 # multiply 3 ^ 4 # and exponentiate # remember PEMDAS! y <- (3 + 4) * 7 + 3 ``` --- # The `tidyverse` The tidyverse is a collection of packages that all work together to make data science easier and more sensical in R. <img src = "intro_to_r_and_rstudio_files/hex-stickers.png", width = 40%, style = "position:absolute; top: 5%; left: 50%;"></img> --- <img src = "intro_to_r_and_rstudio_files/tidyverse_install.png", width = 100%></img> --- <img src = "intro_to_r_and_rstudio_files/tidyverse_load.png", width = 100%></img> --- <img src = "intro_to_r_and_rstudio_files/tidyverse_philosophy.png", width = 100%></img> --- # What does the tidyverse look like? ```r my_microbiome_data %>% filter(genus == "Streptococcus") %>% group_by(sample) %>% summarise(total_strep = sum(relative_abundance)) %>% my_plotting_function() ``` --- class: center, middle # the pipe operator `%>%` --- class: center, middle # `%>%` is pronounced "and then" --- # What does the tidyverse look like? ```r my_microbiome_data %>% filter(genus == "Streptococcus") %>% group_by(sample) %>% summarise(total_strep = sum(relative_abundance)) %>% left_join(sample_metadata) %>% my_plotting_function() ``` --- # What does the plotting in the tidyverse look like? ```r # inside the my_plotting_function() data %>% ggplot(aes(x = sample_condition, y = total_strep)) + geom_boxplot() + geom_point(size=2) + scale_y_log10("Streptococcus relative abundance") ``` --- # Getting help You can get help for a function by using `?` in front ```r ?sqrt ``` -- **CHEATSHEETS!** If there were one take away from today and tomorrow, it'd be to print these and hang them by your desk. https://posit.co/resources/cheatsheets/ Can also find the cheatsheets from inside RStudio: Help -> Cheat Sheets -> Browse Cheat Sheets -- **When googling an R question, add "tidyverse" to your search** ---