Go to TogaWare.com Home Page. Data Science Desktop Survival Guide
by Graham Williams
Duck Duck Go



CLICK HERE TO VISIT THE UPDATED SURVIVAL GUIDE

KnitR Basic Example

20200602 Our first example of running actual R code will generate some random uniform data using stats::runif() and then viewing the utils::head() of the data and calculating the base::mean(). The following code block shows how this will look in the source .Rnw file.

<<example_random_mean>>=
# Always include a short comment to support the code.
x <- runif(1000) * 1000
head(x)
mean(x)
@

Below is what it looks like after it is processed by knitr and then LaTeX (as happens when we click the Compile PDF button in ).

# Always include a short comment to support the code.

x <- runif(1000) * 1000
head(x)
## [1] 183.53871 308.71132  29.63586 833.02629 633.34938 169.63990

mean(x)
## [1] 496.6546

Notice that the syntax is colour highlighted and the output is included as comments introduced in R with the ##. If we were to evaluate these commands ourselves in R the the output would not include the ##.


Support further development by purchasing the PDF version of the book.
Other online resources include the GNU/Linux Desktop Survival Guide.
Books available on Amazon include Data Mining with Rattle and Essentials of Data Science.
Popular open source software includes rattle and wajig.
Hosted by Togaware, a pioneer of free and open source software since 1984.
Copyright © 2000-2020 Togaware Pty Ltd. . Creative Commons ShareAlike V4.