Interface predicates to commonly used R functions. This library allows Prolog users to do same simple tasks in R with out writing any R code or call any R functions explicitly.
Dependancies
date(Y,M,D)
).
?- version( Vers, Date ). Vers = 0:7:0, Date = date(2021, 9, 2).
?- requires( colours_pie/1 ). ?- c25( C25 ), colours_pie( C25 ).
c(c11,cl2,c3)
. This should be digestable by real as an
input to a named argument. Requires library(real).
Opts
?- lib(b_real:c25/1). ?- c25(C25), colours_pie( C25 ). ?- lib(b_real:colour_cb/1). ?- colour_cb(Cb), colours_pie( Cb ). ?- lib(real). ?- <- library("RColorBrewer"). ?- Set1 <- brewer.pal(9,"Set1"), colours_pie( Set1, main(main) ). ?- colfunc <- colorRampPalette(c("white", "blue")),Ten <- colfunc(10),colours_pie(Ten). ?- lib(r("colorspace")). ?- Pal <- diverge_hcl(7), colours_pie( Pal ). ?- Pal <- sequential_hcl(7), colours_pie( Pal ). % gives you shades of blue ?- R <- rainbow_hcl( 4 ), colours_pie( R ). ?- colours_pie( ["#008000","#CC0000"], weights(c(1,2)) ). ?- colours_pie( ["#008000","#CC0000"], [weights(c(1,2)),prefix(false)] ). ?- colours_pie( ["#008000","#CC0000"], [weights(c(1,1)),labels([a,b])] ).
?- lib(b_real:colours_pie/1). ?- colour_cb(Cb), colours_pie(Cb).
?- lib(b_real:colours_pie/1). ?- colour_cbb(Cbb), colours_pie(Cbb).
note that standard version of aaheatmap()
has a bug
when breaks and colours are given: https://github.com/renozao/NMF/issues/12
2014/08/12
Opts
aheatmap()
call additional parameters given in Name(Value) syntax?- Mpg <- mtcars$mpg, aheatmap( Mpg, [rowV(false),colV(false)] ). ?- Mpg <- mtcars$mpg, x <- [Mpg], colnames(x) <- rownames(mtcars), aheatmap( x, [cellheight=32,scale(false),rowV(false)] ).
Pval is the anova p.value of Clm2 ~ Clm1, Coef is the coefficient of the regression, R2 is R squared (coefficient of determination). See options.
Opts
?- mtx_data( mtcars, Mt ), lm_plot( mpg, disp, mtx(Mt) ). ?- assert( lm_mtx( [row(a,b,c),row(1,2,3),row(2,3,4),row(3,4,5)] ) ). ?- lm_mtx( Mtx ), lm_plot( a, b, C, R2, P, mtx(Mtx) ). C = 1.0, P = 0.0. ?- lm_mtx(Mtx), lm_plot(Mtx,a,b,C,R2,P,true,). C = 1.0, P = 0.0. ?- ls. what.png ?- use_module( library(by_unix) ). ?- @ eog(what.png).
Opts is a combination of options controlling the predicate as per normal
Prolog convention, and term structures thmove( false, _Df )
.
gg_bar_plot_rvar_remove( true, Df )
:-
<- remove( Df )
at translate to '+' ggplot2 terms.
Originally this only supported lists at the value part of Pairs. Now single values make the predicate plot a non-grouped barplot.
Opts
scale_fill_manual()
term, true use def. colours, else give list of coloursgeom_bar()
termggplot2()
one). only works for Clrs \== falsepdf("myfile.pdf")
?- Pairs = [a-[1,2,3],b-[2,4,6]], gg_bar_plot( Pairs, true ). ?- FClrs = ["gold1", "#E31A1C", "blue1"], BoldTitle = theme(plot.title(element_text(face(+"bold")))), Pairs = [a-[1,2,3],b-[2,4,6]], gg_bar_plot( Pairs, [debug(true), geom_bar_draw_colour(black), labels(x,y,main), fill_colours(FClrs), gg_terms(BoldTitle),legend_title(leeg)] ). ?- FClrs = ["gold1", "#E31A1C","blue1","darkolivegreen"], Pairs = [a-1,b-2,c-3,d-4], gg_bar_plot( Pairs, [flip(false),geom_bar(empty),fill_colours(FClrs)] ). ?- Pairs = [a-1,b-2,c-3,d-4], gg_bar_plot( Pairs, [flip(false),geom_bar(empty),fill_colours(true),df_rvar_rmv(false)] ).
Panel
?- Pairs = [a-[1,2,3],b-[2,4,6]], gg_bar_plot( Pairs, true ). % shows a plot with grid lines and axis lines ?- Pairs = [a-[1,2,3],b-[2,4,6]], gg_bar_plot( Pairs, panel_theme(blank) ). % shows a plot with neither grid lines and axis lines ?- Pairs = [a-[1,2,3],b-[2,4,6]], gg_bar_plot( Pairs, panel_theme(axes) ). % shows a plot with no-background colour and no grid lines, but with lines on both axes
Opts
Mtx = [ row(v,x,y,z), row(1,0,0,a), row(0,0,1,a), row(0,1,1,b), row(1,1,1,b), row(0,1,0,c) ], assert( ex1(Mtx) ). ?- ex1(Mtx), vectors_subed_gg_bar_plot( [v,x,y], 1, z, mtx(Mtx) ). [[1,1,0],[0,2,1],[1,2,0]]
Opts are passed to
pheatmap()
from omonymous package.
See mtx_column_pheatmap/3. Here we print vertically though.
Opts
pheatmap()
R callPredicate uses r_call/2 which takes its own options.
Dependencies
?- pack_install( real ). ?- use_module( library(real) ). ?- install.packages( "pheatmap" ).
Examples
?- use_module( library(real) ). ?- <- write.csv( mtcars, "mtcars.csv" ). % mtcars is an example dataset in R ?- <- csv_read_file( Mt, 'mtcars.csv' ), assert( mt(Mt) ). ?- mt(Mt), mtx_pheatmap( Mt, [names(1),scale="column"] ). ?- mt(Mt), mtx_pheatmap( Mt, [names(1),scale="column",debug(true)] ).
This is mainly constructed as a demonstration of using R's pheatmap function from the omonymous library (http://cran.r-project.org/web/packages/pheatmap/pheatmap.pdf).
Opts
breakpoints(Bkps=51)
number of breakpoints if Centre is a number (should be colours + 1)centre(Centre=false)
when number is given, center breakpoints around this value
*cname(Cnm)
returns the column name of Cidcolumn(Clm)
returns the values of Cid in Mtx (does not include Cnm)cposition(Cps)
returns the column position of Cidmtx(Mtx)
returns the cannonical representation of Mtxnames(Names=[])
defines heatmap's columns names, either a list or a column identifier in Mtxoutputs(Outs=x11)
a single or list of [x11,pdf], see r_call/2privates(Prv=false)
if true, include private Roptsrvar(Rvar=mcp)
R variable to use for the datarvar_rmv(Rdel=true)
delete the Rvariable at end of call fixme: not implemented yet...stem(Stem)
stem for output files. Default: atomic_list_concat([Cnm,phmap],'_',Stem)
.
Roptprivates(true)
?- use_module( library(real) ). ?- <- write.csv( mtcars, "mtcars.csv" ). % mtcars is an example dataset in R ?- csv_read_file( 'mtcars.csv', Mt ), mtx_column_pheatmap( Mt, 2, [] ). ?- csv_read_file( 'mtcars.csv', Mt ), mtx_column_pheatmap( Mt, 2, [names(1)] ). ?- csv_read_file( 'mtcars.csv', Mt ), mtx_column_pheatmap( Mt, 2, [names(1),legend='TRUE'] ). ?- csv_read_file( 'mtcars.csv', Mt ), mtx_column_pheatmap( Mt, 2, [names(1),outputs(pdf),stem(mtcars_c2)] ). ?- shell( 'ls -l mtcars_c2.pdf' ). -rw------- 1 user user 4212 Jan 13 13:56 mtcars_c2.pdf
rvar_rmv(true)
is in Opts.
The rationale is to make rvar_rmv(_)
a commonly occured option
in b_real predicates.
?- x <- 1. ?- <- x. [1] 1 true. ?- options_rvar_rmv( x, [rvar_rmv(true)] ). ?- <- x. Error in print(x) : object 'x' not found ERROR: R was unable to digest your statement, either syntax or existance error ?- x <- 1. ?- options_rvar_rmv( x, [rvar_rmv(false),debug(true)] ). % Keeping R variable: x true.
Recognisable represenation are:
memberchk(mtx(MTx),Opts)
and mtx_column( Mtx, Cid, Vect )
Opts
cnm(Cnm)
the column name of the vector (return value)cnm_def(Def)
use Def as Cnm when VectSpec is a prolog list. Leaves free if none is given.k(Kid)
return a paired vector where K is taken from Kid column of Mtx (below)-
and V from VectSpecmax(Max)
curtail values > Max to Maxmin(Min)
curtail values < Min to Minmtx(Mtx)
a matrixif_rvar(Rvar=true)
how to treat R variables in VectSpec. true: allows them by passing them to Vect,
false: dissallows R variables, and prolog: allows them by passing their Prolog representation to Vect.v(Vid)
return a paired vector where V is taken from Vid column of Mtx (below)-
and K from VectSpec. Only used if k(Kid)
is not present
Currently, k()
and v()
are inompatible to max()
and min()
.
?- pl_vector( [1,2,3], V, true ). V = [1, 2, 3]. ?- mtx_data( mtcars, Mc ), pl_vector( 1, Vect, [mtx(Mc),cnm(Cnm)] ), max_list( Vect, Max ). Mc = [row(mpg, cyl, disp, hp, drat, wt, qsec, vs, am, gear, carb), row(21.0, 6.0|...], Vect = [21.0, 21.0, 22.8, 21.4, 18.7, 18.1, 14.3, 24.4, 22.8|...], Cnm = mpg, Max = 33.9. ?- mtx_data( mtcars, Mc ), pl_vector( 1, Vect, [mtx(Mc),cnm(Cnm),max(30)] ), max_list( Vect, Max ). Mc = [row(mpg, cyl, disp, hp, drat, wt, qsec, vs, am, gear, carb), row(21.0, 6.0|...], Vect = [21.0, 21.0, 22.8, 21.4, 18.7, 18.1, 14.3, 24.4, 22.8|...], Cnm = mpg, Max = 30.
pack(real)
) understands as a LHS to <- assignment.
That is, if it is a Prolog variable, it will be instantiated to the vector as a list,
or a ground term, will translate that to an R expression (typically an R variable) to which
the scaled vector is assigned to.
For details on VectSpec, see pl_vector/3 and options affecting this.
Opts
See R's scale()
.
?- Mtc <- as.vector( mtcars[*,3] ), Mean <- mean(Mtc), vector_scale( Mtc, Sca, [scale(true)] ), ScaMean <- mean(Sca), ScaVrc <- var(Sca). Mtc = ... Mean = 230.721875, Sca = ... ScaMean = -9.084936579070724e-17, ScaVrc = [[1.0]]. ?- Mtc <- as.vector( mtcars[*,3] ), Mean <- mean(Mtc), vector_scale( Mtc, Sca, [scale(centre)] ), ScaMean <- mean(Sca), ScaVrc <- var(Sca). Mtc = ... Mean = 230.721875, Sca = ... ScaMean = -1.199040866595169e-14, ScaVrc = [[15360.799828629033]].
?- pl_plot_on( <- plot(c(1,2,3)), plot_on(x11()) ). ?- pl_plot_on( <- plot(c(1,2,3)), plot_on(pdf(+ex.pdf)) ). ?- shell( 'evince ex.pdf' ). ?- pl_plot_on( <- plot(c(1,2,3)), plot_on(x11(width=14)) ).
Opts
?- mtx_data( mtcars, Mt ), r_mtx( rv, Mt, [debug(true),rownames(1)] ). ...
If DfIn, is an R variable, and Df is atomic it is assumed to be a data frame and Df <- DfIn, is called (this can be elaborated upon later), whereas if Df is variable then Df = DfIn is called.
If DfIn is not an R variable, it is passed through mtx/2 and the result is passed through mtx_df/2. When Df is a variable in this context, a unique variable is generated that looks like tmp.df.N where N is an integer.
?- r_unique( df.data, Uniq ). Uniq = df.data.1. ?- r_unique( df.data, Uniq ). Uniq = df.data.2.
VectSpec should be as that recognised by the 1st argument of pl_vector/3. A list is the simplest representation of a vector.
Displaying is via r_call/2, so Opts can influence that call.
Opts
?- lib(real). ?- Mtc <- as.matrix(mtcars), r_hist( 2, mtx(Mtc) ). ?- rns <- rnorm(1000). ?- Rns <- rns, r_hist( Rns, name("rnorm") ). ?- Rns <- rns, r_hist( Rns, [name("rnorm"),main="Main Title",outputs(svg),stem(rh1)] ).
Produces file: rh1.svg
[[../doc/images/rh1.svg]]
?- rnsm <- rnorm(10000), r_hist( rnsm, true ). ?- r_hist( rnsm, as_density(true) ). ?- pl_vector( rnsm, Rnsm, if_rvar(prolog) ), r_hist( Rnsm, name(+true) ). ?- r_hist( rnsm, [as_density(true),transparent(false)] ). ?- r_hist( rnsm, [as_density(true),transparency_colour("lightgreen"),outputs(svg),stem(rh2)] ).
Produces file: rh2.svg
[[../doc/images/rh2.svg]]
Opts
?- cd( '/usr/local/users/nicos/work/2015/15.11.04-hmrn/Exploristics/data' ). ?- columns_fisher_test( hmrn_muts_t10.csv, inters, odds, Lods, [plot(true),plot_on(x11()),plot_on(pdf())] ).
Note that at_con/3 is used so '' is handled differently to atomic_list_concat/3.
?- dot( tmp, rv, Dot ). Dot = tmp.rv. ?- dot( [tmp,rv], phase1, Dot ). Dot = tmp.rv.phase1. ?- dot( '', x, Dot ). Dot = x. ?- dot( x, ['',x], Dot ). Dot = x.x.
Opts
?- mtx_df( [row(a,b,c),row(1,2,3),row(4,5,6)], df1 ), <- df1, csv_df( Csv1, df1 ). $a [1] 1 4 $b [1] 2 5 $c [1] 3 6 Csv1 = [row(a, b, c), row(1, 2, 3), row(4, 5, 6)].
This seems to get stuck for very large matrices, (>130,000).
See implementation in r_sqlite_load.pl
.
Opts
The following predicates are exported, but not or incorrectly documented.