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