Summary
Check that you can use RStudio to write and run a small R script, and find help when a command is unfamiliar.
Before you start: No previous R programming is required.
Setup: Use R/RStudio for runnable examples; this unit explains the setup.
Your check: Recreate a calculation from a saved script and locate its function help. Practise in the unit summary.
What you learned
- R is the language that performs the calculations. RStudio provides an interface for writing code, running it, inspecting objects, and viewing plots.
- The Console runs commands immediately. A script saves commands as a text file so that you can run them again.
- The Environment pane shows objects in the current R session. The Files, Plots, Packages, and Help tabs support different tasks; their positions depend on your RStudio layout.
- Use
<-to assign a value to an object. Object names are case sensitive:heightandHeightare different names. - Functions take arguments in parentheses. Comments start with
#and explain the code without being executed. - A named argument uses
name = valueinside a call;<-stores the returned result outside it. A project sets a consistent starting folder for relative paths such asData/example.csv.
A short recap in R
Run these lines from a saved script. In RStudio, use Ctrl+Enter on Windows/Linux or Cmd+Enter on macOS to send a line or selection to the Console.
# Illustrative measurements in centimeters
height <- 12
width <- 5
area <- height * width
area
sqrt(area)
area is 60; sqrt(area) is approximately 7.746. Assignment stores a result, while entering an object’s name prints its value. Parentheses control the order of calculations, for example (2 + 3) * 4 gives 20.
Finding help
For a known function, enter ?sqrt or help("sqrt"). For a topic whose function name you do not know, try help.search("square root") and inspect the results. Check the relevant parts of a help page:
| Section | What it tells you |
|---|---|
| Title and Description | What the function does. |
| Usage | How to call it and which arguments have defaults. |
| Arguments | What each input means and which values it accepts. |
| Value | What the function returns. |
| Examples | Calls you can run and adapt. |
When a command fails, read the error message, check spelling and parentheses, and reduce the problem to a few reproducible lines. Include those lines and the exact error when asking for help.
Try it yourself
In a new R script, store a rectangle’s length as 9 cm and breadth as 4 cm. Calculate its area and the square root of that area. Save it as unit01-check.R, restart R using Session > Restart R, and run the script from the top. Use ?sqrt to check its argument and return value.
Expected checks: the area is 36 cm², its square root is 6 cm, and the saved script recreates the results after restart.
Compare a possible solution
length_cm <- 9
breadth_cm <- 4
area_cm2 <- length_cm * breadth_cm
area_cm2
sqrt(area_cm2)
The outputs are 36 and 6. If an object is missing after restart, check that the line creating it is saved and was run before the calculation. Alternative object names are fine.
Ready to continue: you can reproduce these results from your saved commands and locate a function’s help page.
Check your understanding
Try these questions before opening the suggested answers.
- Explain the difference between typing a command in the Console and saving it in a script.
- Change
widthto8. Which lines must you rerun to updateareaand its square root? - Find the Arguments and Value sections of
?sqrt. What input does it expect, and what does it return?
Suggested answers
- The Console executes a command immediately; a script stores its text so you can inspect, edit and rerun the steps. A saved script does not depend on remembering your Console history.
- Rerun
width <- 8, thenarea <- height * width, and then the commands displayingareaandsqrt(area). Withheightstill 12, they give 96 and about 9.798. Changingwidthalone does not recalculate an existingarea. sqrt()accepts a numeric input and returns its square root, element by element. Check Arguments for the input and Value for the returned result; negative real inputs need separate consideration.
Revisit saving and rerunning your first script, function syntax and the supplied import practice, and reading help files if needed.