2+2
2 Programming with R
write objectives
2.1 Terminology
The basis of programming is that we write down instructions for the computer to follow, and then we tell the computer to follow those instructions.
We write, or code, instructions in R because it is a common language that both the computer and we can understand.
We call the instructions commands and we tell the computer to follow the instructions by executing (also called running) those commands.
The console pane is the place where commands written in the R language can be typed and executed immediately by the computer. It is also where the results will be shown for commands that have been executed.
You can type commands directly into the console and press Enter
to execute those commands, but they will be forgotten when you close the session.
2.2 Code appearance
In these notes, code is displayed like this:
where the output or result of the code is displayed with two pound signs (##
)
2.3 R is an overgrown calculator
can I use webR for this page?
:pencil: In the console type the following code, then press Enter
.
2+2
[1] 4
Now try a more complicated equation.
2 + 5*(8^3)- 3*log10)
Uh oh, we got an Error. Nothing to worry about, errors happen all the time.
:pencil: Put a open parenthesis (
before log10
to fix it and try again.
3 > R is waiting on you…
:pencil: In the console type the following code, then press Enter
.
2 + 5*(8^3)- 3*log(10
Notice the console shows a +
prompt. This means that you haven’t finished entering a complete command.
This is because you have not ‘closed’ a parenthesis or quotation, i.e. you don’t have the same number of left-parentheses as right-parentheses, or the same number of opening and closing quotation marks.
When this happens, and you thought you finished typing your command, click inside the console window and press Esc
; this will cancel the incomplete command and return you to the >
prompt.
3.1 Packages
_“Where the real money from the movie is made._”
R is considered an Open Source software program. That means many (thousands) of people contribute to the software. They do this by writing commands (called functions) to make a particular analysis easier, or to make a graphic prettier.
When you download R, you get access to a lot of functions that we will use. However these other user-written packages add so much good stuff that it really is the backbone of the customizability and functionality that makes R so powerful of a language.
For example we will be creating graphics using functions like boxplot()
and hist()
that exist in base R. But we will quickly move on to creating graphics using functions contained in the ggplot2
package. We will be managing data using functions in dplyr
and reading in Excel files using readxl
. Installing packages will become your favorite past-time.
3.2 Installing Packages
:pencil: Start by typing the following in the console to install the ggplot2
package.
install.packages("ggplot2")
When the download and install is complete, you should see a message simliar to:
The downloaded binary packages are in
C:\Users\Robin\AppData\Local\Temp\Rtmpi8NAym\downloaded_packages
:warning: R is case sensitive and spelling matters. If you get an error message like the following:
in install.packages :
Warning available (for R version 3.5.1) package ‘ggplot’ is not
The correct package name is ggplot2
, not ggplot
.
Alternative Method of installing Packages:
Use the Package tab in the lower right pane in R Studio.
3.3 Install all the things!
Now that you’re a package installing pro, go ahead and install the following packages that we will be using in the next few weeks.
set as two columns
Install the list of packages to the left one at a time and wait for it to finish before entering the next command.
tidyverse
rmarkdown
openintro
tinytex
3.4 Download and Install TinyTex
To convert your work into a professional looking PDF, or to write math symbols in your homework, or you need a typesetting program called \(\LaTeX\) (pronounced “lay-tek” or “lah-tex”). It’s a super neat program, but also nearly 4Gb. Too big for our needs. That’s where the tinytex
package came from. we’re going to use it to install a lightweight version of \(\LaTeX\). This will let us write math equations in R Markdown, and create PDF’s of your homework.
In your console type the following and hit Enter
tinytex::install_tinytex()
This may take a few minutes, but then you’re good to go!
3.5 Seeking Help
help me help you
Sometimes a second pair of eyeballs is all you need
Much of this information is also on the FAQ page
still accurate?
3.6 Advice on asking for help
The key to receiving help from someone is for them to rapidly grasp your problem. You should make it as easy as possible to pinpoint where the issue might be.
Try to use the correct words to describe your problem. For instance, a package is not the same thing as a library. Most people will understand what you meant, but it can make things confusing for people trying to help you. Be as precise as possible when describing your problem.
:warning: Don’t let not knowing exactly how to describe your problem prevent you from asking. Screenshots help tremendously!
When asking someone for help try to 1. Explain what thing you are trying to do 2. Explain/show the code you wrote to try to do that thing 3. Explain/show your result, and if it’s not obvious explain why you feel it’s not the correct result. (E.g. you expected the answer to be 5, but instead it’s 10. )
:warning: Don’t spend more than 20 minutes banging your head on the wall before you ask for help!
3.7 Help from inside R Studio
3.7.1 Use the built-in RStudio help interface to search for more information on R functions
One of the fastest ways to get help is to use the RStudio help interface. This panel by default can be found at the lower right hand panel of RStudio. As seen in the screenshot, by typing the word mean
, RStudio tries to also give a number of suggestions that you might be interested in. The description is then shown in the display window.
3.7.2 I know the name of the function I want to use, but I’m not sure how to use it
If you need help with a specific function, let’s say barplot()
, you can type:
?barplot
If you just need to remind yourself of the names of the arguments, you can use:
args(lm)
3.7.3 I want to use a function that does X, there must be a function for it but I don’t know which one…
If you are looking for a function to do a particular task, you can use the help.search()
function, which is called by the double question mark ??
. However, this only looks through the installed packages for help pages with a match to your search request
??kruskal
If you can’t find what you are looking for, you can use the rdocumentation.org website that searches through the help files across all packages available.
Finally, a generic Google or internet search “R <task>” will often either send you to the appropriate package documentation or a helpful forum where someone else has already asked your question.
3.7.4 I get an error message that I don’t understand
Start by googling the error message. However, this doesn’t always work very well because often, package developers rely on the error catching provided by R. You end up with general error messages that might not be very helpful to diagnose a problem (e.g. “subscript out of bounds”). If the message is very generic, you might also include the name of the function or package you’re using in your query.
If you check Stack Overflow, search using the [r]
tag. Most questions have already been answered, but the challenge is to use the right words in the search to find the answers: http://stackoverflow.com/questions/tagged/r
:warning: Development of R moves pretty fast. When at all possible, use results from the past 1-2 years. Anything over 5 years old for packages such as ggplot
, dplyr
, and forcats
are likely obsolete.
3.8 In Person help
- Start by ask the person sitting next to you! Don’t hesitate to talk to your neighbor, compare your answers, and ask for help.
- Ask in the Math & Statistics Discord server - we have a dedicated channel just for this class but a lot of students in the server know how to use R.
- Attend Community Coding.
- Drop in work session & dedicated space to work on coding projects.
- Collaborate with your peers and learn from experts.
- Some hours are held over Zoom
3.9 Other Online
- In RStudio go to
Help
–>Cheatsheets
or to this URL https://posit.co/resources/cheatsheets/ - Stack Overflow: if your question hasn’t been answered before and is well crafted, chances are you will get an answer in less than 5 min. Remember to follow their guidelines on how to ask a good question.
- Chico R Users Google Group.
- The Posit Community: it is read by a lot of people and is welcoming to new users.
- If your question is about a specific package, see if there is a mailing list for it. Usually it’s included in the DESCRIPTION file of the package that can be accessed using `packageDescription(“name-of-package:. You may also want to try to email the author of the package directly, or open an issue on the code repository (e.g., GitHub).
3.10 Using AI tools
write guidance here
3.11 Written
If you’re a book kinda person, there is plenty of help available as well. Many have online versions or free PDF’s.
- R Markdown, the Definitive Guide: https://bookdown.org/yihui/rmarkdown/
- R for Data Science https://r4ds.had.co.nz/
- Cookbook for R http://www.cookbook-r.com/
- R Graphics Cookbook (I use this all the time) – Chapter 8 in the above link
- The Art of R Programming https://nostarch.com/artofr.htm
- R for… http://r4stats.com/
- Excel Users https://www.rforexcelusers.com/
- SAS and SPSS Users http://r4stats.com/books/r4sas-spss/
- STATA Users http://r4stats.com/books/r4stata/
4 Final Tips
4.1 Saving and closing your work.
Unless you’re returning to work in R Studio in a short while, you should make a habit to save all open tabs and completely shut down R studio when you are done working. This ensures your environment is cleared. This is a good thing.
4.2 Restart R
To restart R without shutting the entire window down, go to the file menu bar in the top,
Session –> Restart R and Clear Output
This is good to do when switching between projects/classes.