Applied Supervised Learning with R
上QQ阅读APP看书,第一时间看更新

Write R Markdown Files for Code Reproducibility

The considerable success of analytics is a result of the way the information and knowledge network around the subject started to spread. More open source communities emerged, developers were happily sharing their work with the outer world, and many data projects were becoming reproducible. This change meant that work started by one person was soon getting adapted, improvised, and modified in many different forms by a community of people before it got adopted in an entirely different domain than the one from where it initially emerged. Imagine every research work that gets published in conference submitting a collection of code and data that is easily reproducible along with their research paper. This change is accelerating the pace at which an idea meets reality, and innovation will start to boom.

Now, let's see how to create such reproducible work in a single file that we call the R Markdown file. In the following activity, we will demonstrate how to create a new R Markdown file in RStudio. A detailed intro to R Markdown could be found at https://rmarkdown.rstudio.com/lesson-1.html.

In the next activity, you will recreate the code shown in Exercise 4, Reading a CSV File with Text Column and Storing the Data in VCorpus, into an R Markdown. Observe in Figure 4.2 that you have just written the explanation and the code in R Markdown, and when the Knit to Word action is performed, it interweaves the explanation, code, and its output neatly into a word document.

Activity 1: Create an R Markdown File to Read a CSV File and Write a Summary of Data

In this activity, we will create a R Markdown file to read a CSV file and print a small summary of the data in a word file:

Perform the following steps to complete the activity:

  1. Open RStudio and navigate to the R Markdown option:

    Figure 1.4: Creating a new R Markdown file in Rstudio

  2. Provide the Title and Author name for the document and select the Default Output Format as Word:

    Figure 1.5: Using the read.csv method to read the data

  3. Use the read.csv() method to read the bank-full.csv file.
  4. Finally, print the summary into a word file using the summary method.

    The output is as follows:

Figure 1.6: Final output after using the summary method

Note

The solution for this activity can be found at page 438.