If a new empty R script file does not open automatically when you launch RStudio you can open a new one^[Alternatively use Ctrl + shift + n
to launch a new R script]
File -> New File -> Rscript
\noindent Make sure that you choose the correct type of file. The RStudio IDE is clever in the way that it treats file extensions. Choosing a R script file will make sure that when you come to save your script it is saved as the correct type. It is also necessary to allow us to easily send R code from the text editor to be evaluated in the R terminal.
Let's start with sending our first piece of code to be evaluated. In the script editor (likely the top left window) write a simple piece of code, say
x = 5
\noindent To send this code from the editor to be run by R you have a few options:
Run
at the top left of the editor windowCtrl + enter
Ctrl + Shift + s
sends all code from the editor to be evaluated.r margin_note("I tend to prefer using 'Ctrl + Enter' as I like using keyboard shortcuts. If you want to see all available keyboard shortcuts either go to Help and choose keyboard shortcuts, alternatively 'Alt + Shift + k' is the keyboard shortcut for the keyboard shortcut menu.")
Make sure that you have the course package loaded into the current session. Instructions on how to install the package are contained in the appendix to the course notes. You should only need to install a package once using install.packages()
and then we need to use library()
in every session or script that we want to use the functionality of that package.
library("jrIntroduction")
Write the following code in the editor and run it
x1 = GetNumericVector()
\noindent This code generates a large vector of random numbers such that everyone has the same.^[This function is part of the course package jrIntroduction, you can view it's help page if you like r knitr::inline_expr('?GetNumericVector()')
]
What is the length of x1
?
r
length(x1)
What is the 55^th^ element of x1
?
r
x1[55]
What is the final value in x1
?
r
x1[length(x1)]
What is the 50^th^ smallest value of x1
?
r
sort(x1)[50]
How many unique values are there in x1
?
r
length(unique(x1))
What is the total of all elements?
r
sum(x1)
Solutions to the practical questions are contained within the package
vignette("solutions1", package = "jrIntroduction")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.