[1] "I'm code"
After module 1, you should be able to…
RStudio is an Integrated Development Environment (IDE) for R
Easier working with R
More information
“Analysis” Script
Static copy of what you did (reproducibility)
Top by default
Useful RStudio “cheat sheet”: https://github.com/rstudio/cheatsheets/blob/main/rstudio-ide.pdf
If RStudio doesn’t look the way you want (or like our RStudio), then do:
In R Studio Menu Bar go to View Menu –> Panes –> Pane Layout
In slides, the R command/code will be in a box, and then directly after it, will be the output of the code starting with [1]
So print("I'm code")
is the command and [1] "I'm code"
is the output.
Commands/code and output written as inline text will be typewriter blue font. For example code
You can do basic arithmetic in R, which I surprisingly use all the time.
+
, -
, /
, *
are add, subtract, divide and multiply^
or **
is power(
and )
– work with order of operations%%
finds the remainderTo execute or run a line of code (i.e., command), you just put your cursor on the command and then:
OR
Cmd + Return
(iOS) OR Ctrl + Enter
(Windows).To execute or run multiple lines of code, you need to highlight the code you want to run and then follow option 1 or 2.
Execute 5+4
from your .R file, and then find the answer 9 in the Console.
The syntax #
creates a comment, which means anything to the right of #
will not be executed / run
Commenting is useful to:
Add a comment header to Module1.R. This is the one I typically use, but you may have your own preference. The goal is that you are consistent so that future you / collaborators can make sense of your code.
You can also create sections within your code by ending a comment with 4 hash marks. This is very useful for creating an outline of your R Script. The “Outline” can be found in the top right of the your Source pane
I tend to use:
Object - an object is something that can be worked with in R - can be lots of different things!
… many more
<-
to assign values to an object nameX
and x
are differentNote, c()
, paste0()
, and matrix()
are functions, which we will talk more about in module 2.
like this
` for R to recognize it.A syntactically valid name consists of letters, numbers and the dot or underline characters and starts with a letter or the dot not followed by a number. Names such as “.2way” are not valid, and neither are the reserved words.
if
, else
, repeat
, while
, function
, for
, in
, next
, break
, TRUE
, FALSE
, NULL
, Inf
, NaN
, NA
, NA_integer_
, NA_real_
, NA_Complex_
, _NA_Character
, ...
, ..1
, ..2
, ..3
, and so on.Valid | Invalid |
---|---|
my_object |
my-data |
the.vector |
2data |
num12 |
for |
measles_data |
.9data |
.calc |
xX~mŷ_δätą~Xx |
=
and <-
can both be used for assignment, but <-
is better coding practice, because sometimes =
doesn’t work and we want to distinguish between the logical operator ==
. We will talk about this more, later.
Try creating one or two of these objects in your R script
Note, you can find these objects now in your Environment pane.
Also, you can print them anytime (i.e, see them in the Console) by executing (running) the object. For example,
List is a special data class, that can hold vectors, strings, matrices, models, list of other lists.
Will certainly save you time
Cmd + Return
(iOS) OR Ctrl + Enter
(Windows) in your script evaluates current line/selection
See http://www.rstudio.com/ide/docs/using/keyboard_shortcuts for many more
If you start typing a object, RStudio will show you options that you can choose without typing out the whole object.
<-
syntax to create objectsmy.object
my.vector
using the c()
functionmy.object
and my.vector
together using an arithmetic operatorThese are the materials we looked through, modified, or extracted to complete this module’s lecture.