class: center, middle, inverse, title-slide # Meet R
⚒ ### S. Mason Garrison --- layout: true <div class="my-footer"> <span> <a href="https://r-computing-lab.github.io/psychtesting//" target="_blank">Psychological Testing</a> </span> </div> --- class: middle # Reproducible data analysis --- ## Reproducibility checklist .question[ What does it mean for a data analysis to be "reproducible"? ] -- Near-term goals: - Are the tables and figures reproducible from the code and data? - Does the code actually do what you think it does? - In addition to what was done, is it clear **why** it was done? (e.g., how were parameter settings chosen?) Long-term goals: - Can the code be used for other data? - Can you extend the code to do other things? --- ## Toolkit ![toolkit](img/toolkit.png) - Scriptability `\(\rightarrow\)` R - Literate programming (code, narrative, output in one place) `\(\rightarrow\)` R Markdown - Version control `\(\rightarrow\)` Git / GitHub --- class: middle # Toolkit overview --- ## What is R/RStudio? - R is a statistical programming language - RStudio is a convenient interface for R (an integrated development environment, IDE) - At its simplest: - R is like a car’s engine - RStudio is like a car’s dashboard <img src="img/engine-dashboard.png" width="66%" style="display: block; margin: auto;" /> --- ## Let's take a tour - R / RStudio .center[ ![](../img/demo.png) ] - Console - Using R as a calculator - Environment - Loading and viewing a data frame - Accessing a variable in a data frame - R functions --- ## Working with R at the command line - Launch RStudio/R. - Notice the default panes: - Console (entire left) - Environment/History (tabbed in upper right) - Files/Plots/Packages/Help (tabbed in lower right) -- - FYI: You can change the default location of the panes, among many other things - [Customizing RStudio](https://support.rstudio.com/hc/en-us/articles/200549016-Customizing-RStudio) --- ## Working with R at the command line (pt 2) - Go into the Console, where we interact with the live R process. - Make an assignment and then inspect the object you just created: ```r x <- 3 * 4 x ``` ``` ## [1] 12 ``` - All R statements where you create objects -- "assignments" -- have this form: ```r objectName <- value ``` - Read this as 'x gets 12' <!--- and in my head I hear, e.g., "x gets 12". You will make lots of assignments and the operator `<-` is a pain to type. Don't be lazy and use `=`, although it would work, because it will just sow confusion later. Instead, utilize RStudio's keyboard shortcut: Alt + - (the minus sign). --> --- ## R essentials A short list (for now): - Functions are (most often) verbs, followed by what they will be applied to in parentheses: ```r do_this(to_this) do_that(to_this, to_that, with_those) ``` -- - Columns (variables) in data frames are accessed with `$`: ```r dataframe$var_name ``` -- - Packages are installed with the `install.packages` function and loaded with the `library` function, once per session: ```r install.packages("package_name") library(package_name) ``` --- class: middle # Wrapping Up... --- class: middle # Getting help in R --- ## Reading help files <img src="img/r-help.png" width="50%" style="display: block; margin: auto;" /> --- ## Asking good questions .pull-left[ - **Good:** Describe your intention and include your code and the error - **Better:** Describe your intention and create a minimum working example - **Best:** Write a **rep**roducible **ex**ample (reprex) -- we'll introduce this concept more formally and teach you the tools for it a little later in the semester ] -- .pull-right[ ![](https://media.giphy.com/media/uRb2p09vY8lEs/giphy.gif) - Use code formatting - For issues with R code: copy / paste your code and resulting error, don't use screenshots! ] --- # Sources - Mine Çetinkaya-Rundel's Data Science in a Box ([link](https://datasciencebox.org/)) - Kieran Healy's [Data Visualization: A practical introduction] (http://socviz.co/appendix.html#a-little-more-about-r) - [Jenny Bryan's Stat545](https://stat545.com) - [Modern Dive](https://moderndive.com/) --- class: middle # Wrapping Up... Getting help in R