Most Basic use of knitr::spin()

NOTE: Better read the script that generated this web page first and then compare to this output

1. Most basic rules

Just slightly change the way you write your R scripts:

For your explanations and all your “non R code” , use #’ (include an empty space) instead of just #. Those lines will be formatted through markdown to appear as normal text on the final web page.

For your R code to be run, write nothing at the begining of the line. Make an R commented line with # and write your R code normally, including graphic commands. Those lines will appear as code (within a rectangular block) on the final web page.
The corresponding R console output will be preceeded by ## on the final web page and figures will just be inserted. For example:

#pi is 3.1416... constant
x <- seq(-pi,pi,by=0.1)
x[1:10]
##  [1] -3.141593 -3.041593 -2.941593 -2.841593 -2.741593 -2.641593 -2.541593
##  [8] -2.441593 -2.341593 -2.241593
plot(x,sin(seq(-pi,pi,by=0.1)),type="l",col="red",xlab="angle",ylab="sin(x)",main="Example of graphic")

plot of chunk unnamed-chunk-1

head(mtcars)
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

2. A bit of style

  • To make lists such as this one use “#’ * item”.
  • For the title, just write it with #‘and then add another line with #’ ==== (see at the top of this script)
  • For headings, use #‘# Heading and add more # for subheadings, i.e: #’ ## Subheading). Note the title is a Level 1 Heading.
  • For bold, enclose the words as in bolded words (see the script)
  • For italics, use words in italics
  • Use lines starting by #+ to set markdown chunk options (once you know what they are)
  • For line break, just leave 2 empty spaces at the end of the line.
  • After the list, leave an empty line with its #’

3. Once you have your script

After you have completed and saved your script (i.e., this one), and installed and loaded package knitr, go to the console and run: spin(“BasicSpin_log.R”, precious=TRUE)

You will get 3 files: “BasicSpin_log.Rmd”, “BasicSpin_log.md” and “BasicSpin_log.html”). You can open the .html file with your browser, most often you will be happy with it. The .Rmd and .md files are intermediate files that let you fine tune the html output in case you know some (pretty simple) markdown syntax.
If you open the .md file in RStudio, you will see a Preview HTML button that will generate and open another .html file (overwriting the .html file that had been generated by spin())