This is an example of how to use Knitr for doing historical data analysis and writing.

Text that is outside the code block is just ordinary text. You can use it to explain what you’re doing or to write an article.

But text that goes inside a code block is run by R. For example, here we load the historydata package and display one of the datasets.

library(historydata)
library(dplyr)
## 
## Attaching package: 'dplyr'
## 
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## 
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
us_state_populations
## Source: local data frame [983 x 4]
## 
##    GISJOIN year          state population
## 1     G090 1790    Connecticut     237655
## 2     G100 1790       Delaware      59096
## 3     G130 1790        Georgia      82548
## 4     G240 1790       Maryland     319728
## 5     G250 1790  Massachusetts     475199
## 6     G330 1790  New Hampshire     141899
## 7     G340 1790     New Jersey     184139
## 8     G360 1790       New York     340241
## 9     G370 1790 North Carolina     395005
## 10    G420 1790   Pennsylvania     433611
## ..     ...  ...            ...        ...

Now I can create a plot inside another code block:

library(ggplot2)
ggplot(data = us_state_populations,
       aes(x = year, y = population)) + 
  geom_point()
## Warning: Removed 1 rows containing missing values (geom_point).

plot of chunk unnamed-chunk-2

This would be a good place to explain what that plot means.

Now I can click the “Knit HTML” button in RStudio and get a document with the plot embedded. Then I can click the “Publish” button to get a document to share.