Intro2R Wk2
Intro2R Wk2
Intro2R Wk2
WK2
Funding
• U.S. Department of State awards SMLAQM19CA2361
Partnerships
• The U.S Embassy in Bangladesh and U.S Consulate General, Kolkata.
Academic collaborations
• Dhaka University and Bose Institute, Kolkata.
Introduction to R
Subjects to be covered
Rattle – an R
https://www.rstudio.com package
https://cran.r-project.org/web/packages/rattle/index.html
R Commander
https://socialsciences.mcmaster.ca/jfox/Misc/Rcmdr/
RKWard
https://rkward.kde.org/About.html
More alternatives exist.
1. Lots of powerful features for editing and running code. Easier to write scripts.
2. Easier to to import and inspect data (in table form) + see objects in the
working environment.
3. Version control through R Studio projects.
4. Improved organization of workspaces, scripts, data etc.
5. Graphics can be more accessible.
6. Can execute long running scripts in the background ( if memory is available).
7. Simplifies monitoring memory usage.
R Studio – getting started
2. Install RStudio: After installing R, you can download and install RStudio from
the RStudio website at https://www.rstudio.com/products/rstudio/download/
. Choose the appropriate installer for your operating system and follow the
installation instructions.
R Studio – Menu bar
1. Source 3. Environment
• The Environment tab shows the variables, data
• Source: This pane is where you
frames, and other objects in your current R
write and edit your R scripts.
workspace. The History tab displays a log of
You can create a new script by
previously executed commands.
clicking on "File" > "New File" >
"R Script" or open an existing
script using "File" > "Open File".
4. Files / plots /
packages/ Help/ …..
2. Console • These tabs provide access to your file system,
• plots generated by your R code, installed and
The console pane is where you can enter
available R packages, and help documentation.
R commands interactively and see the
output. You can execute a single line of
code by typing it in the console and
pressing Enter.
Write and run R code:
In RStudio, you can write R code in the Source pane and run it
using the Console pane.
e.g. install.packages("ggplot2").
e.g. library(ggplot2)
Save your
work
3. Save the R script by clicking on "File" > "Save" or pressing Ctrl+S (Cmd+S on
macOS). Choose a location on your computer and give your script a name, such as
"scatter_plot_example.R".
4. Install the ggplot2 package if you haven't already. You can do this in the Console
pane by typing install.packages("ggplot2") or by clicking on the "Packages" tab,
clicking "Install", typing "ggplot2", and pressing Enter.
6. After running the script, you should see the scatter plot in the "Plots" tab in the
lower right pane of RStudio. The Environment tab in the upper right pane will show
the variables created during the execution, such as data and scatter_plot.
Example
R Studio – Workflows
R workflows
R Projects
R Studio – R Projects
R Projects
• Having to set working directories and absolute paths to data
folders rapidly becomes a problem and slows work-flows.
Creating an R project
Menu bar ▶
File ▶
New project ▶
New Directory
New project
R project settings
R project subfolders
• Enables work to be organized in a
portable, self-contained folder.
• Use relative directory paths, not absolute paths to maintain E.g. relative path for a
portability. project subfolder.
“./subfolder”
WK3 R Studio – R Notebooks & Markdown
Knitr
• Both R Notebooks and R Markdown use a package called
Knitr.
• R Markdown: when the file is Knit, all the code (chunks) are
also run and their results rendered.
R Notebooks
• Workflow where analyses are performed within a document which
acts as the final report.
Advantages
YAML Header Metadata: title, author, date information, options for output.
YAML Header
• Includes title, author, and date information as
well as options for customizing output.
https://www.cloudbees.com/blog/yaml-tutorial-everything-
you-need-get-started
R Studio – R Notebook document
Markdown formatting
Text formatting - titles E.g.
# Title
• Use “#” for title of section ## Title
• Use ## for sub-section title (smaller font size) ### Title
• Use ### ……..
Options
Code chunk
e.g.
See https://sebastiansauer.github.io/figure_sizing_knitr/
R Studio – R Notebooks and R Markdown
R Notebooks & R Markdown Web page output
Click to run code chunk
Knit to render output, preview in R Notebook
rendered
output
Menu bar ▶
File ▶
New File ▶
R Markdown...
•Select the type of output to with the radio buttons (you can
change this later) e.g. HTML, PDF, Word …
•Click OK
R Studio – R Notebooks
R Notebook
• When you save an R Notebook Knitr
will automatically render a html version
of the notebook. And save it along with
an .RMD file (the Notebook)
R Notebook