annotate cpack/dml/components/r_fig.pl @ 0:718306e29690 tip

commiting public release
author Daniel Wolff
date Tue, 09 Feb 2016 21:05:06 +0100
parents
children
rev   line source
Daniel@0 1 /* Part of DML (Digital Music Laboratory)
Daniel@0 2 Copyright 2014-2015 Samer Abdallah, University of London
Daniel@0 3
Daniel@0 4 This program is free software; you can redistribute it and/or
Daniel@0 5 modify it under the terms of the GNU General Public License
Daniel@0 6 as published by the Free Software Foundation; either version 2
Daniel@0 7 of the License, or (at your option) any later version.
Daniel@0 8
Daniel@0 9 This program is distributed in the hope that it will be useful,
Daniel@0 10 but WITHOUT ANY WARRANTY; without even the implied warranty of
Daniel@0 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Daniel@0 12 GNU General Public License for more details.
Daniel@0 13
Daniel@0 14 You should have received a copy of the GNU General Public
Daniel@0 15 License along with this library; if not, write to the Free Software
Daniel@0 16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Daniel@0 17 */
Daniel@0 18
Daniel@0 19 :- module(cp_r_fig,
Daniel@0 20 [ figure//4
Daniel@0 21 , figure//3
Daniel@0 22 , term_rendering//3
Daniel@0 23 ]).
Daniel@0 24
Daniel@0 25 :- meta_predicate figure(0,+,+,+,?,?).
Daniel@0 26 :- meta_predicate figure(0,+,+,?,?).
Daniel@0 27
Daniel@0 28 :- use_module(library(http/http_dispatch),[http_link_to_id/3]).
Daniel@0 29 :- use_module(library(http/html_write)).
Daniel@0 30 :- use_module(library(http/html_head)).
Daniel@0 31 :- use_module(library(dcg_core)).
Daniel@0 32 :- use_module(library(listutils)).
Daniel@0 33 :- use_module(library(optutils)).
Daniel@0 34 :- use_module(library(pengines_io)).
Daniel@0 35 :- use_module(library(swish/render)).
Daniel@0 36 :- use_module(library(code_cache)).
Daniel@0 37 :- use_module(library(insist)).
Daniel@0 38 :- use_module(library(real)).
Daniel@0 39 :- use_module(components(smartimg)).
Daniel@0 40
Daniel@0 41 :- register_renderer(rfig,"Render R figures").
Daniel@0 42
Daniel@0 43 term_rendering(rfig(Code),_,Opts) -->
Daniel@0 44 {rendering_options(Opts,Opts1)},
Daniel@0 45 figure(r(Code),Opts1).
Daniel@0 46 term_rendering(rfig(Code,W,H),_,Opts) -->
Daniel@0 47 term_rendering(rfig(Code),_,[width(W),height(H)|Opts]).
Daniel@0 48 term_rendering(rfig(Code,FOpts),_,Opts) -->
Daniel@0 49 {merge_options(FOpts,Opts,Opts1)},
Daniel@0 50 term_rendering(rfig(Code),_,Opts1).
Daniel@0 51
Daniel@0 52 rendering_options -->
Daniel@0 53 seqmap(option_default_select,[module(_),numbervars(_),quoted(_)],_).
Daniel@0 54
Daniel@0 55
Daniel@0 56 %% figure(+Code:callable, +Width:natural, +Height:natural, +Params:list)// is det.
Daniel@0 57 %% figure(+Code:callable, +Width:natural, +Height:natural)// is det.
Daniel@0 58 %
Daniel@0 59 % Component to render an R figure into a HTML code. Code must be a Prolog
Daniel@0 60 % goal that renders the desired figure.
Daniel@0 61 % The default figure format (eg SVG or PNG) is taken from the default_figure_format
Daniel@0 62 % setting. See figure_render/1 for valid parameters.
Daniel@0 63 figure(Code,W,H) --> figure(Code,[width(W),height(H)]).
Daniel@0 64 figure(Code,W,H,Params) --> figure(Code,[width(W),height(H)|Params]).
Daniel@0 65 figure(Code,Params) -->
Daniel@0 66 { debug(r_fig,"figure(~q,~q)",[Code,Params]),
Daniel@0 67 process_options(Download,Smart,Code,Params,Params1),
Daniel@0 68 insist(option(width(W),Params1), missing_parameter(width)),
Daniel@0 69 insist(option(height(H),Params1), missing_parameter(height)),
Daniel@0 70 http_link_to_id(r_figure_render, Params1, URL)
Daniel@0 71 },
Daniel@0 72 ( {Download=true}
Daniel@0 73 -> { option_default_select(format(_),_,Params1,Params2)},
Daniel@0 74 html(div([\image(Smart,URL,W,H),br([]),'Download as:', \download(pdf,Params2), \download(eps,Params2)]))
Daniel@0 75 ; image(Smart,URL,W,H)
Daniel@0 76 ).
Daniel@0 77
Daniel@0 78 download(F,Params) -->
Daniel@0 79 {http_link_to_id(r_figure_render,[format(F)|Params],URL)},
Daniel@0 80 html([' ',a([href(URL),download],F)]).
Daniel@0 81
Daniel@0 82
Daniel@0 83 process_options(Download,Smart,Code) -->
Daniel@0 84 {setting(r_plot:default_figure_format,Fmt0)},
Daniel@0 85 seqmap(option_default_select, [format(Fmt), cache(Cache), downloadable(Download), smart(Smart)],
Daniel@0 86 [Fmt0,false,false,false]),
Daniel@0 87 (select_option(color_map(CM)) -> {term_to_atom(CM,CMA)}, cons(color_map(CMA)); []),
Daniel@0 88 (select_option(size(W,H)) -> cons(width(W)), cons(height(H)); []),
Daniel@0 89 {Cache=true -> cache_code(Download,Code,Code1); Code=Code1},
Daniel@0 90 {term_to_atom(Code1,CodeAtom)},
Daniel@0 91 cons(format(Fmt)),
Daniel@0 92 cons(code(CodeAtom)).