10
R Handouts Fall 2018-19 R Essentials – Save Your Work with R Markdown R handout Fall 2018 R Essentials – Save Your Work with RMarkdown.docx Page 1 of 10 Introduction to R and R-Studio 2018-19 R Essentials Save Your Work with R Markdown This illustration Assumes that You Have Installed R and R-Studio and knitr Introduction Why are we doing this? The short answer is: to get in the habit of saving your work. Once you get the hang of it, this really is a terrific “best practice”. Briefly, in this “R Essentials”, you will learn how to: 1. Begin your R-Studio session by opening an empty file that will contain your future code and output; 2. Chunk by chunk, navigate between writing code, fixing code, and executing code; 3. All done? Save (archiving) your work (nifty either as a record of your work, or for re-use later!) Preliminary Install the package knitr There are (at least) 2 methods. METHOD I – From the console window Step 1. Launch R Studio Step 2. In the console window, type the following EXACTLY at the prompt > TIPS – 1) don’t forget the period in install.packages and 2) don’t forget to enclose knitr in quotes: install.packages(“knitr”)

R handout Fall 2018 R Essentials - Save Your Work …people.umass.edu/biep540w/pdf/R handout Fall 2018 R...Click on INSTALL at bottom right. R Handouts Fall 2018-19 R Essentials –

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: R handout Fall 2018 R Essentials - Save Your Work …people.umass.edu/biep540w/pdf/R handout Fall 2018 R...Click on INSTALL at bottom right. R Handouts Fall 2018-19 R Essentials –

R Handouts Fall 2018-19 R Essentials – Save Your Work with R Markdown

R handout Fall 2018 R Essentials – Save Your Work with RMarkdown.docx Page 1 of 10

Introduction to R and R-Studio 2018-19

R Essentials Save Your Work with R Markdown

This illustration Assumes that You Have Installed R and R-Studio

and knitr

Introduction Why are we doing this? The short answer is: to get in the habit of saving your work. Once you get the hang of it, this really is a terrific “best practice”. Briefly, in this “R Essentials”, you will learn how to:

1. Begin your R-Studio session by opening an empty file that will contain your future code and output; 2. Chunk by chunk, navigate between writing code, fixing code, and executing code; 3. All done? Save (archiving) your work (nifty either as a record of your work, or for re-use later!)

Preliminary Install the package knitr There are (at least) 2 methods.

METHOD I – From the console window

Step 1. Launch R Studio Step 2. In the console window, type the following EXACTLY at the prompt > TIPS – 1) don’t forget the period in install.packages and 2) don’t forget to enclose knitr in quotes: install.packages(“knitr”)

Page 2: R handout Fall 2018 R Essentials - Save Your Work …people.umass.edu/biep540w/pdf/R handout Fall 2018 R...Click on INSTALL at bottom right. R Handouts Fall 2018-19 R Essentials –

R Handouts Fall 2018-19 R Essentials – Save Your Work with R Markdown

R handout Fall 2018 R Essentials – Save Your Work with RMarkdown.docx Page 2 of 10

How to Install knitr METHOD II – From the Environment, Files, Plots, Packages, etc window

Step 1. Launch R Studio Step 2. In the Files, Files, Plots, Packages window, click on PACKAGES tab. Next, click INSTALL. Step 3. From the drop down menu, the default selections are fine. In the box PACKAGES (separate multiple with space or comma), type knitr. Click on INSTALL at bottom right.

Page 3: R handout Fall 2018 R Essentials - Save Your Work …people.umass.edu/biep540w/pdf/R handout Fall 2018 R...Click on INSTALL at bottom right. R Handouts Fall 2018-19 R Essentials –

R Handouts Fall 2018-19 R Essentials – Save Your Work with R Markdown

R handout Fall 2018 R Essentials – Save Your Work with RMarkdown.docx Page 3 of 10

__1. Begin your R-Studio Session by Opening An Empty File that will contain your future code and output. The type of file you are about to create is called an R Markdown file How to Open a New R Markdown File.

Step 1. Launch R Studio Step 2. From the top menu bar: FILE > NEW FILE > R Markdown You should see something like the following (note – yours won’t say Carol Bigelow of course):

• At top right, at title: Type in a title of your choosing • Just below, under Default Output Format: choose your output format

o HTML – This is the default selection. It’s fine to choose this. • Click OK

Page 4: R handout Fall 2018 R Essentials - Save Your Work …people.umass.edu/biep540w/pdf/R handout Fall 2018 R...Click on INSTALL at bottom right. R Handouts Fall 2018-19 R Essentials –

R Handouts Fall 2018-19 R Essentials – Save Your Work with R Markdown

R handout Fall 2018 R Essentials – Save Your Work with RMarkdown.docx Page 4 of 10

Example –

STOP!! Here is a key to what you are looking at above: * R Markdown has provided you with a “shell” with, seemingly, a bunch of stuff! * Each gray shaded area is called a chunk. A chunk is a set of R commands with a “beginning” and “end” CHUNK BEGINNING: Each “chunk” begins with ```{r} or it begins with ```{r SOMETHING YOU CHOOSE HERE} IF you choose ```{r include=FALSE} THEN messages and code will be NOT SHOWN (I do not recommend this) If you choose ```{r echo=FALSE} THEN code will NOT BE SHOWN (I do not recommend this either) Personally, I recommend sticking with beginning each “chunk” using ```{r } CHUNK END: Each “chunk” ends with ```

Page 5: R handout Fall 2018 R Essentials - Save Your Work …people.umass.edu/biep540w/pdf/R handout Fall 2018 R...Click on INSTALL at bottom right. R Handouts Fall 2018-19 R Essentials –

R Handouts Fall 2018-19 R Essentials – Save Your Work with R Markdown

R handout Fall 2018 R Essentials – Save Your Work with RMarkdown.docx Page 5 of 10

Step 3. One more thing before getting to work is to clear this “shell” R Markdown of the “stuff” that we don’t really need, as we’re going to create and run our own chunks! To clear the “shell”: - Place your cursor at line 7 of the “shell” R Markdown. Drag to highlight and select all below - Click delete You should now see something like the following (with your name, not mine, obviously):

Page 6: R handout Fall 2018 R Essentials - Save Your Work …people.umass.edu/biep540w/pdf/R handout Fall 2018 R...Click on INSTALL at bottom right. R Handouts Fall 2018-19 R Essentials –

R Handouts Fall 2018-19 R Essentials – Save Your Work with R Markdown

R handout Fall 2018 R Essentials – Save Your Work with RMarkdown.docx Page 6 of 10

__2. Chunk by chunk, navigate between writing code, fixing code and executing code What we will do is to cycle, as follows:

1st – We open a new blank chunk (to do a specific task that we want to do) 2nd - We type some commands into this chunk and then we run it. 3rd - As, typically is the case, we EDIT the commands in this chunk until we get what we like and then re-run it 4th - Once, we’re happy with the current chunk and the current task, we move on to the next chunk/next task!

1st – How to Open a New Blank Chunk Click on the little green “insert a chunk” icon at top (on the right). From the drop down menu, choose R

You should see the following. - You should see the gray chunk start ```{r} - You should see your cursor placed inside - You should see the chunk end ``` - TIP - Do NOT edit or delete the chunk start or end!

Page 7: R handout Fall 2018 R Essentials - Save Your Work …people.umass.edu/biep540w/pdf/R handout Fall 2018 R...Click on INSTALL at bottom right. R Handouts Fall 2018-19 R Essentials –

R Handouts Fall 2018-19 R Essentials – Save Your Work with R Markdown

R handout Fall 2018 R Essentials – Save Your Work with RMarkdown.docx Page 7 of 10

2nd – Type Some Commands in the Chunk and Run it For Example, here I’ve typed some commands

How to Run/Execute the commands in the chunk Use your cursor to select the lines or chunk that you want to run/execute. From the Run drop down at top right, make your selection

You should see your output/results BELOW, in the CONSOLE window. Error messages appear here, too

Page 8: R handout Fall 2018 R Essentials - Save Your Work …people.umass.edu/biep540w/pdf/R handout Fall 2018 R...Click on INSTALL at bottom right. R Handouts Fall 2018-19 R Essentials –

R Handouts Fall 2018-19 R Essentials – Save Your Work with R Markdown

R handout Fall 2018 R Essentials – Save Your Work with RMarkdown.docx Page 8 of 10

3rd - As, typically is the case, we EDIT the commands in this chunk until we get what we like and then re-run it

That’s right. Most of the time, you will tinker with a chunk over and over (either to fix a mistake or to obtain a better solution to the task at hand). To do this:

- Position your cursor in the link of the chunk that you want to edit. - Edit, and re-execute

HACK! Is the stuff in the console window getting to be too much? Want to clear it? Position your cursor at the <- prompt in the CONSOLE window Click control-L to clear.

4th - Once, we’re happy with the current chunk and the current task, we move on to the next chunk/next task! Repeat 1st, 2nd’ and 3rd for each chunk/task you want to do.

Page 9: R handout Fall 2018 R Essentials - Save Your Work …people.umass.edu/biep540w/pdf/R handout Fall 2018 R...Click on INSTALL at bottom right. R Handouts Fall 2018-19 R Essentials –

R Handouts Fall 2018-19 R Essentials – Save Your Work with R Markdown

R handout Fall 2018 R Essentials – Save Your Work with RMarkdown.docx Page 9 of 10

__3. All done? Save (archiving) your work (nifty either as a record of your work, or for re-use later!)

The action of saving your work is what is meant by knit. How to knit:

- At top click on the drop down menu for the knit icon - From the drop down menu, I recommend that you choose KNIT TO WORD

(Why? Answer – so that you can open this file later in word and perhaps fancy it up a bit) - Tip: Take care to choose a destination folder that you’ll remember (I always choose DESKTOP)

One more thing. Save the R Markdown file itself! This has extension “.Rmd” (Handy if you want to be able to re-use it)

Huzzah. You will now have TWO files: - A R Markdown file (“.Rmd”) - An MS Word file (“.docx”)

Voila! This is what my MS Word file looks like

Page 10: R handout Fall 2018 R Essentials - Save Your Work …people.umass.edu/biep540w/pdf/R handout Fall 2018 R...Click on INSTALL at bottom right. R Handouts Fall 2018-19 R Essentials –

R Handouts Fall 2018-19 R Essentials – Save Your Work with R Markdown

R handout Fall 2018 R Essentials – Save Your Work with RMarkdown.docx Page 10 of 10

Final TIP!!!

NEVER!!!!!! Put a install.packages(“ “) command into a R Markdown file

INSTEAD Issue all your install.packages(“ “) commands in the CONSOLE