[gnumeric] fn-r: generate some sample expressions for help texts.



commit 82212e96aaa8ea11520bdd836cb68c93f2d688e0
Author: Morten Welinder <terra gnome org>
Date:   Wed Nov 22 20:33:44 2017 -0500

    fn-r: generate some sample expressions for help texts.

 plugins/fn-r/functions.c |   15 ++++++++++++++
 plugins/fn-r/generate    |   48 ++++++++++++++++++++++++++++++++++++++++-----
 2 files changed, 57 insertions(+), 6 deletions(-)
---
diff --git a/plugins/fn-r/functions.c b/plugins/fn-r/functions.c
index eec26bd..dd3a0e6 100644
--- a/plugins/fn-r/functions.c
+++ b/plugins/fn-r/functions.c
@@ -22,6 +22,7 @@ static GnmFuncHelp const help_r_dbeta[] = {
        { GNM_FUNC_HELP_ARG, F_("b:the second scale parameter of the distribution") },
        { GNM_FUNC_HELP_ARG, F_("give_log:if true, log of the result will be returned instead") },
        { GNM_FUNC_HELP_DESCRIPTION, F_("This function returns the probability density function of the beta 
distribution.") },
+       { GNM_FUNC_HELP_EXAMPLES, "=r.dbeta(0.4,1,4)" },
        { GNM_FUNC_HELP_SEEALSO, "R.PBETA,R.QBETA" },
        { GNM_FUNC_HELP_END }
 };
@@ -47,6 +48,7 @@ static GnmFuncHelp const help_r_pbeta[] = {
        { GNM_FUNC_HELP_ARG, F_("lower_tail:if true (the default), the lower tail of the distribution is 
considered") },
        { GNM_FUNC_HELP_ARG, F_("log_p:if true, the natural logarithm of the probability is given or 
returned; defaults to false") },
        { GNM_FUNC_HELP_DESCRIPTION, F_("This function returns the cumulative distribution function of the 
beta distribution.") },
+       { GNM_FUNC_HELP_EXAMPLES, "=r.pbeta(0.4,1,4)" },
        { GNM_FUNC_HELP_SEEALSO, "R.DBETA,R.QBETA" },
        { GNM_FUNC_HELP_END }
 };
@@ -73,6 +75,7 @@ static GnmFuncHelp const help_r_qbeta[] = {
        { GNM_FUNC_HELP_ARG, F_("lower_tail:if true (the default), the lower tail of the distribution is 
considered") },
        { GNM_FUNC_HELP_ARG, F_("log_p:if true, the natural logarithm of the probability is given or 
returned; defaults to false") },
        { GNM_FUNC_HELP_DESCRIPTION, F_("This function returns the probability quantile function, i.e., the 
inverse of the cumulative distribution function, of the beta distribution.") },
+       { GNM_FUNC_HELP_EXAMPLES, "=r.qbeta(0.3,1,4)" },
        { GNM_FUNC_HELP_SEEALSO, "R.DBETA,R.PBETA" },
        { GNM_FUNC_HELP_END }
 };
@@ -98,6 +101,7 @@ static GnmFuncHelp const help_r_dbinom[] = {
        { GNM_FUNC_HELP_ARG, F_("psuc:the probability of success in each trial") },
        { GNM_FUNC_HELP_ARG, F_("give_log:if true, log of the result will be returned instead") },
        { GNM_FUNC_HELP_DESCRIPTION, F_("This function returns the probability density function of the 
binomial distribution.") },
+       { GNM_FUNC_HELP_EXAMPLES, "=r.dbinom(4,10,0.4)" },
        { GNM_FUNC_HELP_SEEALSO, "R.PBINOM,R.QBINOM" },
        { GNM_FUNC_HELP_END }
 };
@@ -123,6 +127,7 @@ static GnmFuncHelp const help_r_pbinom[] = {
        { GNM_FUNC_HELP_ARG, F_("lower_tail:if true (the default), the lower tail of the distribution is 
considered") },
        { GNM_FUNC_HELP_ARG, F_("log_p:if true, the natural logarithm of the probability is given or 
returned; defaults to false") },
        { GNM_FUNC_HELP_DESCRIPTION, F_("This function returns the cumulative distribution function of the 
binomial distribution.") },
+       { GNM_FUNC_HELP_EXAMPLES, "=r.pbinom(4,10,0.4)" },
        { GNM_FUNC_HELP_SEEALSO, "R.DBINOM,R.QBINOM" },
        { GNM_FUNC_HELP_END }
 };
@@ -149,6 +154,7 @@ static GnmFuncHelp const help_r_qbinom[] = {
        { GNM_FUNC_HELP_ARG, F_("lower_tail:if true (the default), the lower tail of the distribution is 
considered") },
        { GNM_FUNC_HELP_ARG, F_("log_p:if true, the natural logarithm of the probability is given or 
returned; defaults to false") },
        { GNM_FUNC_HELP_DESCRIPTION, F_("This function returns the probability quantile function, i.e., the 
inverse of the cumulative distribution function, of the binomial distribution.") },
+       { GNM_FUNC_HELP_EXAMPLES, "=r.qbinom(0.3,10,0.4)" },
        { GNM_FUNC_HELP_SEEALSO, "R.DBINOM,R.PBINOM" },
        { GNM_FUNC_HELP_END }
 };
@@ -174,6 +180,7 @@ static GnmFuncHelp const help_r_dcauchy[] = {
        { GNM_FUNC_HELP_ARG, F_("scale:the scale parameter of the distribution") },
        { GNM_FUNC_HELP_ARG, F_("give_log:if true, log of the result will be returned instead") },
        { GNM_FUNC_HELP_DESCRIPTION, F_("This function returns the probability density function of the Cauchy 
distribution.") },
+       { GNM_FUNC_HELP_EXAMPLES, "=r.dcauchy(1.5,1,4)" },
        { GNM_FUNC_HELP_SEEALSO, "R.PCAUCHY,R.QCAUCHY" },
        { GNM_FUNC_HELP_END }
 };
@@ -199,6 +206,7 @@ static GnmFuncHelp const help_r_pcauchy[] = {
        { GNM_FUNC_HELP_ARG, F_("lower_tail:if true (the default), the lower tail of the distribution is 
considered") },
        { GNM_FUNC_HELP_ARG, F_("log_p:if true, the natural logarithm of the probability is given or 
returned; defaults to false") },
        { GNM_FUNC_HELP_DESCRIPTION, F_("This function returns the cumulative distribution function of the 
Cauchy distribution.") },
+       { GNM_FUNC_HELP_EXAMPLES, "=r.pcauchy(1.5,1,4)" },
        { GNM_FUNC_HELP_SEEALSO, "R.DCAUCHY,R.QCAUCHY" },
        { GNM_FUNC_HELP_END }
 };
@@ -225,6 +233,7 @@ static GnmFuncHelp const help_r_qcauchy[] = {
        { GNM_FUNC_HELP_ARG, F_("lower_tail:if true (the default), the lower tail of the distribution is 
considered") },
        { GNM_FUNC_HELP_ARG, F_("log_p:if true, the natural logarithm of the probability is given or 
returned; defaults to false") },
        { GNM_FUNC_HELP_DESCRIPTION, F_("This function returns the probability quantile function, i.e., the 
inverse of the cumulative distribution function, of the Cauchy distribution.") },
+       { GNM_FUNC_HELP_EXAMPLES, "=r.qcauchy(0.3,1,4)" },
        { GNM_FUNC_HELP_SEEALSO, "R.DCAUCHY,R.PCAUCHY" },
        { GNM_FUNC_HELP_END }
 };
@@ -250,6 +259,7 @@ static GnmFuncHelp const help_r_dchisq[] = {
        { GNM_FUNC_HELP_ARG, F_("give_log:if true, log of the result will be returned instead") },
        { GNM_FUNC_HELP_DESCRIPTION, F_("This function returns the probability density function of the 
chi-square distribution.") },
        { GNM_FUNC_HELP_ODF, F_("A two argument invocation R.DCHISQ(@{x},@{df}) is exported to OpenFormula as 
CHISQDIST(@{x},@{df},FALSE()).") },
+       { GNM_FUNC_HELP_EXAMPLES, "=r.dchisq(2.5,4)" },
        { GNM_FUNC_HELP_SEEALSO, "R.PCHISQ,R.QCHISQ" },
        { GNM_FUNC_HELP_END }
 };
@@ -274,6 +284,7 @@ static GnmFuncHelp const help_r_pchisq[] = {
        { GNM_FUNC_HELP_ARG, F_("log_p:if true, the natural logarithm of the probability is given or 
returned; defaults to false") },
        { GNM_FUNC_HELP_DESCRIPTION, F_("This function returns the cumulative distribution function of the 
chi-square distribution.") },
        { GNM_FUNC_HELP_ODF, F_("A two argument invocation R.PCHISQ(@{x},@{df}) is exported to OpenFormula as 
CHISQDIST(@{x},@{df}).") },
+       { GNM_FUNC_HELP_EXAMPLES, "=r.pchisq(2.5,4)" },
        { GNM_FUNC_HELP_SEEALSO, "R.DCHISQ,R.QCHISQ" },
        { GNM_FUNC_HELP_END }
 };
@@ -299,6 +310,7 @@ static GnmFuncHelp const help_r_qchisq[] = {
        { GNM_FUNC_HELP_ARG, F_("log_p:if true, the natural logarithm of the probability is given or 
returned; defaults to false") },
        { GNM_FUNC_HELP_DESCRIPTION, F_("This function returns the probability quantile function, i.e., the 
inverse of the cumulative distribution function, of the chi-square distribution.") },
        { GNM_FUNC_HELP_ODF, F_("A two argument invocation R.QCHISQ(@{p},@{df}) is exported to OpenFormula as 
CHISQINV(@{p},@{df}).") },
+       { GNM_FUNC_HELP_EXAMPLES, "=r.qchisq(0.3,4)" },
        { GNM_FUNC_HELP_SEEALSO, "R.DCHISQ,R.PCHISQ" },
        { GNM_FUNC_HELP_END }
 };
@@ -322,6 +334,7 @@ static GnmFuncHelp const help_r_dexp[] = {
        { GNM_FUNC_HELP_ARG, F_("scale:the scale parameter of the distribution") },
        { GNM_FUNC_HELP_ARG, F_("give_log:if true, log of the result will be returned instead") },
        { GNM_FUNC_HELP_DESCRIPTION, F_("This function returns the probability density function of the 
exponential distribution.") },
+       { GNM_FUNC_HELP_EXAMPLES, "=r.dexp(1.5,4)" },
        { GNM_FUNC_HELP_SEEALSO, "R.PEXP,R.QEXP" },
        { GNM_FUNC_HELP_END }
 };
@@ -345,6 +358,7 @@ static GnmFuncHelp const help_r_pexp[] = {
        { GNM_FUNC_HELP_ARG, F_("lower_tail:if true (the default), the lower tail of the distribution is 
considered") },
        { GNM_FUNC_HELP_ARG, F_("log_p:if true, the natural logarithm of the probability is given or 
returned; defaults to false") },
        { GNM_FUNC_HELP_DESCRIPTION, F_("This function returns the cumulative distribution function of the 
exponential distribution.") },
+       { GNM_FUNC_HELP_EXAMPLES, "=r.pexp(1.5,4)" },
        { GNM_FUNC_HELP_SEEALSO, "R.DEXP,R.QEXP" },
        { GNM_FUNC_HELP_END }
 };
@@ -369,6 +383,7 @@ static GnmFuncHelp const help_r_qexp[] = {
        { GNM_FUNC_HELP_ARG, F_("lower_tail:if true (the default), the lower tail of the distribution is 
considered") },
        { GNM_FUNC_HELP_ARG, F_("log_p:if true, the natural logarithm of the probability is given or 
returned; defaults to false") },
        { GNM_FUNC_HELP_DESCRIPTION, F_("This function returns the probability quantile function, i.e., the 
inverse of the cumulative distribution function, of the exponential distribution.") },
+       { GNM_FUNC_HELP_EXAMPLES, "=r.qexp(0.3,4)" },
        { GNM_FUNC_HELP_SEEALSO, "R.DEXP,R.PEXP" },
        { GNM_FUNC_HELP_END }
 };
diff --git a/plugins/fn-r/generate b/plugins/fn-r/generate
index 60b36e4..8e91488 100644
--- a/plugins/fn-r/generate
+++ b/plugins/fn-r/generate
@@ -51,7 +51,9 @@ my %defaults;
         'beta',
         ({ 'a' => "the first shape parameter $of",
            'b' => "the second scale parameter $of",
-           @common })];
+           @common }),
+        [ { 'x' => 0.4, 'a' => 1, 'b' => 4 } ],
+       ];
 
     $funcs{'dt'} = $funcs{'pt'} = $funcs{'qt'} =
        [\&distribution,
@@ -70,7 +72,9 @@ my %defaults;
        [\&distribution,
         'chi-square',
         ({ 'df' => "the number of degrees of freedom $of",
-           @common })];
+           @common }),
+        [ { 'x' => 2.5, 'df' => 4 } ],
+       ];
 
     $funcs{'dweibull'} = $funcs{'pweibull'} = $funcs{'qweibull'} =
        [\&distribution,
@@ -86,14 +90,18 @@ my %defaults;
     $funcs{'dexp'} = $funcs{'pexp'} = $funcs{'qexp'} =
        [\&distribution,
         'exponential',
-        ({ @common })];
+        ({ @common }),
+        [ { 'x' => 1.5, 'scale' => 4 } ],
+       ];
 
     $funcs{'dbinom'} = $funcs{'pbinom'} = $funcs{'qbinom'} =
        [\&distribution,
         'binomial',
         ({ 'n' => 'the number of trials',
            'psuc' => "the probability of success in each trial",
-           @common })];
+           @common }),
+        [ { 'x' => 4, 'n' => 10, 'psuc' => 0.4 } ],
+       ];
 
     $funcs{'dnbinom'} = $funcs{'pnbinom'} = $funcs{'qnbinom'} =
        [\&distribution,
@@ -115,7 +123,9 @@ my %defaults;
        [\&distribution,
         'Cauchy',
         ({ 'location' => "the center $of",
-           @common })];
+           @common }),
+        [ { 'x' => 1.5, 'location' => 1, 'scale' => 4 } ],
+       ];
 
     $funcs{'dgeom'} = $funcs{'pgeom'} = $funcs{'qgeom'} =
        [\&distribution,
@@ -279,7 +289,7 @@ sub cfileorder {
 # -----------------------------------------------------------------------------
 
 sub distribution {
-    my ($func,$restype,$proto,$distname,$argdescs) = @_;
+    my ($func,$restype,$proto,$distname,$argdescs,$psamples) = @_;
 
     my $args = $proto;
     $args =~ s/^.*\((.*)\)\s*;$/$1/;
@@ -330,6 +340,32 @@ sub distribution {
        &emit ("\t{ GNM_FUNC_HELP_ODF, F_(\"$odf\") },\n");
     }
 
+    my $sdef = { 'p' => 0.3,
+                'give_log' => undef,
+                'lower_tail' => undef};
+
+    if ($psamples) {
+      SAMPLE:
+       foreach my $s (@$psamples) {
+           my @sample_args = ();
+         ARG:
+           foreach (@args) {
+               my ($type,$name) = @{ $_ };
+               if (exists $s->{$name}) {
+                   my $a = $s->{$name};
+                   push @sample_args, $a;
+               } elsif (exists $sdef->{$name}) {
+                   my $a = $sdef->{$name};
+                   last ARG unless defined $a;
+                   push @sample_args, $a;
+               } else {
+                   next SAMPLE;
+               }
+           }
+           &emit ("\t{ GNM_FUNC_HELP_EXAMPLES, \"=r.$func(" . join (",", @sample_args) . ")\" },\n");
+       }
+    }
+
     my $seealso = "";
     my $f1 = substr ($func, 1);
     my $F1 = uc ($f1);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]