Reproducible Code
1.5 Reproducible Code
reproducible
code: The
An analysis should be repeatable at some future time. To reproduce an analysis the code to instructions to generate an generate the analysis must be able to be re-run, what is called reproducible code. The R statements analysis can be to accomplish a specific analysis, such as in Listing 1.5 , should generally be saved in a standard repeated at some future time. text file so that these instructions are reproducible. A text file consists of only standard characters, text file: A file which consist of the upper and lowercase letters of the alphabet, digits, punctuation marks, and that consists of
a few control codes. Text files are generally created with a text editor, a kind of simplified word only standard
processor that only works with the standard text characters. 3 characters. text editor: An Several text editors integrate with the R environment so well that they become an extension editor for editing a of R itself. Your author exclusively uses a non-traditional, free text editor vim , which runs on text file. Windows, Macintosh, Linux/Unix, iOS for the iPad and iPhone, and Android. An advantage of vim: General vim as a general text editor is that once having learned its insert and normal modes, and various purpose text editor with excellent R keystroke combinations for navigating and editing text, writing and editing text is considerably compatibility.
faster than with a traditional word processor. vim also integrates with other environments. For code
example, this book was written with vim in conjunction with a powerful, cross-platform, open integration: source and free typesetting system LaTeX .
Transfer code entered into an
Code integration, which transfers instructions from vim directly to R , is one means by which editor to an vim integrates with R . From within vim one keystroke combination starts an R session, or submits environment for executing the
a single line, selected range of lines, or an entire file of R instructions directly into the running code, such as R. R session. All entering and editing of code takes place within a vim window, and all output flows into an adjacent R window.
syntax
Another form of integration of a text editor with R is syntax highlighting. Within the editor, highlighting: Differently R keywords are highlighted, as are numerical constants, quoted strings, and so forth, all usually highlight different in different colors. This highlighting makes the on-screen text considerably more readable and elements of the code to enhance mistakes less likely. Figure 1.6 illustrates syntax highlighting, though only in gray scale.
readability.
20 R for Data Analysis
1 library (lessR) 2 set (colors= “dodgerblue” , ghost=TRUE, trans.fill.pt=0)
3 4 Read () 5 6 Histogram (Salary, xlab= “Salary (USD)” , ylab=“Count”)
Figure 1.6 Excerpt of R instructions in the vim editor with gray scale syntax highlighting.
Alternatives also exist for a more traditional editing experience than vim yet that still provide a free editing product with code integration and syntax highlighting. The recommended traditional style text editor for Macintoshs is TextWrangler . Windows users have the Tinn-R editor and R development system that integrates with the R system, or the general purpose text editor and Notepad replacement Notepad++ , with the NpptoR plug-in. Linux gnome users,
Rstudio,
as well as Windows and Macintosh users, have gedit with the RGedit plug-in. Many other
rstudio.com
possibilities exist as well, including the full editing and development environment provided by RStudio .
At a minimum, use a simple, basic text editor to develop and store your R programs. An editor that links directly to R is the most elegant solution, but even without linking to R directly you can manually copy and paste your R instructions from an editor into R . Save your R instructions gradually so as to build a library of instructions to accomplish a variety of analyses. With this strategy you can reproduce the output of any one analysis when desired, or do a simple modification to obtain a related analysis.