From b070f9c70795519aeac96b5d722c6f5a45364069 Mon Sep 17 00:00:00 2001 From: Jeroen Date: Mon, 29 Jul 2013 22:51:07 +0200 Subject: [PATCH] changed to opencpu js library --- .gitignore | 3 + inst/www/index.html | 6 +- .../{ocpu => opencpu}/jquery-1.10.1.min.js | 0 inst/www/opencpu/opencpu.js | 168 ++++++++++++++++++ inst/www/{ocpu => opencpu}/resize.js | 0 inst/www/stocks.js | 75 ++++---- stocks.Rproj | 16 ++ 7 files changed, 227 insertions(+), 41 deletions(-) create mode 100644 .gitignore rename inst/www/{ocpu => opencpu}/jquery-1.10.1.min.js (100%) create mode 100644 inst/www/opencpu/opencpu.js rename inst/www/{ocpu => opencpu}/resize.js (100%) create mode 100644 stocks.Rproj diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..807ea25 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.Rproj.user +.Rhistory +.RData diff --git a/inst/www/index.html b/inst/www/index.html index 1c42a2a..c28c8ab 100644 --- a/inst/www/index.html +++ b/inst/www/index.html @@ -15,9 +15,9 @@ - - - + + + diff --git a/inst/www/ocpu/jquery-1.10.1.min.js b/inst/www/opencpu/jquery-1.10.1.min.js similarity index 100% rename from inst/www/ocpu/jquery-1.10.1.min.js rename to inst/www/opencpu/jquery-1.10.1.min.js diff --git a/inst/www/opencpu/opencpu.js b/inst/www/opencpu/opencpu.js new file mode 100644 index 0000000..e3d7ae3 --- /dev/null +++ b/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", $('
').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", $('').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", $('').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", $('').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", $('').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 )); diff --git a/inst/www/ocpu/resize.js b/inst/www/opencpu/resize.js similarity index 100% rename from inst/www/ocpu/resize.js rename to inst/www/opencpu/resize.js diff --git a/inst/www/stocks.js b/inst/www/stocks.js index b46f0f9..9d42f4e 100644 --- a/inst/www/stocks.js +++ b/inst/www/stocks.js @@ -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(''); - } - } - } - }); + 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('

' + company + '

Yahoo Finance: '+stock+'
'); + } + } + } + }); var myToolbar = Ext.create('Ext.toolbar.Toolbar', { "items" :['->',{ @@ -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", @@ -252,9 +252,8 @@ Ext.onReady(function() { Ext.getCmp("tree-panel").getStore().load(); }); } - - - //init + + //init loadtree(); }); \ No newline at end of file diff --git a/stocks.Rproj b/stocks.Rproj new file mode 100644 index 0000000..3a76475 --- /dev/null +++ b/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