Skip to content

Instantly share code, notes, and snippets.

@kenwebb
Last active December 30, 2018 12:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kenwebb/934af431ea402100efa07206751e8dbb to your computer and use it in GitHub Desktop.
Save kenwebb/934af431ea402100efa07206751e8dbb to your computer and use it in GitHub Desktop.
R - OpenCPU
<?xml version="1.0" encoding="UTF-8"?>
<!--Xholon Workbook http://www.primordion.com/Xholon/gwt/ MIT License, Copyright (C) Ken Webb, Sun Dec 30 2018 07:50:41 GMT-0500 (Eastern Standard Time)-->
<XholonWorkbook>
<Notes><![CDATA[
Xholon
------
Title: R - OpenCPU
Description:
Url: http://www.primordion.com/Xholon/gwt/
InternalName: 934af431ea402100efa07206751e8dbb
Keywords:
My Notes
--------
December 28, 2018
Use XholonOpenCpuR.html, for example:
http://127.0.0.1:8888/XholonOpenCpuR.html?app=HelloWorld&gui=clsc
http://127.0.0.1:8888/XholonOpenCpuR.html?app=R%20-%20OpenCPU&src=lstr&gui=clsc
Chrome Dev Apps
---------------
ocpu.seturl("http://localhost:5656/ocpu/library/stats/R"); // this works
result:
------
Setting path to CORS server http://localhost:5656/ocpu/library/stats/R
undefined
opencpu-0.5.js:402 Path updated. Available objects/functions:
acf
acf2AR
add1
...
test (I did this after the above ocpu.seturl(...) )
----
var mydata = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
//call R function: stats::sd(x=data)
var req = ocpu.rpc("sd",{
x : mydata
}, function(output){
alert("Standard Deviation equals: " + output);
});
//optional
req.fail(function(){
alert("R returned an error: " + req.responseText);
});
result (alert):
------
Standard Deviation equals: 4.4721
TODO
----
(1) OK run just R rather than RStudio (the following works, from a Ubuntu terminal):
R
library(opencpu)
ocpu_start_app("rwebapps/appdemo")
then:
http://127.0.0.1:8888/XholonOpenCpuR.html?app=R%20-%20OpenCPU&src=lstr&gui=clsc
http://localhost:5656/ocpu/apps/rwebapps/appdemo/www/plot.html
http://localhost:5656/ocpu/apps/rwebapps/appdemo/www/hello.html
http://localhost:5656/ocpu/tmp/x065cad66e63d78/graphics/last/svg?width=11.69&height=8.27
(2) write an opencpu app
(3) OK add jquery and opencpu JS libraries to Xholon public/lib
(4) write R/RStudio functions
References
----------
(1) https://www.opencpu.org
OpenCPU is a system for embedded scientific computing and reproducible research.
The OpenCPU server provides a reliable and interoperable HTTP API for data analysis based on R.
You can either use the public servers or host your own.
Seamless R and JavaScript Integration
The OpenCPU JavaScript client library provides the most seamless integration of R and JavaScript available today.
Enjoy simple RPC and data I/O through standard Ajax techniques.
Check for yourself by trying the jsfiddle examples.
(2) https://www.opencpu.org/download.html
#Local Single-User Server (do this in R)
# Install OpenCPU
install.packages("opencpu")
# Run Apps directly from Github
library(opencpu)
ocpu_start_app("rwebapps/nabel")
ocpu_start_app("rwebapps/markdownapp")
ocpu_start_app("rwebapps/stockapp")
ocpu_start_app("rwebapps/appdemo")
(3) https://www.opencpu.org/jslib.html
(4) https://github.com/rwebapps/appdemo
(5) http://adv-r.had.co.nz/
Advanced R by Hadley Wickham
(6) http://r-pkgs.had.co.nz/
R packages by Hadley Wickham
(7) http://r4ds.had.co.nz/
R for Data Science: Import, Tidy, Transform, Visualize, and Model Data, by Hadley Wickham and Garrett Grolemund
(8) http://had.co.nz/ggplot2/
(9) https://github.com/hadley/
(10) https://support.rstudio.com/hc/en-us/articles/115010915687
Using RStudio Connections
) https://db.rstudio.com/
) https://db.rstudio.com/rstudio/connections/
) https://db.rstudio.com/best-practices/drivers/
Setting up ODBC Drivers
includes info on "Linux Debian / Ubuntu"
sudo apt-get update
sudo apt-get install unixodbc unixodbc-dev --install-suggests
sudo apt-get install odbc-postgresql
and then read rest of instructions
edit 2 text files that are in /etc
]]></Notes>
<_-.XholonClass>
<PhysicalSystem/>
<OpenCpuTest01/>
<OpenCpuTest02/>
</_-.XholonClass>
<xholonClassDetails>
</xholonClassDetails>
<PhysicalSystem>
<OpenCpuTest01/>
<OpenCpuTest02/>
</PhysicalSystem>
<OpenCpuTest01behavior implName="org.primordion.xholon.base.Behavior_gwtjs"><![CDATA[
var me, beh = {
postConfigure: function() {
me = this.cnode.parent();
$wnd.ocpu.seturl("http://localhost:5656/ocpu/library/stats/R");
},
act: function() {
var mydata = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
//call R function: stats::sd(x=data)
var req = $wnd.ocpu.rpc("sd",{
x : mydata
}, function(output){
me.println("Standard Deviation equals: " + output); // result: Standard Deviation equals: 4.4721
});
//optional
req.fail(function(){
me.println("R returned an error: " + req.responseText);
});
}
// result in RStudio R console:
//[2018-12-28 10:07:34] OPTIONS /ocpu/library/stats/R/sd
//[2018-12-28 10:07:34] POST /ocpu/library/stats/R/sd
//[2018-12-28 10:07:34] GET /ocpu/tmp/x0c010c1652adb6/R/.val/json
}
//# sourceURL=OpenCpuTest01behavior.js
]]></OpenCpuTest01behavior>
<OpenCpuTest02behavior implName="org.primordion.xholon.base.Behavior_gwtjs"><![CDATA[
var me, beh = {
postConfigure: function() {
me = this.cnode.parent();
//$wnd.ocpu.seturl("http://localhost:5656/ocpu/library/stats/R");
},
act: function() {
var req = $wnd.ocpu.call("rnorm", {n: 100}, function(session){
me.println(session.getKey());
me.println(session.getLoc());
//retrieve session console (stdout) async
session.getConsole(function(outtxt) {
$wnd.console.log(session);
me.println(outtxt);
});
//retrieve the returned object async; the following code does nothing ?
session.getObject(function(data) {
$wnd.console.log(data);
//data is the object returned by the R function
//alert("Array of length " + data.length + ".\nFirst few values:" + data.slice(0,3));
me.println("Array of length " + data.length + ".\nFirst few values:" + data.slice(0,3));
});
}).fail(function(){
me.println("OpenCpuTest02behavior R returned an error: " + req.responseText);
});
}
}
//# sourceURL=OpenCpuTest02behavior.js
]]></OpenCpuTest02behavior>
<SvgClient><Attribute_String roleName="svgUri"><![CDATA[data:image/svg+xml,
<svg width="100" height="50" xmlns="http://www.w3.org/2000/svg">
<g>
<title>OpenCpuTest01</title>
<rect id="PhysicalSystem/OpenCpuTest01" fill="#98FB98" height="50" width="50" x="25" y="0"/>
<g>
<title>OpenCpuTest01behavior</title>
<rect id="PhysicalSystem/OpenCpuTest01/OpenCpuTest01behavior" fill="#6AB06A" height="50" width="10" x="80" y="0"/>
</g>
</g>
</svg>
]]></Attribute_String><Attribute_String roleName="setup">${MODELNAME_DEFAULT},${SVGURI_DEFAULT}</Attribute_String></SvgClient>
</XholonWorkbook>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment