Académique Documents
Professionnel Documents
Culture Documents
1
2
PART I: INSTALLING AND USING SCIVIEWS-R.......................................................... 6
1. INTRODUCTION ....................................................................................................... 7
3
THE OBJECT EXPLORER AND DATA EDITOR ......................................................................... 83
OTHER “EXPLORERS”............................................................................................................ 85
VIEWS AND REPORTING FEATURES ....................................................................................... 86
FACILITATED INPUT AND OUTPUT......................................................................................... 88
MANAGEMENT OF THE SYSTEM ............................................................................................ 88
OTHER ASSISTANTS ................................................................................................................ 88
SIMPLIFIED ONLINE HELP AND MANUALS............................................................................. 89
TEXT EDITOR AND IDE .......................................................................................................... 89
4
11. TCLTK2: ADDITIONAL TCL/TK FEATURES FOR R ...ERROR! BOOKMARK
NOT DEFINED.
5
PART I: INSTALLING AND USING SCIVIEWS-R
6
1. INTRODUCTION
data and code. In this paradigm, an ‘object’ contains both the data and the code
to manipulate it, and the way data is manipulated depends on the “nature” of
the object (which is called its ‘class’).
7
Maechler for this suggestion and for providing these data). We propose
to analyze these data using R itself:
If this looks like some intimidating code to you, you just have to
know that we constructed a special R object ‘RhelpUsage’, of class ‘ts’ (a
regular time-series) with monthly intervals. Here is a friendlier textual
presentation of its content (the default print for this ‘ts’ object):
> RhelpUsage
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
1997 55 19 19 18 19 17 35 27 47
1998 55 32 50 55 41 49 50 28 53 42 81 54
1999 99 60 84 80 76 75 78 61 83 97 141 122
2000 96 144 173 153 226 202 131 165 183 175 168 187
2001 240 272 262 195 236 244 285 249 326 345 392 268
2002 455 320 418 453 468 422 447 400 323 516 478 327
2003 450 487 535 658 573 606 659 543 655 722 677 567
2004 519 703 886
> plot(RhelpUsage)
8
800
600
RhelpUsage
400
200
0
Time
3 The reason why we use a log transformation here is not important for
the sake of the demonstration. For those who want to know, it is because the
high-frequency signal appears multiplicative with the general trend (its
amplitude increases proportionally with the value of this trend). In this case, a
log transformation makes those two signals additive and stabilizes the variance
of the high-frequency signal (i.e., the residuals in the models we apply, further
in the analysis).
9
> LogRhU <- log(RhelpUsage)
> plot(LogRhU)
6
LogRhU
5
4
3
Time
> library(pastecs)
Loading required package: boot
> RhU.turno <- turnogram(RhelpUsage, FUN = sum)
> summary(RhU.turno)
Simple turnogram for: RhelpUsage
10
7 7 12 0 27.8354558
8 8 11 1 14.2519023
9 9 10 1 11.7938816
10 10 9 1 9.4747796
11 11 8 1 7.3105233
12 12 7 0 11.2992080
13 13 7 1 5.3219281
14 14 6 0 8.4918531
15 15 6 1 3.5376568
16 16 6 1 3.5376568
10
0
1 2 5 10
interval ()
sum / two-tailed probability
11
4000
3000
RhU6
2000
1000
0
Time
Indeed, the series looks much cleaner now, isn’t it? Notice how simple
was the analysis (once you know which function you have to call, of
course!). We just had to load a package containing additional functions
called ‘pastecs’ with the instruction library(pastecs). Then, we used a
specialized function called turnogram() on our data with an argument,
FUN = sum. We got a summary of the results simply by calling …
summary() on the object we just created… and extraction of the
optimized series was not more complex, just calling extract() on the
same object! Let’s look now at the graph of the log-transformed form of
our time series:
12
8.0
7.5
7.0
LogRhU6
6.5
6.0
5.5
5.0
Time
Can we fit a linear model on this? (take care! This is a time series.
There is probably some autocorrelation. So, the ANOVA on the
regression and the t-tests on parameters are not valid here!)
Call:
lm(formula = LogRhU6 ~ time(LogRhU6))
Residuals:
Min 1Q Median 3Q Max
-0.23652 -0.10947 0.03873 0.12922 0.20945
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -1.018e+03 4.095e+01 -24.85 1.09e-11 ***
time(LogRhU6) 5.121e-01 2.047e-02 25.02 1.01e-11 ***
---
Signif. codes: 0 `***' 0.001 `**' 0.01 `*' 0.05 `.' 0.1 ` ' 1
13
8.0
7.5
7.0
LogRhU6
6.5
6.0
5.5
5.0
Time
Parameters:
Estimate Std. Error t value Pr(>|t|)
Asym 11.1333 0.9797 11.364 2.03e-07 ***
lrc -2.1036 0.2320 -9.067 1.95e-06 ***
c0 1992.3589 0.6269 3177.920 < 2e-16 ***
---
Signif. codes: 0 `***' 0.001 `**' 0.01 `*' 0.05 `.' 0.1 ` ' 1
14
> plot(df1$time, df1$load, type = "l")
> lines(df1$time, predict(LogRhU6.nls), col = 2)
8.0
7.5
7.0
df1$load
6.5
6.0
5.5
5.0
df1$time
15
> abline(v = 1998:2003, col= "gray", lty = 2)
> # Horizontal grid lines
> abline(h = (1:9)*1000, col= "gray", lty = 2)
8000
nbr. of messages / semester
6000
4000
2000
0
time (years)
16
If you feel comfortable with this user interface, then probably you
do not need the SciViews-R extension. You may even consider using
(X)Emacs with ESS extension (http://stat.ethz.ch/ESS/) if you want
more power to control R from a textual-based user interface. Windows
users may also be interested by
http://socserv.mcmaster.ca/jfox/Books/Companion/ESS/ which
explains how to install and configure Xemacs and ESS under Windows4.
4 Note from Marta Rufino : it seems that Emacs could cause problems
17
writing its code, to show him by the example how to write a script for a
given analysis.
18
companion applications. R-wxPython
(http://bioinf.wehi.edu.au/folders/james/wxPython/) uses RSPython
(http://www.omegahat.org/RSPython/), but RPy
(http://rpy.sourceforge.net/) is a possible, although unidirectional,
alternative to write a Python/wxWidgets GUI for R. We warmly
encourage any initiative in this direction and will support and help any
volunteer that would like to write a platform-independent version of
SciViews-R with these tools. Remember: your contributions are
welcome!
Acknowledgements
We would like to tank the R Core Team for providing such an
excellent piece of software as R. We are particularly grateful to Duncan
Murdoch for his efforts in implementing features for R that ease writing
of a GUI on top of it. We also thank Peter Dalgaard for the tcltk package
and James Wettenhall for his preliminary version of R-wxPython.
19
2. INSTALLATION OF SCIVIEWS-R, R AND R PACKAGES
> R.Version()
> capabilities("tcltk")
> search()
20
Condensed installation instructions
TO DO…
21
Figure 2-1: the welcome screen of the SciViews-R 0.7-0 setup assistant. If you
see this screen after launching ‘SciViews-R_0.7-0.exe’, you are certainly logged
with administrative rights on Windows 98se, 2000, or XP (otherwise, an error
message explains the origin of the problem). Click ‘Next’.
Figure 2-2: second screen of the assistant. You have to accept the license
terms (click ‘I accept the agreement’) to be allowed to continue. Click ‘Next’.
22
Figure 2-3: the setup assistant asks you where you want to install SciViews-R.
You can install it where you want, preferably on a local hard disk with enough
free space. Most of the time, the default directory is fine. Click ‘Next’.
23
Figure 2-5: it is strongly advised to create shortcuts for the programs in the
start menu. The default folder ‘SciViews-R’ should be fine. Just click ‘Next’.
Figure 2-6: this screen presents various additional tasks. You can create a
desktop icon for the program, either for yourself only, or for all users of this
computer. This is a convenient shortcut to start R with the SciViews-R interface
24
rapidly. If you had to log off your account, and log on as administrator, make
sure you select ‘For all users’, otherwise this shortcut will not be available in
your own user session. The other task is to associate files with specific
extensions to SciViews-R applications… there is no reasons for not doing this.
So, leave the ‘Associate files’ option checked and click ‘Next’.
Figure 2-7: a summary of tasks that will be done if you click ‘Install’. You can
check if everything is fine, and then start the installation with ‘Install’ (or exit
it with ‘Cancel’). Note that you can fully uninstall SciViews-R at a later time by
going in Start menu -> control panel -> Add or Remove Programs ->
SciViews-R version 0.7-2.
25
Figure 2-8 : the installation of SciViews-R is in progress…
Figure 2-9 : SciViews-R is now installed. It is generally a good idea to read the
README file, which may provide latest news and information. Click ‘Finish’.
26
Installing R under Windows
Once SciViews-R is installed on your computer, it is not yet
operational: you also need to install R and a couple of additional R
packages to be able to use it. It is not important whether you install
SciViews-R before or after R, but you must make sure you install at least
R version 2.2.0 (SciViews-R is not compatible with previous version of
R)! You can download a setup for Windows of the latest R version from
http://cran.r-project.org/bin/windows/base/. The file you need is ‘R-
X.X.X-win32.exe’ where X.X.X is the version (for instance, 2.2.0). It
weights about 25Mb. You can also download it from one of the CRAN
mirror sites. If you have time, browse the README and the FAQ from
this web page. These give useful additional information about R.
Figure 2-10: the welcome screen of the R setup assistant. Click ‘Next’.
27
Figure 2-11: the license agreement. You must click ‘I accept the agreement’ to
be allowed to continue. Then, click ‘Next’.
Figure 2-12: where do you want to install R? Usually, the default directory
provided is fine. Especially if you run R with SciViews-R, it is advised (but not
required) to keep this default directory. Click ‘Next’.
28
Figure 2-13: choose components. Select as in the figure. Make sure ‘Support
Files for library(tcltk)’ is checked! Click ‘Next’.
Figure 2-14: it is advised to create a shortcut in the start menu. The ‘R’ folder
should be fine. So, you do not need to change default options. Click ‘Next’.
29
Figure 2-15: additional tasks. It does not hurt to run them all. So, unless you
know what you do, check all options and then click ‘Next’.
30
Figure 2-17: R is now installed on your computer! Just click ‘Finish’.
31
Figure 2-18: locate the ‘RSciViews’ shortcut in the SciViews-R installation
directory with the files explorer.
Figure 2-19: the context menu when you right-click on the ‘RSciViews’
shortcut. Select ‘Properties’.
32
3. Select ‘properties’ in this menu. The ‘RSciViews Properties’ dialog
box is displayed (fig. 2-20).
Figure 2-20: the ‘RSciViews Properties’ dialog box. You have to fix the ‘Target’
entry in the ‘Shortcut’ tab.
4. Change the ‘Target’ field in the ‘Shortcut’ tab to reflect the actual
location where you installed R. By default, this target is:
"C:\Program Files\R\R-2.2.0\bin\Rgui.exe" --sdi
RSciViews.RData.
33
Installing additional R packages
Figure 2-21: you can start SciViews R Console from its shortcut in the
Start menu.
34
3. The SciViews bundle is probably missing on your computer at this
point. So, R should prompt you to install it (fig 2-22).
Figure 2-22: the message that appears the first time you start SciViews R
Console when the SciViews bundle is missing. If your computer is connected to
Internet, click ‘Yes’ and continue. Otherwise, click ‘No’ and follow instructions
in the next section to install it manually.
4. R gives you now the choice of several places from where you can
install the SciViews bundle. Since it is currently only available on
the SciViews web site, select the first entry, ‘SciViews’, exclusively
(see fig. 2-23).
Figure 2-23: select from where you want to install the ‘SciViews’ bundle.
Currently, the only valid location is the ‘SciViews’ web site (it may be installable
from other locations as well in the future). Select this entry and click ‘OK’.
5. The bundle is automatically installed from the web site (fig. 2-24).
Figure 2-24: the ‘Download progress’ dialog box while the SciViews bundle is
being downloaded. Then it is automatically installed on your computer (version
number may change: you got always the very last one from the web site).
35
select ‘Local Zip File’ and locate the file where you saved it. This
way, you do not have to download it every time from the Internet!
Figure 2-25: here is the text that appears in the ‘R Console’ when the SciViews
bundle is correctly installed. You have the opportunity to save the installation
file to reuse it in the future (enter ‘N’), otherwise, enter ‘y’.
Figure 2-26: you are now prompted to install the ‘R2HTML’ package. Proceed
and install it the same way (click ‘Yes’), but select either ‘SciViews’, or ‘CRAN’
in the list this time.
36
9. Check that the R2HTML package is correctly installed (fig. 2-27),
and enter either ‘y’, or ‘N’, depending if you want to save the
installation file of R2HTML for reuse or not.
Figure 2-27: now, R2HTML is installed in R. Click ‘y’ to delete installation file
and continue, or ‘N’ to save it and continue.
37
10. After a while, you should see the main window of SciViews R
Console, as in fig. 2-28. The SciViews-R graphical user interface
is now installed properly on your computer!
Figure 2-28: the main SciViews R Console window that appears when
SciViews-R, R and required additional R packages are correctly installed on
your computer. Note that the ‘Dock1’ and ‘Dock2’ panels at right could be
different, or even not be displayed at this stage. This is perfectly normal.
38
Figure 2-29: the error message displayed when SciViews R Console cannot
connect to R. It proposes an alternate way to start it than the one explained
here. You can either retry the current procedure (close R and close this dialog
box, and then try again launching SciViews R Console from its icon in the start
menu or on the desktop), or you could try this alternate procedure (see later in
this manual: ‘starting and stopping the SciViews-R GUI’ at the end of this
chapter). Note that the message anticipates the availability of the SciViews
bundle on CRAN (http://cran.r-project.org), while it is currently only available
at http://www.sciviews.org/SciViews-R!
> capabilities("tcltk")
It should answer TRUE. Otherwise, reinstall R and check the tcltk feature
this time in optional components. Then, you will need the following
additional R packages/bundles (table 2-1):
39
Table 2-1: additional packages required to run SciViews-R and R Commander.
40
1. Download all windows binaries of the R packages you need (files
with names ‘package_version.zip’) in a common directory on your
hard disk, or better, on a network directory or a CD-Rom if you
want to install SciViews-R on several computers. Use table 2-1 to
determine which R packages/bundles you need.
2. Start R from its icon in the desktop, or from the Start menu ->
All programs -> R -> R x.x.x.
4. In the ‘Select files’ dialog box, navigate to the folder where you
saved your files, and select them all (fig. 2-30). Click ‘Open’.
Figure 2-30: select all the packages you downloaded in your installation
directory. R will install them all in turn.
41
Figure 2-31: the messages you should get when all the additional packages
and bundles are correctly installed in R.
3. Start R alone (either from its desktop icon, or from the Start
menu -> All programs -> R -> R x.x.x), or in any other way
you use to start R (for instance, from a .Rdata file, look at R
documentation). In this case, RGui must be started in SDI mode
exclusively –see hereunder, ‘how to start RGui in SDI mode’–. If
you enter the following instruction:
> library(svGUI)
42
The ‘svGUI’ package is loaded and it automatically starts ‘SciViews
R Console’, providing ‘RConsole.exe’ is located in ‘C:\Program
Files\SciViews\bin’. [PhG TO DO: explain how to customize
this!!!]. This third way to start SciViews R Console is
interesting if you want to save/load workspaces from
session to session (see R documentation if you don’t know what
a workspace is [PhG… or explain this here]).
Whatever the way you start ‘SciViews R Console’, you should end
up with a window similar to this (fig. 2-31; the ‘Dock1’ panel at right is
probably not the same at this stage, or it can even not be visible… this is
normal):
Note that you can start the SciViews R Console from its icon, and
stop it by selecting File -> Exit SciViews only, or by clicking on the
top-left ‘x’ button to close the window without closing the R session,
once the svGUI package is loaded. You can thus switch from the plain
‘Rgui’ to ‘SciViews R Console’ back and forth several times in the same
session! This way, it is always possible to free some memory by
temporarily switching the GUI extensions off, if required. If you just
want to hide or show these extensions, but do not care about memory
usage, you can also use Misc -> Hide/Show bars, or Ctrl-H in the
SciViews R Console.
43
How to start RGui in SDI mode
Figure 2-32: RGui started in MDI mode (default mode). The main window,
called ‘RGui’ contains one or several child windows like ‘R Console’.
44
Figure 2-33: RGui started in SDI mode the large, parent window called ‘RGui’
is not there any more. Only ‘R Console’ is there, and the main menu is displayed
in this window. SciViews R Console can only be started when RGui is in this
mode.
1. Start R (RGui) from its desktop icon, or from the start menu.
45
Figure 2-34: The Rgui Configuration Editor. You have to select ‘SDI’ in the top
of this dialog box to switch RGui in SDI mode.
7. Stop RGui (File -> Exit). Click ‘No’ at the question “Save
workspace image?”.
8. Start R again from its desktop icon, or Start menu entry. You
should have RGui running in SDI mode now (fig. 2-33).
46
Figure 2-35: the file selector displayed when you click ‘Save’ in the Rgui
Configuration Editor. Do not change the default file and path proposed and just
click ‘Save’.
[TO DO: explain how one can save and restore sessions in R /
SciViews-R; explain also the difference between the workspace and the
script edited in the command bar… this appears to be often confusing
for beginners!].
47
3. COMPANION APPLICATIONS FOR WINDOWS IN SCIVIEWS-R
48
SciViews R Console
SciViews R Console is the main application. It consists in an
enhanced Graphical User Interface (GUI) you can use instead on top of
the basic Rgui that comes with R (providing it is running in SDI mode,
see ‘installation of SciViews-R and the SciViews bundle’). Since SciViews
R Console runs with Rgui and do not replace it, it remains 100%
compatible with the latter. No worry thus, about compatibility issues
between the two!
49
From classical Rgui (in SDI mode)…
50
At any time, you can come back to the classical Rgui:
51
Command bar / Script Editor
The command bar allows editing R code and acts as a rapid script
editor (look at Tinn-R for a fully-featured code editor). Associated
toolbar includes buttons to:
52
For your convenience, you can drag and drop items from the
object explorer (see hereunder) to the script. If it is a function, the
opening bracket is added automatically as well as the corresponding
code tip. Double-click on an item in the object explorer has the same
effect, except that it will send the variable name either to the script or
directly to the console, depending which one currently owns the caret.
With double-click, the name of the variable is naturally send to the caret
position, while with drag and drop you can freely decide where to drop
it. You can even use drag and drop from the object explorer to any
external text editor that is drag&drop compatible!
Please, note that both code tips and completion lists are
only able to display items that are currently visible from the
user workspace. It is not context specific when you call it from within
a function you write in the script window, and it will not show items of
your script if you have not run it yet!
53
Warning! Changes you make in the Properties… dialog
box of the script panel are not saved yet between sessions.
This will be fixed in further releases.
The main goal of this command bar is the edition of small pieces of
code, when debugging but it turns to be also a simple multiline code
editor for the R console. You could either submit the whole (selected)
code or press repeatedly F5 key to submit it line by line (step-by-step
execution). For more features and comfort to edit large pieces of R code,
consider an external code editor, like Tinn-R (see later in this manual).
Dock windows
[TO DO: M.R. -> explain how do we change and customize the
dockable windows. For example, if I want to create one that is called
‘geostatistics’ and opens the library gstat and geoR, and has the graph
whatever… I do I do it ? I saw in the example that you can create other
dockwindows, but when I press customize it states that it is still not
implemented. Ph.G.: yes, good idea, we should add a section explaining
how to write such dock contents]
Libraries
Libraries are web pages that can interact with the R Console, or
the script panel. Since you use HTML code, you are only limited by your
imagination here! In this version of SciViews-R, we have only added a
few of them, to illustrate their great potentials. Here is an overview of
the few libraries that come with the program:
54
R Reference card is an electronic Colour picker allows choosing
version of Jonathan Baron’s R colours. Hexadecimal RGB
reference card. Items are classified representation code is inserted
by categories and could be clicked to within your R session [to do: link to
be inserted in your R session. ‘RColorBrewer’ + include ideas from
http://geography.uoregon.edu/dat
agraphics/color_scales.htm].
55
Libraries are stored in the user directory. You can add your own
libraries (a document will explain how to do that in future versions; in
the meantime, just study the code of example libraries).
Scratchpads
Object Explorer
56
The contextual menus of the object explorer are generated in pure
R code in the current R session. They adapt, thus, to the current state of
R, and custom items can be easily added, just with R functions. The list
of available views, by example, is generated on the fly, so that if you add
a view it will appear next time you right-click on an object of the given
class. [TO DO: redraw the picture: bad resolution, unreadable in PDF!]
57
1. Call Customize dialog box…
Enter the R code to submit with this button and press OK.
58
You can change other attributes of the button if you want (icon,
caption, description, tooltip text…).
Close the customize dialog box. A list of all custom buttons and
corresponding R commands appear. You can put it in clipboard and
paste it in a text editor to remember those commands you added to the
toolbars/menus.
59
SciViews R Report
60
SciViews R Report is a separate, lightweight
HTML editor that communicates with SciViews R
Console to easily build nicely formatted reports
during the analysis. Interactions are the
following:
> report(iris)
61
Tinn-R, a R code editor
Tinn is a text editor developed with Borland Delphi 5 for
Windows. It is designed as a small, simple, but richer replacement of the
default Notepad.exe under Windows. It is distributed under a GPL
license.
62
Tips for using Tinn-R
63
3. All highlighters are user configurable:
64
65
6. The specific R toolbar can be placed everywhere on the screen:
7. Files with *.r, *.R, *.q and *.Q extensions are recognized as R/S-
PLUS scripts. If the file does not have one of these extensions, or if it
has not been saved yet, some R tools are disabled
66
8. To send code line by line from Tinn-R to RGui, it is
recommended to dispose the two windows (RGui and Tinn-R) side-by-
side. If you want to place Tinn-R on top of Rgui / R Console or SciViews
R Console, you have to set the ‘On Top’ option of Tinn-R for this
purpose (the button with an icon representing a pushpin):
There are far more tools and features in Tinn-R. You can discover
them by using the program and experimenting with its menus. You can
also read the Tinn-R FAQ, whose latest version is available at:
http://www.sciviews.org/tinn-R/Tinn-R_FAQ.txt.
67
Using SciViews R Console with R Commander
R Commander (Rcmdr) is an R package from John Fox that offers
a basic-statistics Tcl/Tk GUI for R. You can download it from CRAN
(http://cran.r-project.org), or from John Fox’s web pages
(http://socserv.socsci.mcmaster.ca/jfox/Misc/Rcmdr/). The target
audience consists mainly in student users, for teaching purpose. Under
SciViews, Rcmdr seamlessly integrates within its environment, starting
from version 0.9-12 (but SciViews-R 0.8-9 requires at least Rcmdr 1.1-1).
The main window of R Commander is not displayed there. SciViews uses
instead a dock menu. State (which data set and which model is in use) is
displayed in the status bar of SciViews R Console. Log of commands is
done directly in the script panel.
68
4. PRESENTATION OF SOME FUNCTIONS IN THE SCIVIEWS BUNDLE
For a given object (let’s say a data frame), we would like to make
different types of exportation available from the same function, like raw
text, HTML, LaTeX… The argument type controls this. Depending on
the object, different types of exportation may be available (including
custom ones). To know which types are available for a given object in a
given environment, use export(x, type = "typelist") as shown below:
> library(svIO)
> data(iris)
> export(iris, type = "typelist")
[1] "raw" "ascii" "html" "latex" "sascode"
ascii: exports a text (ASCII) version of the object, with values separated
by tabulations. This version could be read directly by many
external applications, such as text editors, spreadsheets, or other
statistical software.
html: exports the object as HTML code (we may consider in the future to
export as XML).
69
latex:exports the object as LaTeX code. It uses the code from HMisc
package, which is thus required. Useful for pasting the object in a
LaTeX document.
sascode: export the SAS code which defines the object within SAS (only
for data frames).
You may easily add your own export types, by adding a function
whose name is: export.class..type. Please, refer to the second part of
this manual for further information on how to extend features of the
SciViews bundle.
Instead of exporting the whole object, one could also export a part
of it or the result of any calculation: export also accepts expressions as
first argument. Keep in mind, however, that the expression will always
be evaluated in the user workspace, whatever the place it is called (a
restriction required by the GUI extensions of SciViews-R). Use get(x,
pos = ) to retrieve an object in a particular environment.
70
> export(as.data.frame(apply(iris[ , 1:4], 2, mean)), file = "",
+ type = "sascode")
DATA work.asdataframeapplyiris_14_2_mean;
INFILE cards missover;
INPUT applyiris142mean ;
CARDS;
5.84333333333333
3.05733333333333
3.758
1.19933333333333
;
RUN;
[to do: an example using the argument append, and also, an example
with HTML format]
view functions
Main usage syntax:
view(x, type = )
Views are HTML pages that include graphs, text and nicely
formatted tables. Their primary goal is to provide insight on objects.
71
Comparing two different objects is as simple as looking at two views
side-by-side. As a side effect, they are also nice chunks of results to feed
your reports!
Each object has a default view, so that one could always simply call
view(x). Let’s try the default view on the iris data frame.
> view(iris)
The view function could work jointly with an external application (the
“viewer”). If such an external application is unavailable, the viewer will
be displayed in the default web browser (see browser function). A
dedicated viewer could add some functionality. The one provided within
SciViews R Console displays tabs for all available views and allows direct
inclusion of the current view in the report (see previous section).
Further control of views parameters is another, planned, additional
feature of the dedicated viewer.
72
Sepal.Length
Numeric
Range: 4.3-7.9
Coming back to the list of available views for data frames, we could
see that there is also a view called “twovars”. As this is not the default
view, we have to precise the type in order to call it:
73
This view presents graphs for all pairs of variable within the data frame
and a correlation matrix with different colors according to correlations
values.
74
Let’s recall views are HTML pages. They rely also on so-called
Cascading Style Sheets (CSS), which allow separating the content from
the formatting. You can change the colours of this correlation matrix by
editing the associated CSS file (in fact, you can even give colours only to
the correlation up to 0.9, by example). In SciViews R Console, you
switch to a different CSS file just by selecting a different entry in the
‘Style’ dropdown list that appears in the View window. We show here
the same correlation matrix, just using a different style:
clippaste function
Main usage syntax:
clippaste("name", type = ,objclass = )
The export and copy functions allow both exporting objects to the
clipboard. The function clippaste is complementary: it reads from the
clipboard and creates an R object from its content.
> library(svIO)
> copy(iris, type = "ascii")
> clippaste("irisCP")
> irisCP[1, 1]
[1] 5.1
75
An import function is planned for the future, as the counterpart of
export.
report function
report in svViews, together with all guiXXX functions (not
described here, see SciViews-Internals online help) in the svGUI
package won’t work without an external application. Its purpose is to
append a suitable representation of an R object to a report edited in that
external application. On Windows platform, SciViews-R proposes the
SciViews R Report application to create reports in HTML format and
rapidly publish them on the inter/intranet, or import it to Microsoft
Word for further edition.
76
PART II: THE SCIVIEWS GUI API
77
5. CONCEPTION OF A “CODE-CENTRED” GUI
78
Figure 5-1: The main window of R Commander (version 0.9-12). The lower
panel (1) collects inputs and outputs and is similar to a console, even if it is not
editable, if it collects only outputs from R Commander itself and if the actual R
console is not hidden (separate window). Above it (2), there is a simple script
editor. No syntax highlighting, or other help like code completion or code tips,
but the tendency is there: a script panel supplements a console! Finally, on top
(3), there is a menu and a toolbar. In R Commander, menus are particularly
rich and give access to many different dialog boxes to parameterize the
analyses.
79
Figure 5-2: The main window of JGR (version DP3). It is also centred on a
non-editable console (1), with the lower panel (2) being a multi-line code editor
with syntax highlighting and code completion. In this case, however, this panel
is not a true script editor: once a line is issued on the console, its content is
cleared from this panel. Yet, the concept is very close. It should not be difficult
to transform it into a real script editor (unless their programmers believe it is
an unnecessary duplication of the separate script edition windows of JGR). On
top, one finds the traditional menus and toolbar (3).
80
Figure 5-3: The main SciViews-R window (version 0.7-2). The console (1),
which is editable here, is supported by a script editor, called a ‘Command’
window. It permits the edition of one script at a time, with syntax colouring,
step-by-step execution, code tips and code completion (like here, a code
completion for iris list items). This panel also has its own little toolbar at left, in
supplement to the main menu and toolbar(s) at top (3). The script editor can be
docked at the bottom or at the top of the console. It can also be hidden or
separated (in this case, it floats on top of the console window).
81
Figure 5-4: an example of an R Commander dialog box: the independent t-
test.
> gui.t.test()
That’s it! The code that should appear is rather something like:
82
The object explorer and data editor
What makes the success of a spreadsheet-like GUI as Excel?
Everything is visible and can be handled with the mouse. Data,
calculations, graphs are all mixed in spreadsheets… and they are visible.
On the contrary, in R, data are stored in variables, themselves located in
environments… that is, abstract concepts for those who never
programmed before. They appear to be inaccessible, hidden, complex
objects in the eyes of a beginner stuck in front of a meaningless prompt!
83
Figure 5-6: The JGR object explorer (version DP3). Presentation and concept
is a little different, but the basic idea is the same: to materialize objects and
allow their handling with the mouse (you can drag and drop items to the
command line, for instance).
Figure 5-7: the object explorer of SciViews-R (version 0.8-9) is dockable at left
or at right of the main window. The top part allows filtering variables. The
middle part presents a tree with the various environments, and the lower part is
a list of variables in the selected environment (here in .GlobalEnv). Objects can
be drag-dropped in the script. A right click on an object displays a context menu
with a few pertinent commands for this object (here, for the data frame ‘iris’).
In particular, the ‘Edit’ entry pops up a data editor.
84
Figure 5-8: The basic data editor of R.
Other “explorers”
Besides the object explorer, one can imagine other explorers, with
the same idea to materialize, summarize and allow handling abstract
items with the mouse. A file explorer now exists in all recent graphical
platforms. SciViews-R proposes one in its dock windows. Files on disk
are thus very easily accessible from within SciViews-R! It also proposes
various other “explorers” to access code chunks or commands: dockable
“menus”, graph galleries, electronic reference cards, … (see next figure).
Such kind of explorers are only limited by the imagination: they are
indeed HTML pages with javascript code, so, one can very easily imagine
other forms of explorers, or dockable forms to support beginners in R.
85
Figure 5-9: Two docked windows in SciViews-R. The top one holds a graph
gallery, a convenient way for a beginner to explore several graphs R provides
and to use them with real data sets (note that the graph gallery is not provided
any more in SciViews-R 0.8-9). The lower one is the electronic equivalent of
Jonathan Baron’s R reference card. It can also be viewer as an explorer
dedicated to easily browse and find various R code constructs (“code chunks”).
86
Figure 5-10: The SciViews R Report application. A report is a HTML
document mixing formatted text and tables with graphs and pictures. SciViews-
R provides functions to append results easily to such a report, thanks to the
R2HTML package.
87
Figure 5-11: A view window in SciViews-R (it is the same view that is placed in
the report presented in the previous figure, but slightly reworked). The second
button of the toolbar automatically appends the current view to the report.
Behind, a series of tabs allows a fast and easy navigation between the different
views provided for this object.
TO DO…
Other assistants
TO DO…
88
Simplified online help and manuals
There are several introductory manuals for R. However, online
help presents all available functions. In R 2.2.0, despites a
reorganization of the main packages, there are still 1040 visible
functions in the package base! Among all these functions, many are very
unlikely to be used by the beginner. The beginner is lost in the mass of
the documentation and does not know where to find the functions to
perform simple tasks.
89
6. OVERVIEW OF THE SCIVIEWS R GUI API
90
7. SVGUI, FUNCTIONS TO INTERFACE GUI CLIENT APPLICATIONS
The svGUI package of the SciViews bundle is the one that provides
various interfaces to client GUIs. There are functions to communicate
between the GUI client and R (…), other functions to implement an
object explorer (…), …
91
8. SVIO AND SVVIEWS: FORMATTED DATA EXCHANGE
If you want to add a new type for a given class, you should use the
following skeleton:
# We define a new type called "dim"
"view.data.frame..dim" <- function(expr, objname, file, CSSFile,
command, browse = TRUE, ...) {
# To simplify the function, we consider it must be
# called from view.data.frame exclusively,
# We do not perform the full calculation of arguments like
# expr and assume it is an expression. This expression must
# be evaluated in the global environment to remain compatible
# with all GUI extensions
x <- eval(expr, envir = .GlobalEnv)
92
"view.myclass" <- function(x, type = "print", objname =
deparse(substitute(x)), file = guiViewsFile(), CSSFile =
guiViewsCSS(), command = "", browse = TRUE, ...) {
93
If this mechanism looks complex, it is indeed the most flexible way
we have found to :
1. Get the function work the same way wherever it is called (from the
command line, from a Tcl/Tk dialog box, from within another
function, etc..)
94
9. SVMISC: MISCELLANEOUS UTILITARY FUNCTIONS
95
10. SVDIALOGS: REUSABLE DIALOG BOXES
Given the need of standard dialog boxes for basic GUI features in
R, and the various possible toolkits, we propose here a framework for a
reusable GUI API for dialog boxes. This API is part of a larger one that
deals with many aspects of R GUIs (menus, toolbars, object explorers,
completion lists, …). It is intended to provide a common interface to
various GUIs. In this experimental svDialogs package, we focus
exclusively on dialog boxes (indeed, more generally, on “reusable
windows”).
96
Three levels of making dialog boxes in your GUI
Figure 10-1: The standard open file dialog box obtained with the script.
97
various commands in the tcltk package (adapted from an example
of the tcltk package):
Figure 10-2: The custom Tcl/Tk dialog box that we just constructed
with R.
Although this approach brings far more flexibility, it does not deal
easily with the graphical and aesthetic aspects of the dialog boxes.
The developer has to figure out how the dialog box will be painted
when he writes the code, and has to construct its dialog box by
trials and errors most of the time. It turns to be a painful and
irritating task for complex dialog boxes.
3. The third level for building dialog boxes, is to paint them thanks to
a dedicated program. The tcltk package does not provide such a
level yet (programs like Visual Tcl could probably be adapted to do
it). The experimental R-wxPython package does so, using a
separate application called Boa Constructor.
98
Figure 10-3 : A screenshot of Boa Constructor during the construction of a dialog box.
99
R-wxPython. We believe it has great potentials. In this svDialogs
package, we focus exclusively on level 1 dialog boxes. However, our
intention is to provide far more that the four or five standard dialog
boxes (message, input, open/save, choose dir) usually proposed. We
think that, with a carefully designed set of about twenty standard,
and/or flexible dialog boxes, one can build very easily a rather complex
GUI without the need of “programmed” dialog boxes (either at level 2, or
at level 3).
If guiWidgets is not defined (it is NULL), then the default Tcl/Tk version
of the dialog box is also used.
There are various ways to define the API and then to dispatch to
actual implementation of the dialog box. Here are a couple of
techniques:
100
2. To use the same mechanism, but with S4 objects. This is the way
we are supposed to program in R since version 1.7.0!
101
guiDlgMessage.wxPython <- function(message) {
if (!require(wxPython)) {
warning("Package 'wxPython' required. Please, (re)install
it!")
# Allow for using default dialog box by returning NULL
return(NULL)
}
102
in all toolkits (and possibly in all platforms). As an illustration of this
mechanism, I implement a guiSetStyle.tlctk() function, which
operates with Tcl/Tk dialog boxes, and defines four styles: “classic”,
“SciViews”, “Rcmdr” and “large”, as examples. Of course, new styles,
and/or customization of existing ones should be possible here. It is not
the case yet (I am still experimenting with styles, so, don’t ask too much
at once)! Here is an example (using guiDlgFunction(), a function that
automatically builds a dialog box to prompt for arguments of a function,
see further in this document):
Figure 10-4: an example Tcl/Tk dialog box, using the “classic” style.
Figure 10-5: the same Tcl/Tk dialog box, using the “Rcmdr” style.
103
look simpler also, with no 3D-borders but a little bit more space around
them than in the “Rcmdr” style… just a question of taste!
Figure 10-6: the same Tcl/Tk dialog box, using the “SciViews” style.
The “large” style is there to make dialog boxes more readable for
people with poor vision, or for on-screen presentations. It is basically a
“classic” style, but with enlarged fonts.
Figure 10-7: the same Tcl/Tk dialog box, using the “large” style.
As you notice, styles define some aspects of the dialog boxes like
fonts, fonts colours, widgets borders or spacing, but not all aspects. It is
not their purpose to redefine the whole appearance of the dialog boxes.
Features like the appearance of the title bar, the background colour, the
colour of 3D-frames and borders, etc… are defined globally on the
system by the windows manager and it is thus unnecessary to duplicate
these global options in svDialogs styles.
104
Internationalization of the dialog boxes
l("Cancel")
[1] "Cancel"
options(guiLang = "french")
l("Cancel")
[1] "Annuler"
This solution is simple because one only has to embed strings that
must be internationalized in the dialog box inside l()! Of course, it is
not optimal: all string definitions in English and in the foreign language
are loaded in memory in the respective .guiLang.XXX character vectors.
One can imagine doing the same thing using text files in l(). However,
would it be fast enough? If a better solution is proposed, I would happily
integrate it in svDialogs. For the moment, there is no
internationalization support in the current version of the dialog boxes.
105
Specification of standard dialog boxes
In order to make this collection of dialog boxes really useful, they
must cover a large panel of common tasks. However, to make them easy
to implement with several graphical toolkits, they must remain as simple
as possible (to cover common features of the different graphical
toolkits). There is thus a trade-off between features, and potentials to
implement them with the various widgets and on the different platforms
where R can run. Here we deal with standard dialog boxes, a few dialog
boxes designed for a very specific use, like displaying a message, asking
for a file to open or save, etc… In svDialogs, we propose also standard
windows, a few, possibly non modal windows for tasks like visualization
or edition of data sets, and flexible dialog boxes that can contain an
arbitrary number of widgets, for general purposes. These standard
windows and flexible dialog boxes will be described in the two following
sections.
3 One or several example screenshots of the dialog box (but since the
graphical toolkits have their own look and feel, it is not necessary
to conform strictly to these pictures).
Figure 10-8: A message box with an "ok" button and an "info" icon.
106
Figure 10-9: A message box with two buttons ("okcancel") and the
"question" icon.
Figure 10-10: A message box with two buttons ("yesno") and the "error"
icon.
Figure 10-11: A message box with three buttons ("yesnocancel") and the
"warning" icon. The second button is here the default one (default = 2
argument).
Prototype:
Arguments:
3 title is the caption of the dialog box (if a character vector longer
than one is provided, only the first item is used),
107
3 icon defines the icon to display in the dialog box. It can be "info",
"question", "error" or "warning". Suggested icons (standard ones
in the Windows platform) are depicted in the screenshots.
Return value:
Figure 10-13: The same input box in wxPython. This is a default dialog box
defined in wxWidgets and it does not provide a ‘Cancel’ button, but the user can
click on the close button (‘X’) at the top right of the dialog box to get the same
result.
108
Prototype:
Arguments:
3 title is the caption of the dialog box (if a character vector longer
than one is provided, only the first item is used),
3 default is a string with the default value to display in the text box
(if a character vector is provided, only the first item is used),
Return value:
This is the classical “open file” dialog box defined on any system. It
should be able to open several files at once (argument multi = TRUE),
and to define filters for opening only files with a given extension (for
instance, only R script files ending with a ‘.R’ extension).
109
Figure 10-14: An example of an open file dialog box, as it appears in Windows
XP.
Figure 10-15: The dialog box displayed when a non-existing file is provided by
the user in the open file dialog box. Then the open dialog box is displayed again
for correction of the file name.
Prototype:
Arguments:
3 defaultFile is a string with the default name of the file to open (if
a character vector is provided, only the first item is used),
3 defaultDir isa string with the default directory in which the dialog
box is opened (if a character vector is provided, only the first item
is used),
110
3 multi indicates if the user can open several files at once (multi =
TRUE), or not (multi = FALSE, by default). The wxPython version of
this dialog box does not support multi = TRUE yet.
Return value:
This dialog box is similar to the open file(s) one, but its purpose is
different. It is intended to provide a name of a file where data should be
written. Arguments are also a little bit different.
111
Figure 10-16: An example of a save file dialog box, as it appears in Windows
XP.
Figure 10-17: The dialog box displayed when an existing file name is provided
by the user in the save file dialog box. It prompts for confirmation to overwrite
this file. If the user clicks "no", then the save dialog box is displayed again for
correction of the file name.
Prototype:
Arguments:
3 defaultFile is a string with the default name of the file to save (if a
character vector is provided, only the first item is used),
3 defaultDir isa string with the default directory in which the dialog
box is opened (if a character vector is provided, only the first item
is used),
112
3 defaultExt is a string with the default extension of the file to save
(if a character vector is provided, only the first item is used). If the
user entered a name without extension, that extension is
automatically appended to the file name,
Return value:
A string containing the path of the file. If this file exists, the
function already took care to ask confirmation for overwriting it (see
picture). It returns character(0) if the user clicked "cancel" or the
closing button (X). It returns NULL if the dialog box could not be
displayed (for instance, neither the specific package, nor the default one
–tcltk– is installed).
113
If the user specifies a non-existing directory and the argument new
= FALSE, then the function should not accept its entry and should display
a dialog box like this (from tcltk):
Figure 10-19: The dialog box displayed when a non-existing directory name is
provided by the user and the argument new = FALSE. When the user clicks
"ok", it is returned to the choose directory dialog box to change its selection.
Prototype:
Arguments:
3 dir is a string with the default name of the directory (if a character
vector is provided, only the first item is used). By default, it is the
current working directory,
Return value:
This dialog box displays a list and allows selection (either simple,
or multiple) in it. It also offers "ok" and "cancel" buttons.
114
Prototype:
TO DO…
Prototype:
TO DO…
Prototype:
TO DO…
Prototype:
115
parent = 0, GUI = getOption("guiWidgets"))
TO DO…
Prototype:
TO DO…
Prototype:
TO DO…
Prototype:
TO DO…
116
The text window
Prototype:
TO DO…
Prototype:
TO DO…
117
Figure 10-20: an example of a view in SciViews-R. Here the summary of a
linear regression is presented in a nicer way than the usual presentation in the
R console. The R2HTML is used here to make most of the job. The CSS file used
is “R2HTML”, but it is possible to change the look of this view very easily using
other styles.
Prototype:
TO DO…
118
Figure 10-21: an example of an explorer window. Here, it is a version of the R
Commander menu displayed in a dock window of SciViews-R. All items of a
related topic are collected together in tabs. Here the ‘Statistics’ tab is opened,
and a part of its content is shown. Moving the mouse on top of an item displays
a tooltip with further information about it. Clicking on it opens the
corresponding dialog box, just like the corresponding menu item of R
Commander does. Using this explorer appears to be faster in practice, because
items are more easily accessible than when they are embedded is several levels
of submenus… However, choice between a classical menus or this explorer is
more a matter of taste, and should be left to the end user! This window is
implemented in HTML and javascript only.
119
Figure 10-23: a third example of an explorer window in SciViews-R. This is
the electronic version of Jonathan Baron’s R reference card available on CRAN.
The presentation is similar than the R commander menu, but the idea is
different. Here we collect together items that show to the occasional user, or to
the beginner, how to do usual tasks in R (when you click on an item, the
corresponding syntax is pasted to the console, or to your script).
Prototype:
TO DO…
120
Figure 10-24: the object explorer in SciViews-R. It is a (possibly simplified)
version of this object explorer that we want to implement here.
Prototype:
TO DO…
Prototype:
TO DO…
121
where everything inside square brackets is optional. Basically, one call a
function myfun(), possibly giving values for one or more of its arguments
[arg1], [arg2], … either in the form argvalue, or in the form myarg =
argvalue. Finally, the result of the function can possibly be assigned to a
variable (myvar <- ). Thus, the dialog box should reflect, and suggest
such a construction to be a didactic tool that teaches the user how to
program in R!
> data(sleep)
> t.test(extra ~ group, data = sleep)
Figure 10-25: the t-test dialog box for independent samples in R Commander.
This way, the user sees it and can learn how to write code for a t-test by
the example. This is, unfortunately, not the case with all GUIs. Some try
to hide completely “this horrible code that we don’t want to see
122
anymore”. But then, how will the beginner learn the R language if he
only uses dialog boxes and never sees a single line of R code?
Now, is it possible to write a dialog box for the t-test that will learn
even better how to write an instruction in the language than the one
provided by R Commander? May be… look at the next one.
Figure 10-26: the default flexible dialog box automatically built for the
chisq-test function.
Which argument should have an area in the dialog box, and which
one should not,
123
The idea of this dialog box is to have a (semi)-automatic
construction of an appropriate dialog box to prompt for function
arguments. This can be used to build buttons or menu entries by drag
and drop from the object explorer to a toolbar, or to a menu (see
http://www.sciviews.org/SciViews-R). There is nothing new here, since
S-PLUS does this since a long time… but it is a nice feature I would like
to get in R too! Obviously, this is a piece of work, anyway…
Prototype:
TO DO…
A dialog box with a list or tree at left, and a textbox, list, checkbox
or option boxes at right. It allows changing various options from within
the same dialog box. Options should be specified in a list (like
options()). A second list indicates the appearance and various choices
for each particular option.
Prototype:
TO DO…
Prototype:
TO DO…
Prototype:
TO DO…
124
11. SVIDE AND SVSOCKET: COMMUNICATION BETWEEN SCRIPT
EDITORS/IDE AND R
125
12. TCLTK2: ADDITIONAL TCL/TK FEATURES FOR R
126
REFERENCES
Grosjean, Ph. & F. Ibanez, 2004. PASTECS: a package for analysis of space-
time ecological series. [online: http://www.sciviews.org/pastecs]
127