class: center, middle, inverse, title-slide .title[ # R, RStudio - intro to R language ] .subtitle[ ## R Workshop: August 24, 2023 ] .author[ ###
Melinda Higgins
] .date[ ### Director Biostatistics & Data Core School of Nursing - Emory University ] --- # Overview - Website for All 4 Workshops <iframe src="https://melindahiggins2000.github.io/RWorkshops_HJFGlobal_AugSept2023/" width="100%" height="400px" data-external="1"></iframe> .footnote[[https://melindahiggins2000.github.io/RWorkshops_HJFGlobal_AugSept2023/](https://melindahiggins2000.github.io/RWorkshops_HJFGlobal_AugSept2023/)] --- # 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 # Getting 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%" /> --- # Download "module01_Rscript.R" and place in this folder <img src="module01_saveLink.png" width="=80%" /> --- # Files for your `Rworkshop` folder/project on your computer <img src="module01_Rworkshop02.png" width="=80%" /> --- # Files for your `Rworkshop` 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[ ![globalenv](img/results4_globalenvironment.jpg) ] --- # 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 ``` ![](HJF_Rworkshop_08242023_pt1_files/figure-html/console2_auto_05_output-1.png)<!-- --> ] <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> --- # Getting Help .pull-left[ ### Help Window 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%" /> ]