<- "Hello, World!"
message
print(message)
[1] "Hello, World!"
This lesson is designed to be run on posit.cloud in a workshop situation, but there is no reason it can’t be completed using RStudio Desktop if you are familiar with it. The lessons would need to be downloaded from Github and packages installed, but directions for that are beyond the scope of this lesson.
These lessons are written using Markdown in Quarto notebooks, so you will be directly reading the code. Most everything will look like text and be perfectly readable, but some constructions may be new to you. So some quick tips.
variable_name
. This is a way to designate code or a file name within Markdown prose. If you are asked to use a value in some way, don’t include the tick marks.If you see items in an ordered list that start with numbers, that is an indication YOU SHOULD DO WHAT IT SAYS.
We’ll talk more about why we use Markdown and Quarto in a bit.
We are assuming at this point that you have ALREADY done these things:
If all that is true, then you are running a “virtual machine” in your browser that has R and RStudio installed, and you have a “project” open, and in that project you reading this file.
RStudio is your dashboard for writing R code. Open files will show up in the upper-left quadrant of RStudio.
The upper-right quadrant has several tabs, the first of which is Environment: Code objects stored in memory (such as a table of data), will show up there.
The lower-right quadrant also has several tabs, the first of which is Files: it shows what files exist in your current working directory, which is this workshop folder. Among those files are ones that end with .qmd
, which are Quarto files we will edit. Once opened, they appear in the upper-left quadrant.
In the lower-left, you’ll see the Console. This will reflect all the R code you run, but we don’t do much with it when we’re working in Quarto files. There is also Terminal, which is a way to interface your computer through written commands. We won’t be using that today, but if you continue with this type of work you likely will.
I encourage writing R code within Quarto Document files (.qmd) because they enable you to both write comments (like these sentences) and to write code, in what we call a code chunk.
When you see a numbered list item like the one below, these are directions for you follow. So do what is says!
In Quarto files, the results of the code in each code chunk will print below the chunk, allowing you to quickly see the results of your work. This allows us to intersperse our prose notes and explanations in between our code. The merits of this will hopefully become apparent as we work on this throughout the day.
You create a code chunk by typing ctrl+alt+i
(cmd+option+i
on a Mac) and you write your code
in between the first and last lines of the code chunk. All your comments, questions, thoughts, and notes are written outside of the code chunks are written in Markdown, a documentation language that reads well as text but converts easily to HTML and other outputs. If you don’t know it, don’t worry about it for now … it’s really just text.
message
and then run the chunk.The “Hello, World!” message will print out again because that text was saved into an object named message
. We’ll talk about objects next.
We can usually just type an object name and it will print to the console. But we sometimes use print()
to make sure the object prints to the console, even if it’s not the last line of code in a chunk.
Some important terminology has come up already:
ctrl+alt+i
(cmd+option+i
on a Mac). Type your code inside code chunks.message
in your environment now.For example, the following code stores the word “spaghetti” into an object named “x”, using the assignment operator <-
, and then prints the contents of that object to the console below the code chunk.
You can also use keyboard commands to run code: ctrl+Enter
(or cmd-Return
on a Mac) will run a line of code you are on or have selected. ctrl+Shift+Enter
(or cmd-Shift-Return
on Mac) will run all the code within a chunk.
In the example above, “spaghetti” is a string of text, or characters. You can also store numbers in objects (which do not require double quotes):
While you can name variables whatever you want, there are some rules and conventions:
_
to separate terms in a name.age
not my_var
.1
, 5
, 10000
. No decimal places.5.2
or 100.37
TRUE
or FALSE
(not quoted)c()
function:[1] "spagetti sauce" "noodles" "parmesan"
[1] 1 2 3
function_name(arguments)
. For example, the sum()
function works in R the same way it works in other programs.install.packages()
function. Then every time you want to use a package (such as tidyverse) in your file, you load it into your environment using the library()
function. All the necessary packages for this class have already been installed on your virtual machine.|>
or %>%
. A shortcut for typing it is ctrl+shift+m
(cmd+shift+m
on a Mac).typeof()
function to find the data type of “food_list”. The second line does the same thing, but it is moving our object food_list into the function using using the pipe.As we go through the day you’ll hopefully see why pipes make our code much easier to understand.
The key to understanding a programming language like R is to understand how information is passed around. For the sake of this class, let’s refer to information as data, although it won’t always be tabular. Data can be stored in an object or printed to the console (which is directly below a code chunk in our notebooks). Data stored in an object shows up in the Environment, and can be referred to later in your script.
Additionally, data can be passed (or piped) through functions that filter, sort, aggregate, and/or mutate it in some way. But it will always end up either printing to the console or being stored in a variable.
As you’ve seen within this document, we can mix together writing and explanation in our Quarto document. This allows us to publish our work as a repeatable, annotated document as HTML. To do this, we render our documents.
cntl+Shift+k
(cmd+Shift+k
on a Mac), to turn your document into HTML.This will open a web page either within RStudio’s “Viewer” pane, or in a separate web browser window. Look through this page and compare how your text and code looks in your Quarto document vs what is displayed on the web page.
(If you got an error when you rendered, raise your hand so someone can help troubleshoot it.)
You can use Quarto and Markdown to create websites, books, slideshows and more.
Let’s learn some more code. Save and close this document by clicking on the small x after it’s name, then open the 02-tidyverse.qmd
file by clicking on it in your Files pane.