Skip to content

Commit 8b47895

Browse files
committed
add example
1 parent 6348bd4 commit 8b47895

File tree

5 files changed

+124
-7
lines changed

5 files changed

+124
-7
lines changed

example/hello-processing.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
processing$line(11, 22, 33, 22)
2+
print("Hello, Processing.R")

example/test-settings.R

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
yPos <- 0.0;
2+
3+
settings <- function() {
4+
print("Set width and height.")
5+
processing$size(500, 500)
6+
}
7+
8+
draw <- function() {
9+
processing$background(as.integer(204))
10+
yPos <- yPos - 1.0;
11+
if (yPos < 0) {
12+
yPos <- height;
13+
}
14+
processing$line(0, yPos, width, yPos)
15+
}

example/test-setup.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
setup <- function {
2+
processing$line(11, 22, 33, 22)
3+
print("Hello, Processing.R")
4+
}

readme.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,15 @@ processing$line(posAX, posAY, posBX, posBY)
2121
The output is:
2222

2323
<img src="./docs/img/demo.png" height="200">
24+
25+
## License
26+
27+
MIT License
28+
29+
Copyright (c) 2016 Ce Gao
30+
31+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
32+
33+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
34+
35+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

src/rprocessing/RLangPApplet.java

Lines changed: 92 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33
import javax.script.ScriptEngine;
44
import javax.script.ScriptException;
55

6+
import org.renjin.parser.RParser;
67
import org.renjin.script.RenjinScriptEngine;
78
import org.renjin.sexp.Closure;
9+
import org.renjin.sexp.ExpressionVector;
10+
import org.renjin.sexp.FunctionCall;
11+
import org.renjin.sexp.SEXP;
812

913
import processing.core.PApplet;
1014

@@ -25,8 +29,15 @@ private enum Mode {
2529
STATIC, ACTIVE, MIXED
2630
}
2731

32+
// A static-mode sketch must be interpreted from within the setup() method.
33+
// All others are interpreted during construction in order to harvest method
34+
// definitions, which we then invoke during the run loop.
35+
private final Mode mode;
36+
37+
/** The name of processing's PApplet in R top context. */
2838
private static final String PROCESSING_VAR_NAME = "processing";
2939

40+
/** Program Code */
3041
private final String programText;
3142

3243
/** Engine to interpret R code */
@@ -35,32 +46,99 @@ private enum Mode {
3546
public RLangPApplet(final ScriptEngine renjinEngine, final String programText) {
3647
this.renjinEngine = (RenjinScriptEngine) renjinEngine;
3748
this.programText = programText;
49+
this.mode = this.detectMode();
50+
this.prePassCode();
51+
}
52+
53+
/**
54+
* Evaluate all the function calls.
55+
*/
56+
public void prePassCode() {
57+
SEXP source = RParser.parseSource(this.programText + "\n", "inline-string");
58+
if (source.getClass().equals(ExpressionVector.class)) {
59+
ExpressionVector ev = (ExpressionVector) source;
60+
for (int i = 0; i < ev.length(); ++i) {
61+
if (ev.get(i).getClass().equals(FunctionCall.class)) {
62+
this.renjinEngine.getTopLevelContext().evaluate(ev.get(i),
63+
this.renjinEngine.getTopLevelContext().getEnvironment());
64+
}
65+
}
66+
}
67+
}
68+
69+
/**
70+
* TODO: Detect the mode.
71+
*/
72+
private Mode detectMode() {
73+
return Mode.STATIC;
3874
}
3975

76+
/**
77+
* Add PApplet instance to R top context
78+
* Notice: DO NOT do it in constructor.
79+
*/
4080
public void AddPAppletToRContext() {
4181
this.renjinEngine.put(PROCESSING_VAR_NAME, this);
4282
}
4383

84+
/**
85+
* TODO: Evaluate settings before the main program.
86+
* @see processing.core.PApplet#settings()
87+
*/
88+
@Override
89+
public void settings() {
90+
Object obj = this.renjinEngine.get("settings");
91+
if (obj.getClass() == Closure.class) {
92+
((Closure) obj).doApply(this.renjinEngine.getTopLevelContext());
93+
} else if (mode == Mode.STATIC) {
94+
// TODO: Implement Static Mode.
95+
}
96+
}
97+
98+
/**
99+
* Evaluate the program code.
100+
* @see processing.core.PApplet#setup()
101+
*/
44102
@Override
45103
public void setup() {
46-
try {
47-
wrapProcessingVariables();
48-
this.renjinEngine.eval(this.programText);
49-
} catch (ScriptException e) {
50-
System.out.println(e);
104+
if (this.mode == Mode.STATIC) {
105+
try {
106+
wrapProcessingVariables();
107+
this.renjinEngine.eval(this.programText);
108+
// System.out.println(this.renjinEngine.getTopLevelContext().getEnvironment()
109+
// .getVariable("settings"));
110+
} catch (ScriptException e) {
111+
System.out.println(e);
112+
}
113+
} else if (this.mode == Mode.ACTIVE) {
114+
Object obj = this.renjinEngine.get("setup");
115+
if (obj.getClass() == Closure.class) {
116+
((Closure) obj).doApply(this.renjinEngine.getTopLevelContext());
117+
}
118+
} else {
119+
// TODO: implement MIX Mode.
51120
}
52121
}
53122

123+
/**
124+
* Call the draw function in R script.
125+
* @see processing.core.PApplet#draw()
126+
*/
54127
@Override
55128
public void draw() {
56-
Closure c = (Closure) this.renjinEngine.get("draw");
57-
c.doApply(this.renjinEngine.getTopLevelContext());
129+
Object obj = this.renjinEngine.get("draw");
130+
if (obj.getClass() == Closure.class) {
131+
((Closure) obj).doApply(this.renjinEngine.getTopLevelContext());
132+
}
58133
}
59134

60135
/*
61136
* Helper functions
62137
*/
63138

139+
/**
140+
* Set Environment variables in R top context.
141+
*/
64142
protected void wrapProcessingVariables() {
65143
this.renjinEngine.put("width", width);
66144
this.renjinEngine.put("height", height);
@@ -74,8 +152,15 @@ protected void wrapProcessingVariables() {
74152

75153
/*
76154
* Wrapper functions
155+
*
156+
* float is not implemented in R, so processing.r need to cast double to
157+
* float.
77158
*/
78159

160+
public void size(double width, double height) {
161+
super.size((int) width, (int) height);
162+
}
163+
79164
public void point(double x, double y) {
80165
super.point((float) x, (float) y);
81166
}

0 commit comments

Comments
 (0)