Skip to content

Commit

Permalink
changed to opencpu js library
Browse files Browse the repository at this point in the history
  • Loading branch information
jeroen committed Jul 29, 2013
1 parent a1464f7 commit b070f9c
Show file tree
Hide file tree
Showing 7 changed files with 227 additions and 41 deletions.
3 changes: 3 additions & 0 deletions .gitignore
@@ -0,0 +1,3 @@
.Rproj.user
.Rhistory
.RData
6 changes: 3 additions & 3 deletions inst/www/index.html
Expand Up @@ -15,9 +15,9 @@
<script type="text/javascript" src="ext-4.2.1/ext-theme-neptune.js"></script>

<!-- OCPU lib and depends -->
<script type="text/javascript" src="ocpu/jquery-1.10.1.min.js"></script>
<script type="text/javascript" src="ocpu/resize.js"></script>
<script type="text/javascript" src="ocpu/ocpu.js"></script>
<script type="text/javascript" src="opencpu/jquery-1.10.1.min.js"></script>
<script type="text/javascript" src="opencpu/resize.js"></script>
<script type="text/javascript" src="opencpu/opencpu.js"></script>

<!-- Stocks application -->
<script type="text/javascript" src="stocks.js"></script>
Expand Down
File renamed without changes.
168 changes: 168 additions & 0 deletions inst/www/opencpu/opencpu.js
@@ -0,0 +1,168 @@
/**
* Javascript client library for OpenCPU
* Version 0.1
* Depends: jQuery
* http://github.com/jeroenooms/opencpu-js
*
* Include this file in your apps (packages).
* Edit r_path variable below if needed.
*/

(function ( $ ) {
//static vars
var r_path = "../R";

//internal functions
function r_fun_call(fun, args, handler){
if(!fun) throw "r_fun_call called without fun";
var args = args || {};

var jqxhr = $.ajax({
type: "POST",
url: r_path + "/" + fun,
data: JSON.stringify(args),
contentType : 'application/json',
dataType: "text",
success : function(){
var Location = jqxhr.getResponseHeader('Location');
if(!Location){
alert("Response was successful but had no location??")
} else {
handler(Location);
}
},
error : function(){
var status = jqxhr.status;
switch(status) {
case 400:
alert("Call to R resulted in an error:\n\n" + jqxhr.responseText);
break;
case 503:
alert("Problem with OpenCPU server:\n\n" + jqxhr.responseText);
break;
default:
alert("HTTP error: " + status + "\n\n" + jqxhr.responseText);
}
}
});
return jqxhr;
}

function r_session_list(location, handler){
if(!location) throw "r_session_list called without location";
return $.ajax({
type: "GET",
url: location,
dataType: "text",
success : function(data, textStatus, jqXHR){
handler(data);
}
});
}

//plotting widget
//to be called on an (empty) div.
$.fn.r_fun_plot = function(fun, args) {
var targetdiv = this;
if(!targetdiv.data("div")){
targetdiv.data("div", $('<div />').attr({
class: "r_fun_plot",
style: "width: 100%; height:100%; min-width: 100px; min-height: 100px; position:absolute; background-repeat:no-repeat; background-size: 100% 100%;"
}).appendTo(targetdiv));
}

var div = targetdiv.data("div");

if(!div.data("spinner")){
div.data("spinner", $('<span />').attr({
style : "position: absolute; top: 20px; left: 20px; z-index:1000; font-family: monospace;"
}).text("loading...").appendTo(div));
}

if(!div.data("pdflink")){
div.data("pdflink", $('<a />').attr({
target: "_blank",
style: "position: absolute; top: 10px; right: 10px; z-index:1000; text-decoration:underline; font-family: monospace;"
}).text("pdf").appendTo(div));
}

if(!div.data("svglink")){
div.data("svglink", $('<a />').attr({
target: "_blank",
style: "position: absolute; top: 30px; right: 10px; z-index:1000; text-decoration:underline; font-family: monospace;"
}).text("svg").appendTo(div));
}

if(!div.data("pnglink")){
div.data("pnglink", $('<a />').attr({
target: "_blank",
style: "position: absolute; top: 50px; right: 10px; z-index:1000; text-decoration:underline; font-family: monospace;"
}).text("png").appendTo(div));
}

div.css("background-image", "none");
var pdf = div.data("pdflink").hide();
var svg = div.data("svglink").hide();
var png = div.data("pnglink").hide();
var spinner = div.data("spinner").show();


// call the function
return r_fun_call(fun, args, function(Location) {
div.data("plotlocation", Location);
pdf.attr("href", Location + "graphics/last/pdf?width=11.69&height=8.27&paper=a4r");
svg.attr("href", Location + "graphics/last/svg?width=11.69&height=8.27");
png.attr("href", Location + "graphics/last/png?width=800&height=600");

// function to update the png image
var updatepng = debounce(function(e) {
var loc = div.data("plotlocation");
var oldurl = div.css("background-image");
var newurl = "url(" + loc + "graphics/last/png?width=" + div.width() + "&height=" + div.height() + ")";

// use the tail of the url only to prevent absolute url problems
if (div.is(":visible") && oldurl.slice(-50) != newurl.slice(-50)) {
div.css("background-image", newurl);
}
spinner.hide();
pdf.show();
svg.show();
png.show();
}, 500);

// register update handlers
div.on("resize", updatepng);
$(window).on("resize", updatepng);

// show
updatepng();
});
}


// from understore.js
function debounce(func, wait, immediate) {
var result;
var timeout = null;
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
if (!immediate)
result = func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow)
result = func.apply(context, args);
return result;
}
}

// exported functions
window.opencpu = {
r_fun_call : r_fun_call
}

}( jQuery ));
File renamed without changes.
75 changes: 37 additions & 38 deletions inst/www/stocks.js
Expand Up @@ -6,48 +6,48 @@ Ext.onReady(function() {

var today = new Date();

var treePanel = new Ext.tree.TreePanel({
id: 'tree-panel',
var treePanel = new Ext.tree.TreePanel({
id: 'tree-panel',
iconCls: 'chartIcon',
title: 'by Index',
region: 'center',
title: "stocks",
height: 300,
border: false,
autoScroll: true,
title: 'by Index',
region: 'center',
title: "stocks",
height: 300,
border: false,
autoScroll: true,
lazyRender:true,
animate: true,
containerScroll: true,
animate: true,
containerScroll: true,
enableDrag: true,
dragConfig: {ddGroup: 'DragDrop' },
autoWidth: true,

// tree-specific configs:
rootVisible: false,
lines: false,
singleExpand: true,
useArrows: true,
// tree-specific configs:
rootVisible: false,
lines: false,
singleExpand: true,
useArrows: true,
store: {
root: {
expanded: true
}
root: {
expanded: true
}
},
listeners: {
itemdblclick: function(s, r){
if(r.data.leaf){
addWorkspace(r.data.id.substring(7));
}
},
listeners: {
itemdblclick: function(s, r){
if(r.data.leaf){
addWorkspace(r.data.id.substring(7));
}
},
itemclick : function(s, r){
if(r.data.leaf){
var name = r.data.text.split(" - ");
var stock = name[0]
var company = name[1];
Ext.getCmp("details-panel").update('<div class="detaildiv"> <h3>' + company + '</h3> Yahoo Finance: <a target="_blank" href="http://finance.yahoo.com/q?s=' + stock + '">'+stock+'</a></div>');
}
}
}
});
itemclick : function(s, r){
if(r.data.leaf){
var name = r.data.text.split(" - ");
var stock = name[0]
var company = name[1];
Ext.getCmp("details-panel").update('<div class="detaildiv"> <h3>' + company + '</h3> Yahoo Finance: <a target="_blank" href="http://finance.yahoo.com/q?s=' + stock + '">'+stock+'</a></div>');
}
}
}
});

var myToolbar = Ext.create('Ext.toolbar.Toolbar', {
"items" :['->',{
Expand Down Expand Up @@ -243,7 +243,7 @@ Ext.onReady(function() {
}

function loadtree(){
ocpu.r_fun_call("listbyindustry", {}, function(location){
opencpu.r_fun_call("listbyindustry", {}, function(location){
Ext.getCmp("tree-panel").getStore().setProxy({
type: "ajax",
url: location + "R/.val/json",
Expand All @@ -252,9 +252,8 @@ Ext.onReady(function() {
Ext.getCmp("tree-panel").getStore().load();
});
}


//init

//init
loadtree();

});
16 changes: 16 additions & 0 deletions stocks.Rproj
@@ -0,0 +1,16 @@
Version: 1.0

RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8

RnwWeave: Sweave
LaTeX: pdfLaTeX

BuildType: Package
PackageInstallArgs: --no-multiarch --with-keep.source

0 comments on commit b070f9c

Please sign in to comment.