class: center, middle, inverse, title-slide .title[ # Module 01: Getting Started with R, RStudio, R Objects ] .subtitle[ ## Rollins COVID-19 Epidemiology Fellowship R Training: Nov 11, 2022 ] .author[ ###
Melinda Higgins
] .date[ ### Director Biostatistics & Data Core School of Nursing - Emory University ] --- # Overview - Website for Workshop <iframe src="https://melindahiggins2000.github.io/Emory_RWorkshop_11Nov2022/" width="100%" height="400px" data-external="1"></iframe> .footnote[[https://melindahiggins2000.github.io/Emory_RWorkshop_11Nov2022/](https://melindahiggins2000.github.io/Emory_RWorkshop_11Nov2022/)] --- # R + RStudio .pull-left[ ### Plain R Console Old Fashioned "command line" <img src="Rconsole.png" width="=100%" /> ] .pull-right[ ### RStudio IDE Integrated Development Environment <img src="rstudio_ide01.png" width="=100%" /> _NOTE: This is NOT a GUI_ ] --- background-size: 100% background-image: url(rstudio_ide01.png) background-position: 70% 70% class: center, top --- background-size: 100% background-image: url(rstudio_ide02.png) background-position: 70% 70% class: center, top --- background-size: 100% background-image: url(rstudio_ide03.png) background-position: 70% 70% class: center, top --- background-size: 100% background-image: url(rstudio_ide04.png) background-position: 70% 70% class: center, top --- background-size: 100% background-image: url(rstudio_ide05.png) background-position: 70% 70% class: center, top --- background-size: 100% background-image: url(rstudio_ide06.png) background-position: 70% 70% class: center, top --- class: inverse, middle, left # Let's get started!! ### 1. Open RStudio --- # Create a new project Go to File/New Project <img src="module01_newproject01.png" width="=100%" /> --- # Create a new project Choose New Directory <img src="module01_newproject02.png" width="=100%" /> --- # Create a new project Click New Project <img src="module01_newproject03.png" width="=100%" /> --- # Create a new project Type in folder name and choose parent directory <img src="module01_newproject04.png" width="=100%" /> --- # What is a RStudio "Project"? * A "container" or _aka_ a directory with all files for this RStudio Project -- * Good Coding Practice!! -- * The DEFAULT place RStudio looks for your files --- # Your new `Rworkshop` project <img src="module01_Rworkshop01.png" width="=80%" /> --- # Get files for the Workshop 1. Go to [https://melindahiggins2000.github.io/Emory_RWorkshop_11Nov2022/](https://melindahiggins2000.github.io/Emory_RWorkshop_11Nov2022/) 2. Download `module01_Rscript.R` and put it into the new folder on your computer, _e.g._ `C:\Rworkshop`. --- # Files in your `Rworkshop` folder on your computer <img src="module01_Rworkshop02.png" width="=80%" /> --- # Files in your `Rworkshop` folder/RStudio project <img src="module01_Rworkshop03.png" width="=80%" /> .footnote[Click on `module01_Rscript.R` to open it.] --- <img src="module01_Rscript01.PNG" width="=100%" /> .footnote[Type code at command line or run one line at a time from script window.] --- # Basic commands done in Console <img src="module01_console01.png" width="=80%" /> --- count: false Code lines run in the console or from script window .panel1-console1-auto[ ```r *4 + 4 ``` ] .panel2-console1-auto[ ``` [1] 8 ``` ] --- count: false Code lines run in the console or from script window .panel1-console1-auto[ ```r 4 + 4 *sqrt(25) ``` ] .panel2-console1-auto[ ``` [1] 8 ``` ``` [1] 5 ``` ] --- count: false Code lines run in the console or from script window .panel1-console1-auto[ ```r 4 + 4 sqrt(25) *pi ``` ] .panel2-console1-auto[ ``` [1] 8 ``` ``` [1] 5 ``` ``` [1] 3.141593 ``` ] --- count: false Code lines run in the console or from script window .panel1-console1-auto[ ```r 4 + 4 sqrt(25) pi *seq(from=1, to=10, by=0.5) ``` ] .panel2-console1-auto[ ``` [1] 8 ``` ``` [1] 5 ``` ``` [1] 3.141593 ``` ``` [1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0 [16] 8.5 9.0 9.5 10.0 ``` ] <style> .panel1-console1-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-console1-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-console1-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- # **ASSIGN** your results to an "object", use .big[**<-**] .pull-left[ We really don't want to keep typing one line at a time in the Console, so we need to save/store value(s) in an object. An object is a text string we use to retrieve the value (or elements) we saved. Let's save the results of `2+2` in an object `result`. For this we will use the **ASSIGN** operator .big[**`<-`**]. ```r result <- 2+2 ``` ] -- .pull-right[ This is saved now in your "Global Environment". To retrieve this value, simply type the text string `result` to see the value `4`. ```r result ``` ``` [1] 4 ``` ] -- .pull-right[  ] --- class: inverse, middle, left # Your Turn ### 1. Start RStudio - Open "module01_Rscript.R" ### 2. Highlight Run Code for EXERCISE 01 --- # How to "run" code in an R script <img src="module01_runExample01.png" width="=80%" /> --- count: false Create Objects and Use Them .panel1-console2-auto[ ```r # save sequence of numbers in object x *x <- seq(from=1, to=10, by=0.5) ``` ] .panel2-console2-auto[ ] --- count: false Create Objects and Use Them .panel1-console2-auto[ ```r # save sequence of numbers in object x x <- seq(from=1, to=10, by=0.5) # view the contents of x *x ``` ] .panel2-console2-auto[ ``` [1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0 [16] 8.5 9.0 9.5 10.0 ``` ] --- count: false Create Objects and Use Them .panel1-console2-auto[ ```r # save sequence of numbers in object x x <- seq(from=1, to=10, by=0.5) # view the contents of x x # use x to create new object y *y <- x*x ``` ] .panel2-console2-auto[ ``` [1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0 [16] 8.5 9.0 9.5 10.0 ``` ] --- count: false Create Objects and Use Them .panel1-console2-auto[ ```r # save sequence of numbers in object x x <- seq(from=1, to=10, by=0.5) # view the contents of x x # use x to create new object y y <- x*x *y ``` ] .panel2-console2-auto[ ``` [1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0 [16] 8.5 9.0 9.5 10.0 ``` ``` [1] 1.00 2.25 4.00 6.25 9.00 12.25 16.00 20.25 25.00 30.25 [11] 36.00 42.25 49.00 56.25 64.00 72.25 81.00 90.25 100.00 ``` ] --- count: false Create Objects and Use Them .panel1-console2-auto[ ```r # save sequence of numbers in object x x <- seq(from=1, to=10, by=0.5) # view the contents of x x # use x to create new object y y <- x*x y # plot x and y *plot(x,y) ``` ] .panel2-console2-auto[ ``` [1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0 [16] 8.5 9.0 9.5 10.0 ``` ``` [1] 1.00 2.25 4.00 6.25 9.00 12.25 16.00 20.25 25.00 30.25 [11] 36.00 42.25 49.00 56.25 64.00 72.25 81.00 90.25 100.00 ``` <!-- --> ] <style> .panel1-console2-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-console2-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-console2-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- # Questions .pull-left[ 1. How many objects are in your "Environment" [top right window] 2. What kind of object is `x`? 3. How many elements does `y` have in it? 4. Where do you see the plot? ] .pull-right[ <img src="module01_img01.png" width="=100%" /> ] --- # Questions .pull-left[ ### Getting Help To find out what the `seq()` function does, in the Console window [bottom left] at the prompt `>` type: .left[`help(seq)`] ```r seq(from=1, to=10, by=0.5) ``` ``` [1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0 [16] 8.5 9.0 9.5 10.0 ``` Notice that we used equals `=` inside the `()` to assign _values_ to the _arguments_ for the `seq()` function. ] .pull-right[ <img src="module01_img02.png" width="=100%" /> ] --- class: inverse, middle, left # Your Turn ### 1. Open "module01_Rscript.R" ### 2. Highlight Run Code for EXERCISE 02 ### 3. Write Code to make plot for tangent of x ### 4. Write Code to make plot of log of x with log y .center[EXERCISE, 5 MIN]