You can put chunks of R code by putting three backticks at the beginning and end of the chunk. You need to give each chunk a unique name in the {}

Echo

The echo command tell R whether or not to print the code that generated the result.

Here is an example with echo=FALSE

## [1] 0.8830421

and here is an example with echo=TRUE

x<-rnorm(100)
y<-2*x+rnorm(100)
cor(x,y)
## [1] 0.9173503

and here is an example with echo being default

x<-rnorm(100)
y<-2*x+rnorm(100)
cor(x,y)
## [1] 0.8810115

Other Stuff

To insert r code inline, you use a single beginning and end backtick. An example may look like this 0.8810115, where the value of the correlation is reported. You may type in italicized text like this, and bold text like this. For those of you who know Latex, you can type equations using the usual inline dollar command sign, something like this \(\frac{x}{\sin{(x*\pi)}}\).

Graphics

Lets do a simple plot

plot(x,y,main="Scatterplot of X and Y")

Good Practice

In general you want the code in a markdown document to be self contained. That is anyone should be able to run the commands in your document and get the same results you do. Hence, all datawrangling and evaluations should be done in R (not using Excel or other programs on the side)

When reading in datafiles, it is best to have these linked from a URL. I find that storing files on GitHub works the best for those that I create/host (be sure to link to the “raw” version of the file).

# Draft Kings Salaries of Players on Houston Rockets and LA Lakers
dat<-read.csv("https://raw.githubusercontent.com/tmatis12/datafiles/main/DraftKings_Rockets_Lakers.csv")
head(dat)
##   Position                    Name...ID              Name       ID
## 1    PG/SG      James Harden (15014112)      James Harden 15014112
## 2    PG/SF      LeBron James (15014113)      LeBron James 15014113
## 3     PF/C     Anthony Davis (15014114)     Anthony Davis 15014114
## 4       PG Russell Westbrook (15014115) Russell Westbrook 15014115
## 5    SF/PF  Robert Covington (15014116)  Robert Covington 15014116
## 6    PG/SG      James Harden (15014076)      James Harden 15014076
##   Roster.Position Salary                     Game.Info TeamAbbrev
## 1             CPT  19200 LAL@HOU 08/06/2020 09:00PM ET        HOU
## 2             CPT  18600 LAL@HOU 08/06/2020 09:00PM ET        LAL
## 3             CPT  17400 LAL@HOU 08/06/2020 09:00PM ET        LAL
## 4             CPT  15600 LAL@HOU 08/06/2020 09:00PM ET        HOU
## 5             CPT  13500 LAL@HOU 08/06/2020 09:00PM ET        HOU
## 6            UTIL  12800 LAL@HOU 08/06/2020 09:00PM ET        HOU
##   AvgPointsPerGame
## 1            59.34
## 2            55.09
## 3            50.72
## 4            51.19
## 5            29.06
## 6            59.34

The average player salary is 4736 dollars.

Display code without Evaluating

Sometimes, you will want to put your code in document without evaluating. This may be done by setting the option eval=FALSE.

I sometimes like to bunch all the R code used in a document to one place where the reader may copy and execute in their installation of R. When doing this, make sure that the code is self-contained (i.e. have all libraries and links present)

# All R code used in document
x<-rnorm(100)
y<-2*x+rnorm(100)
cor(x,y)
plot(x,y,main="Scatterplot of X and Y")
dat<-read.csv("https://raw.githubusercontent.com/tmatis12/datafiles/main/DraftKings_Rockets_Lakers.csv")
head(dat)

Wrapping Up

When you are done, you can publish you document to the web. Unless you are hosting you own site already, the best was to do this is to use Rpubs, which is automatic and free for hosting R Markdown documents. You will get a unique html address for your page, which you can share with others.