-
Notifications
You must be signed in to change notification settings - Fork 0
/
1gettingStarted.tex
1279 lines (1145 loc) · 47.4 KB
/
1gettingStarted.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
% !Rnw root = Rlearn.Rnw
\input{preamble}
\section{Installation of R}
Click as indicated in the successive panels to download R for
Windows from the web page \url{http://cran.csiro.au}:
\vspace*{-3pt}
\begin{figure}
\fbox{
\includegraphics{figs-inc/01i-cran.jpg}
}
\vspace*{2pt}
\fbox{
\includegraphics{figs-inc/01i-base.jpg}
}
\vspace*{2pt}
\fbox{
\includegraphics{figs-inc/01i-download.jpg}
}
\caption{This shows a sequence of clicks that will download
the R installation file from \url{cran.csiro.edu}. At the time of writing,
the website will offer R-3.4.3 rather than R-2.13.0. The site
\url{cran.csiro.edu} is one of two Australian CRAN (Comprehensive R Archive
Network) sites. The other is:
\url{http://cran.ms.unimelb.edu.au/}}
\end{figure}
\begin{marginfigure}
\begin{center}
\begin{minipage}[c]{0.8\textwidth}
\includegraphics{figs-inc/01i-icons.jpg}
\end{minipage}
\end{center}
\caption{On 64-bit Windows systems the default installation
process creates two icons, one for 32-bit R and one for 64-bit R.
Additional icons can be created as desired.
}
\end{marginfigure}
Click on the downloaded file to start installation. Most users will
want to accept the defaults. The effect is to install the R base
system, plus recommended packages, with a standard ``off-the-shelf''
setup. Windows users will find that one or more desktop R icons have
been created as part of the installation process.
Depending on the intended tasks, it may be necessary to install
further packages. Section \ref{sec:pkgs} describes alternative
ways to install packages.
An optional additional step is to install RStudio.
\marginnote{Clicking on the RStudio icon to start a session will at
the same time start R. RStudio has its own command line interface,
where users can type R commands.} RStudio has abilities that
help in managing workflow, in navigating between projects, and in
accessing R system information. See Section \ref{sec:RStudio}.
\section{First steps}\label{sec:step1}
\marginnote{Readers who have RStudio running can type their commands
in the RStudio command line panel.}
Click on an R icon to start an R session. This opens an R command
window, prints information about the installed version of R, and
gives a command prompt.
\begin{figure}
\includegraphics[trim=0 18 0 0]{figs-inc/01i-gui.jpg}
\caption{Windows command window at startup. This shows the default MDI
(multiple display) interface. For running R from the R Commander,
the alternative SDI (single display) interface may be required, or
may be preferable. The Mac GUI has a SDI type interface; there is
no other option.}
\end{figure}
\noindent
The \code{$>$} prompt that appears on the final line
is an invitation to start typing R commands:
Thus, type \code{2+5} and press the Enter key.
The display shows:
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{> }\hlnum{2}\hlopt{+}\hlnum{5}
\end{alltt}
\begin{verbatim}
[1] 7
\end{verbatim}
\end{kframe}
\end{knitrout}
\noindent
\marginnote[-1.0cm]{The \margtt{[1]} says, a little strangely, ``first
requested element will follow''. Here, there is just one element.}
The result is 7. The output is immediately followed by the \code{>}
prompt, indicating that R is ready for another command.
Try also:
\marginnote[48pt]{Typing \margtt{result} on the
command line has printed the value 7.}
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{> }\hlstd{result} \hlkwb{<-} \hlnum{2}\hlopt{+}\hlnum{5}
\hlstd{> }\hlstd{result}
\end{alltt}
\begin{verbatim}
[1] 7
\end{verbatim}
\end{kframe}
\end{knitrout}
\marginnote[7pt]{Technically, the \textit{workspace} is one of a
number of \textit{databases} where objects may be stored.}
The object \margtt{result} is stored in the \textit{workspace}.
The {\em workspace} holds objects that the user has created or input,
or that were there at the start of the session and not later removed
Type \code{ls()} to list the objects in the workspace, thus:
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{> }\hlkwd{ls}\hlstd{()}
\end{alltt}
\begin{verbatim}
[1] "result"
\end{verbatim}
\end{kframe}
\end{knitrout}
\marginnote[-24pt]{The object \margtt{result} was added to
a previously empty workspace.}
Figure \ref{fig:cmds} shows, with annotations, the screen as
it appears following the above sequence of commands.
\begin{figure}
\includegraphics{figs-inc/01i-cmds.jpg}
\caption{This shows the sequence of commands that are demonstrated in
the text, as they appear on the screen, with added
annotation.\label{fig:cmds}}
\end{figure}
An R session is structured as a hierarchy of databases. Functions that
were used or referred to above --- such as \code{ls()} -- are from a
database or {\em package} that is part of the R system. Objects that
the user has created or input, or that were there at the start of the
session and not later removed, are stored in the {\em workspace}.
\marginnote[12pt]{Technically, the R system refers to the workspace
as \code{.Globalenv}.}
The workspace is the user's database for the duration of a session.
It is a volatile database, i.e., it will disappear if not explicitly
saved prior to or at the end of the session.
\subsection{Points to note}
\noindent
\fbox{\parbox{\textwidth}{\vspace*{-9pt}
\begin{tabbing}
Demonstrations\= \code{example(plot)} x\= \kill
Printing \> Typing the name of an object (and pressing \underline{Enter})\\
\> displays (prints) its contents.\\[4pt]
Quitting \> To quit, type \code{q()}, (not \code{q})\\[4pt]
Case matters \> \code{volume} is different from \code{Volume}
% Examples \> To run examples from a help page, type, e.g.\\
% \> \code{example(plot)} \> \code{\# Example from help(plot)}\\[4pt]
\end{tabbing}
\vspace*{-9pt}
}}
\vspace*{9pt}
Typing the name of an object (and pressing the \underline{Enter} key)
causes the printing of its contents, as above when \code{result} was
typed. This applies to functions also. Thus type \code{q()} in order
to quit, not \code{q}.\footnote{Typing \code{q} lists the code for the
function.} One types \code{q()} because this causes the function
\code{q} to spring into action.
Upon typing \code{q()} and pressing the Enter key, a message will ask
whether to save the workspace image.\sidenote{Such an {\em image}
allows reconstruction of the workspace of which it forms an image!}
Clicking Yes (usually the safest
option) will save the objects that remain in the workspace -- any that
were there at the start of the session (unless removed or overwritten)
and any that have been added since. The workspace that has been thus
saved is automatically reloaded when an R session is restarted in the
working directory to which it was saved.
\begin{figure}
\includegraphics{figs-inc/01i-cmds1.jpg}
\caption{Note the use of the special characters: \code{;} to separate
multiple commands on the one line, \code{+} (generated by the
system) to denote continuation from previous line, and \code{\#} to
introduce comment that extends to end of line.\label{fig:cmds1}}
\end{figure}
Note that for names of R objects or commands, case is significant.
Thus \code{Myr} (millions of years, perhaps) differs from \code{myr}.
For file names,\sidenote[][-28pt]{Under Windows case
is ignored. For Unix case does distinguish.
(Mac OS X Unix is a partial exception.)} the operating system
conventions apply.
Commands may, as demonstrated in Figure \ref{fig:cmds1}, continue over
more than one line. By default, the continuation prompt is \code{+}.
As with the \code{>} prompt, this is generated by R, and appears on
the left margin. Including it when code is entered will give an
error! \marginnote[-30pt]{Here is a command that extends over two lines:\\
\margtt{> result <-}\\
\margtt{+ 2+5} }
\subsection{Some further comments on functions in R}
\marginnote{R is a functional language. Whenever a command is entered,
this causes a function to run. Addition is implemented as a
function, as are other such operations.} Common functions that users
should quickly get to know include \margtt{print()}, \margtt{plot()}
and \margtt{help()}. Above, we noted the function \code{q()}, used
to quit from an R session.
Consider the function \code{print()}. One can explicitly invoke it to print the
number 2 thus:
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{print}\hlstd{(}\hlnum{2}\hlstd{)}
\end{alltt}
\begin{verbatim}
[1] 2
\end{verbatim}
\end{kframe}
\end{knitrout}
Objects on which the function will act are placed inside the round
brackets. Such quantities are known as {\em arguments} to the function.
An alternative to typing \code{print(2)} is to
type \code{2} on the command line. The function \code{print()}
is then invoked implicitly:
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlnum{2}
\end{alltt}
\begin{verbatim}
[1] 2
\end{verbatim}
\end{kframe}
\end{knitrout}
\subsection{Help information}\label{ss:ch1-help}
Included on the information that appeared on the screen when R started
up, and shown in Figures \ref{fig:cmds} and \ref{fig:cmds1}, were brief
details on how to access R's built-in help information:
{\small
\begin{verbatim}
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
\end{verbatim}
}
\noindent
The shorthand \code{?plot} is an alternative to typing
\code{help(plot)}.
Replace `\code{?}' by `\code{??}' for a wider search. This invokes
the function \code{help.search()}, which looks for a partial match
in the title or concept fields as well as in the name.
\begin{marginfigure}[-15pt]
Examples of use of \code{??}:\\[-5pt]
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlopt{??}\hlstd{Arithmetic}
\hlopt{??}\hlstd{base}\hlopt{::}\hlstd{Arith}
\hlcom{# Search base R only}
\end{alltt}
\end{kframe}
\end{knitrout}
\end{marginfigure}
R has extensive built-in help information. Be sure to check it out
as necessary. Section \ref{sec:workinghelp} has further details on
what is available, beyond what you can get by using the help function.
\subsection{The working directory}\label{ss:workdir}
Associated with each session \marginnote{Under Windows, if R is
started by clicking on an R icon, the working directory is that
specified in the \underline{Start in} directory specified in the icon
Preferences. Subsection \ref{sec:fine-tune} has details on how to
specify the \underline{Start in} directory for an icon.}
is a working directory where R will by default look
for files. In particular:
\begin{itemize}
\item If a command inputs data from a file into the
workspace and the path is not specified, this is where R
will look for the file.
\item If a command outputs results to a file, and the path is not specified,
this is where R will place the file.
\item Upon quitting a session, the ``off-the-shelf'' setup will ask
whether to save an ``image'' of the session.
\marginnote{When R finds a {\bf .RData} file in the working
directory at startup, that file will, in an off-the-shelf setup,
be used to restore the workspace.}
Answering
``Yes'' has the result that the contents of the workspace are saved
into a file, in the working directory, that has the name {\bf
.RData}.
\end{itemize}
For regular day to day use of R,
it is advisable to have a separate working
directory for each different project.
RStudio users will be asked to specify
a working directory when setting up a new ``project''.
\section{Installation of R Packages}\label{sec:pkgs}
\marginnote[10pt]{ A fresh install of R packages is typically required
when moving to a new major release (e.g., from a 3.0 series release
to a 3.1 series release).}
\noindent
\fbox{
\parbox{\linewidth}{ \noindent
\textbf{Installation of R Packages (Windows \& MacOS X)} \\[3pt]
Start R (e.g., click on the R icon). Then use the relevant menu item to
install packages via an internet connection. This is (usually) easier than
downloading, then installing.\\[6pt]
For command line instructions to install packages, see below.
}}
\vspace*{6pt}
The functions that R provides are organised into packages. The
packages that need to be installed, additional to those that come with
the initial ready-to-run system, will vary depending on individual
user requirements. The GUIs --- MacOS X, Windows or Linux --- make
package installation relatively straightforward.
\subsection*{Installation of packages from the command line}
To install the R Commander from the command line, enter:
\marginnote{By default, a CRAN mirror is searched for the required
package. Refer back to the introduction for brief comments on
CRAN. Subsection \ref{ss:installpack} gives details of
alternatives to CRAN. Note in particular the Bioconductor
repository.}
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{install.packages}\hlstd{(}\hlstr{"Rcmdr"}\hlstd{,} \hlkwc{dependencies}\hlstd{=}\hlnum{TRUE}\hlstd{)}
\end{alltt}
\end{kframe}
\end{knitrout}
The R Commander has a number of dependencies, i.e., packages that
need to be installed for the R Commander to run. Graphics packages
that are dependencies include \pkg{rgl} (3D dynamic graphics),
\pkg{scatterplot3d}, \pkg{vcd} (visualization of categorical
data) and \pkg{colorspace} (generation of color palettes, etc).
\subsection*{Installation of Bioconductor packages}
\marginnote[10pt]{For installation of Bioconductor packages from the GUI,
see Subsection \ref{sec:repos}.} To set your system up for use of
Bioconductor packages, type:
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{source}\hlstd{(}\hlstr{"http://bioconductor.org/biocLite.R"}\hlstd{)}
\hlkwd{biocLite}\hlstd{()}
\end{alltt}
\end{kframe}
\end{knitrout}
Additional packages can be installed thus:
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{biocLite}\hlstd{(}\hlkwd{c}\hlstd{(}\hlstr{"GenomicFeatures"}\hlstd{,} \hlstr{"AnnotationDbi"}\hlstd{))}
\end{alltt}
\end{kframe}
\end{knitrout}
See further \url{http://www.bioconductor.org/install/}.
\section{Practice with R commands}
\marginnote{Read \margtt{c} as ``\underline{c}oncatenate'',
or perhaps as ``column''.\\[6pt]
\noindent Lists are widely used in R. A data frame is a
special type of list,
used to collect together column objects under one name.}
\noindent
\fbox{\parbox{\textwidth}{\vspace*{-3pt}
\begin{tabular}{l}
{\bf Column Objects}\\
\qquad \code{width = c(11.3, 13.1, 20, 21.1, 25.8, 13.1)}\\[3pt]
\qquad \code{height = c(23.9, 18.7, 27.6, 28.5, 36, 23.4)}\\[4pt]
{\bf Data frame}\\
A data frame is a list of column objects, all of the same length.\\
\qquad \code{widheight <- data.frame( }\\
\qquad \qquad \code{width = c(11.3, 13.1, 20, 21.1, 25.8, 13.1),}\\
\qquad \qquad \code{height = c(23.9, 18.7, 27.6, 28.5, 36, 23.4)}\\
\qquad \code{ )}\\[4pt]
{\bf Also:} Arithmetic operations; simple plots; input of data.\\
\end{tabular}
\vspace*{-3pt}}}
\vspace*{3pt}
\marginnote{The R language has the standard abilities for evaluating
arithmetic and logical expressions. There are numerous functions
that extend these basic arithmetic and logical abilities.}
Try the following
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlnum{2}\hlopt{+}\hlnum{3} \hlcom{# Simple arithmetic}
\end{alltt}
\begin{verbatim}
[1] 5
\end{verbatim}
\begin{alltt}
\hlnum{1}\hlopt{:}\hlnum{5} \hlcom{# The numbers 1, 2, 3, 4, 5}
\end{alltt}
\begin{verbatim}
[1] 1 2 3 4 5
\end{verbatim}
\begin{alltt}
\hlkwd{mean}\hlstd{(}\hlnum{1}\hlopt{:}\hlnum{5}\hlstd{)} \hlcom{# Average of 1, 2, 3, 4, 5}
\end{alltt}
\begin{verbatim}
[1] 3
\end{verbatim}
\begin{alltt}
\hlkwd{sum}\hlstd{(}\hlnum{1}\hlopt{:}\hlnum{5}\hlstd{)} \hlcom{# Sum of 1, 2, 3, 4, 5}
\end{alltt}
\begin{verbatim}
[1] 15
\end{verbatim}
\begin{alltt}
\hlstd{(}\hlnum{8}\hlopt{:}\hlnum{10}\hlstd{)}\hlopt{^}\hlnum{2} \hlcom{# 8^2 (8 to the power of 2), 9^2, 10^2}
\end{alltt}
\begin{verbatim}
[1] 64 81 100
\end{verbatim}
\end{kframe}
\end{knitrout}
In addition to \code{log()}, note \code{log2()} and \code{log10()}:
\marginnote{A change by a factor of 2 is a one unit change
on a log2 scale. A change by a factor of 10 is a one unit change
on a log10 scale.}
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{log2}\hlstd{(}\hlkwd{c}\hlstd{(}\hlnum{0.5}\hlstd{,} \hlnum{1}\hlstd{,} \hlnum{2}\hlstd{,} \hlnum{4}\hlstd{,} \hlnum{8}\hlstd{))}
\end{alltt}
\begin{verbatim}
[1] -1 0 1 2 3
\end{verbatim}
\begin{alltt}
\hlkwd{log10}\hlstd{(}\hlkwd{c}\hlstd{(}\hlnum{0.1}\hlstd{,} \hlnum{1}\hlstd{,} \hlnum{10}\hlstd{,} \hlnum{100}\hlstd{,} \hlnum{1000}\hlstd{))}
\end{alltt}
\begin{verbatim}
[1] -1 0 1 2 3
\end{verbatim}
\end{kframe}
\end{knitrout}
\noindent
It turns out, surprisingly often, that logarithmic scales are
appropriate for one or other type of graph. Logarithmic scales focus
on relative change --- by what factor has the value changed?
The following uses the relational operator \code{$>$}:
\marginnote{
Other relational operators are\\
\margtt{$<\quad >=\quad <\quad <=\quad ==\quad !=$}
}
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{(}\hlnum{1}\hlopt{:}\hlnum{5}\hlstd{)} \hlopt{>} \hlnum{2} \hlcom{# Returns FALSE FALSE TRUE TRUE TRUE}
\end{alltt}
\begin{verbatim}
[1] FALSE FALSE TRUE TRUE TRUE
\end{verbatim}
\end{kframe}
\end{knitrout}
\subsection*{Demonstrations}
Demonstrations can be highly helpful in learning to use R's functions.
The following are some of demonstrations that are available for
graphics functions:
\begin{marginfigure}
Images and perspective plots:
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{demo}\hlstd{(image)}
\hlkwd{demo}\hlstd{(persp)}
\end{alltt}
\end{kframe}
\end{knitrout}
\end{marginfigure}
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{demo}\hlstd{(graphics)} \hlcom{# Type <Enter> for each new graph}
\hlkwd{library}\hlstd{(lattice)}
\hlkwd{demo}\hlstd{(lattice)}
\end{alltt}
\end{kframe}
\end{knitrout}
\begin{marginfigure}For the following, the {\em vcd} package must be installed:
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{library}\hlstd{(vcd)}
\hlkwd{demo}\hlstd{(mosaic)}
\end{alltt}
\end{kframe}
\end{knitrout}
\end{marginfigure}
Especially for \code{demo(lattice)}, it pays to stretch the graphics
window to cover a substantial part of the screen. Place the cursor on
the lower right corner of the graphics window, hold down the left
mouse button, and pull.
The following lists available demonstrations:
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlcom{## List demonstrations in attached packages}
\hlkwd{demo}\hlstd{()}
\hlcom{## List demonstrations in all installed packages}
\hlkwd{demo}\hlstd{(}\hlkwc{package} \hlstd{=} \hlkwd{.packages}\hlstd{(}\hlkwc{all.available} \hlstd{=} \hlnum{TRUE}\hlstd{))}
\end{alltt}
\end{kframe}
\end{knitrout}
\section{A Short R Session}\label{ss:shortR}
We will work with the data set shown in Table
\ref{tab:travel}:
% latex table generated in R 3.0.1 by xtable 1.7-1 package
% Sat Jul 20 10:40:12 2013
\begin{table*}[ht]
\centering
\begin{tabular}{rrrrrrl}
\hline
& thickness & width & height & weight & volume & type \\
\hline
Aird's Guide to Sydney & 1.30 & 11.30 & 23.90 & 250 & 351 & Guide \\
Moon's Australia handbook & 3.90 & 13.10 & 18.70 & 840 & 955 & Guide \\
Explore Australia Road Atlas & 1.20 & 20.00 & 27.60 & 550 & 662 & Roadmaps \\
Australian Motoring Guide & 2.00 & 21.10 & 28.50 & 1360 & 1203 & Roadmaps \\
Penguin Touring Atlas & 0.60 & 25.80 & 36.00 & 640 & 557 & Roadmaps \\
Canberra - The Guide & 1.50 & 13.10 & 23.40 & 420 & 460 & Guide \\
\hline
\end{tabular}
\vspace*{15pt}
\caption{Weights and volumes, for six Australian travel
books.}\label{tab:travel}
\end{table*}
\subsection*{Entry of columns of data from the command line}
The following enters data as numeric vectors:
\marginnote{Read \margtt{c} as
``\underline{c}oncatenate'', or perhaps as
``column''. It joins elements together into a vector, here
numeric vectors.}
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{volume} \hlkwb{<-} \hlkwd{c}\hlstd{(}\hlnum{351}\hlstd{,} \hlnum{955}\hlstd{,} \hlnum{662}\hlstd{,} \hlnum{1203}\hlstd{,} \hlnum{557}\hlstd{,} \hlnum{460}\hlstd{)}
\hlstd{weight} \hlkwb{<-} \hlkwd{c}\hlstd{(}\hlnum{250}\hlstd{,} \hlnum{840}\hlstd{,} \hlnum{550}\hlstd{,} \hlnum{1360}\hlstd{,} \hlnum{640}\hlstd{,} \hlnum{420}\hlstd{)}
\end{alltt}
\end{kframe}
\end{knitrout}
Now store details of the books in the character vector \code{description}:
\marginnote{The end result is that objects \margtt{volume}, \margtt{weight}
and \margtt{description} are stored in the workspace.}
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{description} \hlkwb{<-} \hlkwd{c}\hlstd{(}\hlstr{"Aird's Guide to Sydney"}\hlstd{,}
\hlstr{"Moon's Australia handbook"}\hlstd{,}
\hlstr{"Explore Australia Road Atlas"}\hlstd{,}
\hlstr{"Australian Motoring Guide"}\hlstd{,}
\hlstr{"Penguin Touring Atlas"}\hlstd{,} \hlstr{"Canberra - The Guide"}\hlstd{)}
\end{alltt}
\end{kframe}
\end{knitrout}
\subsection*{Listing the workspace contents}
Use \code{ls()} to examine the current contents
of the workspace.
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{ls}\hlstd{()}
\end{alltt}
\begin{verbatim}
[1] "description" "result" "volume" "weight"
\end{verbatim}
\end{kframe}
\end{knitrout}
Use the argument \code{pattern} to specify a search pattern:
\begin{marginfigure}[40pt]
Note also:\\[-5pt]
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{ls}\hlstd{(}\hlkwc{pattern}\hlstd{=}\hlstr{"^des"}\hlstd{)}
\hlcom{## begins with 'des'}
\hlkwd{ls}\hlstd{(}\hlkwc{pattern}\hlstd{=}\hlstr{"ion$"}\hlstd{)}
\hlcom{## ends with 'ion'}
\end{alltt}
\end{kframe}
\end{knitrout}
\end{marginfigure}
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{ls}\hlstd{(}\hlkwc{pattern}\hlstd{=}\hlstr{"ume"}\hlstd{)} \hlcom{# Names that include "ume"}
\end{alltt}
\begin{verbatim}
[1] "volume"
\end{verbatim}
\end{kframe}
\end{knitrout}
\subsection*{Operations with numeric `vectors'}
Here are the values of \code{volume}
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{volume}
\end{alltt}
\begin{verbatim}
[1] 351 955 662 1203 557 460
\end{verbatim}
\end{kframe}
\end{knitrout}
To extract the final element of \code{volume}, do:
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{volume[}\hlnum{6}\hlstd{]}
\end{alltt}
\begin{verbatim}
[1] 460
\end{verbatim}
\end{kframe}
\end{knitrout}
For the ratio of weight to volume, i.e., the density, we can do:
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{weight}\hlopt{/}\hlstd{volume}
\end{alltt}
\begin{verbatim}
[1] 0.7123 0.8796 0.8308 1.1305 1.1490 0.9130
\end{verbatim}
\end{kframe}
\end{knitrout}
\subsection*{A note on functions}
For the \code{weight/volume} calculation, two decimal places in the output
is more than adequate accuracy. The following uses the function
\code{round()} to round to two decimal places:
\begin{marginfigure}[24pt]
More simply, type:
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{round}\hlstd{(weight}\hlopt{/}\hlstd{volume,} \hlnum{2}\hlstd{)}
\end{alltt}
\end{kframe}
\end{knitrout}
Providing the arguments are in the defined
order, they can as here be omitted from
the function call.
\end{marginfigure}
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{round}\hlstd{(}\hlkwc{x}\hlstd{=weight}\hlopt{/}\hlstd{volume,} \hlkwc{digits}\hlstd{=}\hlnum{2}\hlstd{)}
\end{alltt}
\begin{verbatim}
[1] 0.71 0.88 0.83 1.13 1.15 0.91
\end{verbatim}
\end{kframe}
\end{knitrout}
\marginnote[11pt]{Many functions, among them \margtt{plot()} that is used
for Figure \ref{fig:denplot}, accept unnamed as well as named
arguments. The symbol `\margtt{\ldots}' is used to denote the possibility of
unnamed arguments.} Functions take {\em arguments} --- these supply
data on which they operate. For \code{round()} the arguments are
`\code{x}' which is the quantity that is to be rounded, and
`\code{digits}' which is the number of decimal places that should
remain after rounding.
\marginnote[12pts]{If a `\code{\ldots}' appears, indicating that there can
be unnamed arguments, check the help page for details.}
Use the function \code{args()} to get details of the named
arguments:
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{args}\hlstd{(round)}
\end{alltt}
\begin{verbatim}
function (x, digits = 0)
NULL
\end{verbatim}
\end{kframe}
\end{knitrout}
\subsection*{Tabulation}
Use the function \code{table()} for simple numeric tabulations,
thus:
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{type} \hlkwb{<-} \hlkwd{c}\hlstd{(}\hlstr{"Guide"}\hlstd{,}\hlstr{"Guide"}\hlstd{,}\hlstr{"Roadmaps"}\hlstd{,}\hlstr{"Roadmaps"}\hlstd{,}
\hlstr{"Roadmaps"}\hlstd{,}\hlstr{"Guide"}\hlstd{)}
\hlkwd{table}\hlstd{(type)}
\end{alltt}
\begin{verbatim}
type
Guide Roadmaps
3 3
\end{verbatim}
\end{kframe}
\end{knitrout}
\subsection*{A simple plot}
Figure \ref{fig:denplot} plots \code{weight} against \code{volume},
for the six Australian travel books. Note the use of the graphics
formula \verb!weight ~ volume! to specify the $x-$ and
$y-$variables. It takes a similar from to the ``formulae'' that are
used in specifying models, and in the functions \code{xtabs()} and
\code{unstack()}.
\begin{marginfigure}[-24pt]
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}
{\centering \includegraphics[width=0.98\textwidth]{figs/01-denplot-1}
}
\end{knitrout}
\caption{Weight versus volume, for six Australian travel
books.}\label{fig:denplot}
\end{marginfigure}
Code for Figure \ref{fig:denplot} is:
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlcom{## Code}
\hlkwd{plot}\hlstd{(weight} \hlopt{~} \hlstd{volume,} \hlkwc{pch}\hlstd{=}\hlnum{16}\hlstd{,} \hlkwc{cex}\hlstd{=}\hlnum{1.5}\hlstd{)}
\hlcom{# pch=16: use solid blob as plot symbol}
\hlcom{# cex=1.5: point size is 1.5 times default}
\hlcom{## Alternative}
\hlkwd{plot}\hlstd{(volume, weight,} \hlkwc{pch}\hlstd{=}\hlnum{16}\hlstd{,} \hlkwc{cex}\hlstd{=}\hlnum{1.5}\hlstd{)}
\end{alltt}
\end{kframe}
\end{knitrout}
The axes can be labeled:
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{plot}\hlstd{(weight} \hlopt{~} \hlstd{volume,} \hlkwc{pch}\hlstd{=}\hlnum{16}\hlstd{,} \hlkwc{cex}\hlstd{=}\hlnum{1.5}\hlstd{,}
\hlkwc{xlab}\hlstd{=}\hlstr{"Volume (cubic mm)"}\hlstd{,} \hlkwc{ylab}\hlstd{=}\hlstr{"Weight (g)"}\hlstd{)}
\end{alltt}
\end{kframe}
\end{knitrout}
Interactive labeling of points \marginnote{Use \margtt{text()} for
non-interactive labeling of points.} (e.g., with species names) can
be done interactively, using \code{identify()}:
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{identify}\hlstd{(weight} \hlopt{~} \hlstd{volume,} \hlkwc{labels}\hlstd{=description)}
\end{alltt}
\end{kframe}
\end{knitrout}
Then click the left mouse button above or below a point, or on the
left or right, depending on where you wish the label to appear.
Repeat for as many points as required.
On most systems, the labeling can be terminated by clicking the right
mouse button. On the Windows GUI, an alternative is to click on the
word ``Stop'' that appears at the top left
of the screen, just under ``Rgui'' on the left of the blue panel
header of the R window. Then click on ``Stop locator''.
\subsection*{Formatting and layout of plots}
There are extensive abilities that may be used to control
the formatting and layout of plots, and to add features such as
special symbols, fitted lines and curves, annotation (including
mathematical annotation), colors, \ldots
\section{Data frames -- Grouping columns of data}\label{sec:df}
\marginnote{Data frames are pervasive in R. Most datasets that are
included with R packages are supplied as data frames.}
\fbox{\parbox{\textwidth}{
\begin{tabular}{ll}
Data frames & Store data that have a cases by
columns layout.\\[6pt]
Creating & Enter from the command line (small datasets)\\
data frames & Or: Use \code{read.table()} to input from a file.\\[6pt]
Columns of & \code{travelbooks\$weight} or \code{travelbooks[, 4]}\\
data frames & or \code{travelbooks[, "weight"]}\\
\end{tabular}
}}
\vspace*{8pt}
The following code groups the several columns of Table
\ref{tab:travel} together, under the name \code{travelbooks}. It is
tidier to have matched columns of data grouped together into a data
frame, rather than separate objects in the workspace.
\marginnote[9pt]{The vectors \margtt{weight}, \margtt{volume}
and \margtt{description} were entered earlier, and (unless subsequently
removed) can be copied directly into the data frame.}
{\small
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlcom{## Group columns together into a data frame}
\hlstd{travelbooks} \hlkwb{<-} \hlkwd{data.frame}\hlstd{(}
\hlkwc{thickness} \hlstd{=} \hlkwd{c}\hlstd{(}\hlnum{1.3}\hlstd{,} \hlnum{3.9}\hlstd{,} \hlnum{1.2}\hlstd{,} \hlnum{2}\hlstd{,} \hlnum{0.6}\hlstd{,} \hlnum{1.5}\hlstd{),}
\hlkwc{width} \hlstd{=} \hlkwd{c}\hlstd{(}\hlnum{11.3}\hlstd{,} \hlnum{13.1}\hlstd{,} \hlnum{20}\hlstd{,} \hlnum{21.1}\hlstd{,} \hlnum{25.8}\hlstd{,} \hlnum{13.1}\hlstd{),}
\hlkwc{height} \hlstd{=} \hlkwd{c}\hlstd{(}\hlnum{23.9}\hlstd{,} \hlnum{18.7}\hlstd{,} \hlnum{27.6}\hlstd{,} \hlnum{28.5}\hlstd{,} \hlnum{36}\hlstd{,} \hlnum{23.4}\hlstd{),}
\hlkwc{weight} \hlstd{= weight,} \hlcom{# Use values entered earlier}
\hlkwc{volume} \hlstd{= volume,} \hlcom{# Use values entered earlier}
\hlkwc{type} \hlstd{=} \hlkwd{c}\hlstd{(}\hlstr{"Guide"}\hlstd{,} \hlstr{"Guide"}\hlstd{,} \hlstr{"Roadmaps"}\hlstd{,} \hlstr{"Roadmaps"}\hlstd{,}
\hlstr{"Roadmaps"}\hlstd{,} \hlstr{"Guide"}\hlstd{),}
\hlkwc{row.names} \hlstd{= description}
\hlstd{)}
\hlcom{## Remove objects that are not now needed.}
\hlkwd{rm}\hlstd{(volume, weight, description)}
\end{alltt}
\end{kframe}
\end{knitrout}
}
\marginnote[-36pt]{It is a matter of convenience whether the description
information is used to label the rows, or alternatively placed in a
column of the data frame.}
\subsection*{The storage of character data as factors}
\marginnote{While in most contexts factors and character vectors are
interchangeable, there are important exceptions.}
Vectors of character, such as \code{type}, are by default stored in
the data frame as {\em factors}. In the data as stored,
\code{Guide} is 1 and \code{Roadmaps} is 2. Stored with the factor
is an attribute table that interprets 1 as \code{Guide} and 2 as
\code{Roadmaps}.
\subsection*{Accessing the columns of data frames}
\marginnote[12pt]{For a matrix or array, users are restricted to
the first and second of these alternatives. With a matrix
\margtt{travelmat} use, e.g., \margtt{travelmat[,4]} or
\margtt{travelmat[,"weight"]}.}
The following are alternative ways to extract the column
\code{weight} from the data frame:
{\small
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{travelbooks[,} \hlnum{4}\hlstd{]}
\hlstd{travelbooks[,} \hlstr{"weight"}\hlstd{]}
\hlstd{travelbooks}\hlopt{$}\hlstd{weight}
\hlstd{travelbooks[[}\hlstr{"weight"}\hlstd{]]} \hlcom{# Reference as a list.}
\end{alltt}
\end{kframe}
\end{knitrout}
}
%$
There are several mechanisms that avoid repeated
reference to the name of the data frame.
The following are alternative ways to plot \code{weight} against \code{volume}:
\begin{itemizz}
\item[]{\em 1. Use the parameter \code{data},
\marginnote{Most modeling functions and many plotting functions accept a
\margtt{data} argument.}
where available, in
the function call}\\[-2pt]
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{plot}\hlstd{( weight} \hlopt{~} \hlstd{volume,} \hlkwc{data}\hlstd{=travelbooks)}
\end{alltt}
\end{kframe}
\end{knitrout}
\item[]{\em 2. Use \code{with()}: Take columns from specified
data frame}\\[-2pt]
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlcom{## Take columns from the specified data frame}
\hlkwd{with}\hlstd{(travelbooks,} \hlkwd{plot}\hlstd{(weight} \hlopt{~} \hlstd{volume))}
\end{alltt}
\end{kframe}
\end{knitrout}
\vspace*{-3pt}
\noindent
\end{itemizz}
Both these mechanisms look first for a data frame column with a needed
name. The workspace is searched only if this fails.
\begin{marginfigure}[84pt]
Attachment of a data frame:
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{attach}\hlstd{(travelbooks)}
\hlkwd{plot}\hlstd{( weight} \hlopt{~} \hlstd{volume)}
\hlkwd{detach}\hlstd{(travelbooks)}
\hlcom{## Detach when no longer}
\hlcom{## required.}
\end{alltt}
\end{kframe}
\end{knitrout}
\end{marginfigure}
A third option, usually best avoided,
is to use \code{attach()} to add the data frame to the search list. In
this case, names in the workspace take precedence over column names in
the attached data frame -- not usually what is wanted if there are
names in common.
Subsection \ref{ss:moreattach} will discuss the attaching of packages
and image files.
\section{Input of Data from a File}\label{sec:input}
The function \code{read.table()} is designed for input from a
rectangular file into a data frame. There are several variants on this
function --- notably \code{read.csv()} and \code{read.delim()}.
\marginnote[12pt]{This use of \margtt{datafile()}, avoiding
use of the mouse to copy the file and the associated need
to navigate the file system, is a convenience for teaching
purposes.} First use the function \code{datafile()}
(\pkg{DAAG}) to copy from the \pkg{DAAG} package and into the
working directory a data file that will be used for demonstration purposes.
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlcom{## Place the file in the working directory}
\hlcom{## NB: DAAG must be installed}
\hlstd{DAAG}\hlopt{::}\hlkwd{datafile}\hlstd{(}\hlstr{"travelbooks"}\hlstd{)}
\end{alltt}
\end{kframe}
\end{knitrout}
\noindent
Use \code{dir()} to check that the file is indeed in the working directory:
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{dir}\hlstd{(}\hlkwc{pattern}\hlstd{=}\hlstr{"travel"}\hlstd{)}
\hlcom{# File(s) whose name(s) include 'travel'}
\end{alltt}
\end{kframe}
\end{knitrout}
The first two lines hold the column headings and first row, thus:\vspace*{9pt}
\noindent
{
\begin{tabular}{rrrrrrl}
\hline
& thickness & width & height & weight & volume & type \\
\hline
Aird's Guide to Sydney & 1.30 & 11.30 & 23.90 & 250 & 351 & Guide \\
. . .
\end{tabular}
}
\vspace*{3pt}