summaryrefslogtreecommitdiff
path: root/tex/context/modules/mkii/m-r.mkii
blob: c2cb7ba8816204c6791ab6b926bc47c5d303f010 (plain)
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
%D \module
%D   [       file=m-r,
%D        version=2006.06.06,
%D          title=\CONTEXT\ Modules,
%D       subtitle=R Support,
%D         author={Johan Sandblom \& Hans Hagen},
%D           date=\currentdate,
%D      copyright={Johan Sandblom \& Hans Hagen}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

\unprotect

%D The following R-processor is a variation on Johan Sandblom's
%D prototype.
%D
%D We can combine both variants in one macro definition. Also, we
%D can minimize the number of runs by checking for a change.

%D JS: The call to R has \type {-q} in order to prevent banner,
%D \type {--save} to make sure it saves the workspace after the run,
%D \type {--restore} to make sure it reads any workspace from a
%D previous session.

%D An easier and better solution is to use the buffering mechanisms:

\def\Rbufferprefix{r-}

\newcounter\nofRfiles

\def\Rfile{\TEXbufferfile{\Rbufferprefix\nofRfiles}}

\def\startR
  {\doglobal\increment\nofRfiles
   \dostartbuffer[\Rbufferprefix\nofRfiles][startR][stopR]}

\def\stopR
  {\doifmode{*\v!first}\runR
   \typefile{\Rfile.out}}

\def\startRhidden
  {\doglobal\increment\nofRfiles
   \dostartbuffer[\Rbufferprefix\nofRfiles][startRhidden][stopRhidden]}

\def\stopRhidden
  {\doifmode{*\v!first}\runR}

\def\runR
  {\executesystemcommand{texmfstart
      --ifchanged=\Rfile\space --direct R
      CMD BATCH -q --save --restore \Rfile\space \Rfile.out}}

\protect \doifnotmode{demo}{\endinput}

% Johan's test file:

\usemodule[r]

\def\R{R}

\setupcolors[state=start]

\setuptyping
 [Rtype]
 [color=darkgreen]

\starttext

First a test of whether the workspace is persistent:
bla

\startR
a <- "bla"
b <- "blabla"
ls()
\stopR

One \R run ends, another begins.

\startR
ls()
\stopR

Now follows a hidden \R run which cleans the R workspace

\startRhidden
rm(list=ls())
save.image()
\stopRhidden

What is in the workspace now?

\startR
ls()
\stopR

Then a small test of generating a graphic, in this case a pdf
\startR
ushape <- c(rexp(500000), 12-rexp(500000))
pdf("ushape.pdf")
par(mfrow=c(1,2))
hist(ushape)
plot(density(ushape), main="Density")
dev.off()
\stopR

The graphic \type{ushape.pdf} can be included in the standard \CONTEXT\ way
\startbuffer
\placefigure{An ugly distribution}{\externalfigure[ushape]}
\stopbuffer
\typebuffer
\getbuffer

\startR
x <- rnorm(900)
y <- rexp(900)
# test comment
f <- gl(9,9,900)
summary(aov(y~x+Error(f)))
library(lattice)
pdf("lattice.pdf")
xyplot(y~x|f)
dev.off()
\stopR

With \type{Sweave} lattice graphics calls must be enclosed in
\type{print()} statements but that is not necessary here.

\startbuffer
\placefigure[here]{Lattice graphics}{\externalfigure[lattice]}
\stopbuffer
\typebuffer
\getbuffer

A test string with nasty characters. In \R, the result of a statement
is not printed by default. Enclosing the statement in parentheses,
however causes the parser to see only the value of the statement and
applying the \type{print()} method.
\startR
(test <- ".*\\\\ [[{[{]{[{[{}\]\}=?!+%#|<|>@$")
cat(test)
\stopR

A combination
\startbuffer
\placefigure{A combination of two previously used graphics}{
\startcombination[2*1]
 {\externalfigure[ushape][width=.4\textwidth]}{The first graphic, rescaled}
 {\externalfigure[lattice][width=.4\textwidth]}{The second graphic, rescaled}}
\stopcombination
\stopbuffer
\typebuffer
\getbuffer

Testing a function definition.

\startR
a.df <- data.frame(a=1:2, b=rnorm(2))
a.df$a
testfunction <- function(a=NULL, ...) {
 for(i in 1:length(a)) {
   gsub(a[[i]], "([a-r]|[A-R])", "bla")}
 print(a)}
\stopR

What is in the workspace now?

\startR
ls()
\stopR

\stoptext