How to use this document – and the Rmarkdown code that created it

In the Piazza posting, ALL IN ONE PLACE, is a .zip file, WFED540-10Sept18.zip, which contains a .Rproj directory with all of the code necessary for you to consider this example in detail. You will need to “unzip” WFED540-10Sept18.zip and double-click on the .Rpoj file, WFED540-10Sept18.Rproj, to begin.

If you are reading this report on the web at RPubs , also read the twoBirds.Rmd file on the WFED540-10Sept18 directory along with this Rpubs report to understand how the report was created through RMarkdown.


What is shown here

Study the RMarkdown code that I created to produce this web page, while making reference to the RMarkdown Cheat Sheet. The RMarkdown code is in a file, TwoBirds.Rmd in the pinned Piazza posting, “ALL IN ONE PLACE - Videos, files, slides, etc. from class meetings.”

Just to show how to include images in an RMarkdown report, here is a screen capture of the R Markdown Cheat Sheet:

This image is shown in and read from the “Files” pane in RStudio, but it just as well could have been on the web. In that case, the full URL would have been used instead of the file name.


Using R code chunks for calculation within RMarkdown

Display some simple arithmetic

Here is some simple arithmetic, showing the R code chunk that created the results of calculations:

2 + 3
## [1] 5
4/2
## [1] 2
5 - 3
## [1] 2

Notice that the HTML page shows the R code in a gray box, followed results of the code.

Here is the section of the RMarkdown Cheatsheet that describes R code chunks:

Suppress printing of R code

Here is some more code that suppresses the printing of the R code:

## [1] 2 3 5 6

At most reports, you will want to suppress the R code because the coding most frequently is secondary to the answer. Of course, there are times that you will want to echo the code in the RMarkdown output for demonstration (as wil be the case in some parts of Data Science Challenges or in the Final Examination.)

Suppress double hashmarks (##) at the start of each line

Don’t like the ## at the beginning of each line? Do this:

[1] 2 3 5 6

Importing data for use in RMarkdown

I can import and print a dataset stored on the web and show you the code and print output:

dave <- read.csv("http://www.personal.psu.edu/dlp/w540/datasets/state_indicators.csv")
dave
        statename population income illiteracy life_exp murder hs_grad
1         Alabama       3615   3624        2.1    69.05   15.1    41.3
2          Alaska        365   6315        1.5    69.31   11.3    66.7
3         Arizona       2212   4530        1.8    70.55    7.8    58.1
4        Arkansas       2110   3378        1.9    70.66   10.1    39.9
5      California      21198   5114        1.1    71.71   10.3    62.6
6        Colorado       2541   4884        0.7    72.06    6.8    63.9
7     Connecticut       3100   5348        1.1    72.48    3.1    56.0
8        Delaware        579   4809        0.9    70.06    6.2    54.6
9         Florida       8277   4815        1.3    70.66   10.7    52.6
10        Georgia       4931   4091        2.0    68.54   13.9    40.6
11         Hawaii        868   4963        1.9    73.60    6.2    61.9
12          Idaho        813   4119        0.6    71.87    5.3    59.5
13       Illinois      11197   5107        0.9    70.14   10.3    52.6
14        Indiana       5313   4458        0.7    70.88    7.1    52.9
15           Iowa       2861   4628        0.5    72.56    2.3    59.0
16         Kansas       2280   4669        0.6    72.58    4.5    59.9
17       Kentucky       3387   3712        1.6    70.10   10.6    38.5
18      Louisiana       3806   3545        2.8    68.76   13.2    42.2
19          Maine       1058   3694        0.7    70.39    2.7    54.7
20       Maryland       4122   5299        0.9    70.22    8.5    52.3
21  Massachusetts       5814   4755        1.1    71.83    3.3    58.5
22       Michigan       9111   4751        0.9    70.63   11.1    52.8
23      Minnesota       3921   4675        0.6    72.96    2.3    57.6
24    Mississippi       2341   3098        2.4    68.09   12.5    41.0
25       Missouri       4767   4254        0.8    70.69    9.3    48.8
26        Montana        746   4347        0.6    70.56    5.0    59.2
27       Nebraska       1544   4508        0.6    72.60    2.9    59.3
28         Nevada        590   5149        0.5    69.03   11.5    65.2
29  New Hampshire        812   4281        0.7    71.23    3.3    57.6
30     New Jersey       7333   5237        1.1    70.93    5.2    52.5
31     New Mexico       1144   3601        2.2    70.32    9.7    55.2
32       New York      18076   4903        1.4    70.55   10.9    52.7
33 North Carolina       5441   3875        1.8    69.21   11.1    38.5
34   North Dakota        637   5087        0.8    72.78    1.4    50.3
35           Ohio      10735   4561        0.8    70.82    7.4    53.2
36       Oklahoma       2715   3983        1.1    71.42    6.4    51.6
37         Oregon       2284   4660        0.6    72.13    4.2    60.0
38   Pennsylvania      11860   4449        1.0    70.43    6.1    50.2
39   Rhode Island        931   4558        1.3    71.90    2.4    46.4
40 South Carolina       2816   3635        2.3    67.96   11.6    37.8
41   South Dakota        681   4167        0.5    72.08    1.7    53.3
42      Tennessee       4173   3821        1.7    70.11   11.0    41.8
43          Texas      12237   4188        2.2    70.90   12.2    47.4
44           Utah       1203   4022        0.6    72.90    4.5    67.3
45        Vermont        472   3907        0.6    71.64    5.5    57.1
46       Virginia       4981   4701        1.4    70.08    9.5    47.8
47     Washington       3559   4864        0.6    71.72    4.3    63.5
48  West Virginia       1799   3617        1.4    69.48    6.7    41.6
49      Wisconsin       4589   4468        0.7    72.48    3.0    54.5
50        Wyoming        376   4566        0.6    70.29    6.9    62.9
     area
1   50708
2  566432
3  113417
4   51945
5  156361
6  103766
7    4862
8    1982
9   54090
10  58073
11   6425
12  82677
13  55748
14  36097
15  55941
16  81787
17  39650
18  44930
19  30920
20   9891
21   7826
22  56817
23  79289
24  47296
25  68995
26 145587
27  76483
28 109889
29   9027
30   7521
31 121412
32  47831
33  48798
34  69273
35  40975
36  68782
37  96184
38  44966
39   1049
40  30225
41  75955
42  41328
43 262134
44  82096
45   9267
46  39780
47  66570
48  24070
49  54464
50  97203

Notice that thise code printed the entire dataset (as a result of the dave command.) In fact, there usually is no need to print the dataset after I import it, unless I need to display it in the report. Remember, some datasets are very large, too large to print in a report. Check the RMarkdown code following this paragraph to see how to import a dataset from the Internet and suppress geeky messages, but not to print the dataset.

A few formatting hints

The Markdown codes tht yield particular RMarkdown formats is in the following section of the RMarkdown Cheatsheet:

Submitting an RMarkdown file to RPubs

Remember I wrote in Piazza that you would need an RPubs account?

Here is how you render a .Rmd file for uploading to RPubs:

More info about RMarkdown is available at http://rmarkdown.rstudio.com and at https://ismayc.github.io/rbasics-book/4-rmarkdown.html.

Movement of a rendered .Rmd file to Rpubs is demonstrated in the 10 September 2018 class meeting (video available).