25.1 KnitR Setup

20200602

Packages used in this chapter include diagram (Soetaert 2020), dplyr (Wickham et al. 2021), ggplot2 (Wickham et al. 2020), magrittr (Bache and Wickham 2020), xtable (Dahl et al. 2019), Hmisc (Harrell 2021), and rattle (G. Williams 2021).

Packages are loaded into the currently running R session from your local library directories on disk. Missing packages can be installed using utils::install.packages() within R. On Ubuntu, for example, R packages can also be installed using $ wajig install r-cran-<pkgname>.

# Load required packages from local library into the R session.

library(rattle)       # Dataset: weatherAUS.
library(magrittr)     # Data pipelines: %>% %T>% %<>%.
library(ggplot2)      # Visualise data.
library(xtable)       # Format R data frames as LaTeX tables.
library(Hmisc)        # Escape special LaTeX charaters.
library(diagram)      # Produce a flowchart.
library(dplyr)        # Data wrangling.
library(scales)
library(knitr)

The rattle::weatherAUS dataset is loaded into the template variable ds and further template variables are setup as introduced by Graham J. Williams (2017). See Chapter 8 for details.

dsname <- "weatherAUS"
ds     <- get(dsname)
    
nobs   <- nrow(ds)

vnames <- names(ds)
ds    %<>% clean_names(numerals="right")
names(vnames) <- names(ds)

vars   <- names(ds)
target <- "rain_tomorrow"
vars   <- c(target, vars) %>% unique() %>% rev()

A random sample of the dataset:

ds %>% sample_frac()
## # A tibble: 191,431 x 24
##    date       location     min_temp max_temp rainfall evaporation sunshine
##    <date>     <chr>           <dbl>    <dbl>    <dbl>       <dbl>    <dbl>
##  1 2019-01-16 Portland         15.6     27.2      0          NA       NA  
##  2 2014-05-10 Melbourne        12.1     16.6     NA           3        2  
##  3 2009-04-26 PerthAirport      7.5     26.3      0           3.2      9.4
##  4 2018-03-23 Melbourne        15.2     29.4      0           9.6     10.9
##  5 2017-11-09 Walpole          14.2     29.4      0.2        NA       NA  
##  6 2010-06-10 Tuggeranong       1.3     12.8      0          NA       NA  
##  7 2011-12-07 Townsville       25       33        0.6         5.4     12.3
##  8 2017-08-11 WaggaWagga        8.2     15.2      0           2.8     NA  
##  9 2018-11-09 Albury            5.9     23.6      0          NA       NA  
## 10 2019-07-11 Launceston        8       12.8     22.8        NA       NA  
## # … with 191,421 more rows, and 17 more variables: wind_gust_dir <ord>,
## #   wind_gust_speed <dbl>, wind_dir_9am <ord>, wind_dir_3pm <ord>,
## #   wind_speed_9am <dbl>, wind_speed_3pm <dbl>, humidity_9am <int>,
## #   humidity_3pm <int>, pressure_9am <dbl>, pressure_3pm <dbl>,
## #   cloud_9am <int>, cloud_3pm <int>, temp_9am <dbl>, temp_3pm <dbl>,
## #   rain_today <fct>, risk_mm <dbl>, rain_tomorrow <fct>

References

Bache, Stefan Milton, and Hadley Wickham. 2020. Magrittr: A Forward-Pipe Operator for r. https://CRAN.R-project.org/package=magrittr.
Dahl, David B., David Scott, Charles Roosen, Arni Magnusson, and Jonathan Swinton. 2019. Xtable: Export Tables to LaTeX or HTML. http://xtable.r-forge.r-project.org/.
Harrell, Frank E, Jr. 2021. Hmisc: Harrell Miscellaneous. https://CRAN.R-project.org/package=Hmisc.
Soetaert, Karline. 2020. Diagram: Functions for Visualising Simple Graphs (Networks), Plotting Flow Diagrams. https://CRAN.R-project.org/package=diagram.
Wickham, Hadley, Winston Chang, Lionel Henry, Thomas Lin Pedersen, Kohske Takahashi, Claus Wilke, Kara Woo, Hiroaki Yutani, and Dewey Dunnington. 2020. Ggplot2: Create Elegant Data Visualisations Using the Grammar of Graphics. https://CRAN.R-project.org/package=ggplot2.
Wickham, Hadley, Romain François, Lionel Henry, and Kirill Müller. 2021. Dplyr: A Grammar of Data Manipulation. https://CRAN.R-project.org/package=dplyr.
Williams, Graham. 2021. Rattle: Graphical User Interface for Data Science in r. https://rattle.togaware.com/.
Williams, Graham J. 2017. The Essentials of Data Science: Knowledge Discovery Using r. The r Series. CRC Press.


Your donation will support ongoing development and give you access to the PDF version of this book. Desktop Survival Guides include Data Science, GNU/Linux, and MLHub. Books available on Amazon include Data Mining with Rattle and Essentials of Data Science. Popular open source software includes rattle, wajig, and mlhub. Hosted by Togaware, a pioneer of free and open source software since 1984.
Copyright © 1995-2021 Graham.Williams@togaware.com Creative Commons Attribution-ShareAlike 4.0.