Skip to content

[Rcpp-devel] Default value for Rcpp List

9 messages · Matteo Fasiolo, JJ Allaire, Dirk Eddelbuettel +1 more

#
Dear Rcpp users,

 a very simple question: I have a function that has a Rcpp::List
among its arguments, and I would like to set a default values for
that List.
Unfortunately this code:

cppFunction(
  '
  List myList(List x = List::create(_["a"] = 1, _["b"] = 2))
 {
  return x;
 }
 '
)

raises the warning:

Warning message:Unable to parse C++ default value 'List::create(_["a"]
= 1, _["b"] = 2)' for argument x of function myList >
Similar code with NumericVector works fine:

cppFunction(
  '
  NumericVector myVett(NumericVector x = NumericVector::create(3))
 {
  return x;
 }
 '
 )

myVett()
# [1] 0 0 0

Am I doing something wrong? Thanks!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20130814/5fc791c7/attachment.html>
#
On 14 August 2013 at 11:42, Matteo Fasiolo wrote:
| Dear Rcpp users,
| 
| ?a very simple question: I have a function that has a Rcpp::List
| among its arguments, and I would like to set a default values for
| that List.
| Unfortunately this code:
| 
| cppFunction(
| ? '
| ? List myList(List x = List::create(_["a"] = 1, _["b"] = 2))
| ?{
| ? return x;
| ?}
| ?'?
| )
| 
| raises the warning:
| 
| 
| Warning message:
| Unable to parse C++ default value 'List::create(_["a"] = 1, _["b"] = 2)' for argument x of function myList
| >
| > myList()
| Error in .Primitive(".Call")(<pointer: 0xb5907fb0>, x) : 'x' is missing
| 
| 
| Similar code with NumericVector works fine:
| 
| cppFunction(
| ? '
| ? NumericVector myVett(NumericVector x = NumericVector::create(3))
| ?{
| ? return x;
| ?}
| ?'?
| ?)
| 
| myVett()
| # [1] 0 0 0

Not really. You just allocated space, not values.

I suspect this doesn't work the way you want it to.
 
| Am I doing something wrong? Thanks!

You could create the objects "empty", test in the function body for emptyness
and set values.

Dirk
 
| ----------------------------------------------------------------------
| _______________________________________________
| Rcpp-devel mailing list
| Rcpp-devel at lists.r-forge.r-project.org
| https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
#
Hi Matteo,

The issue here is that the Rcpp attributes code that parses function
declarations isn't able to parse all syntactic forms of C++ but rather a
subset. The default argument parsing is able to handle scalars, strings,
and simple vector initializations but not more complex expressions like the
one in your example. The warning you get is saying that the default
argument couldn't be parsed as a result of these limitations. The lack of a
default argument definition is then what caused the subsequent error.

Best,

J.J.
On Wed, Aug 14, 2013 at 3:42 AM, Matteo Fasiolo <matteo.fasiolo at gmail.com>wrote:

            
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20130814/98d0333e/attachment.html>
#
Dear Dirk and JJ,

 thank you very much for your replies. In the end a named vector is
enough for my purpose:

cppFunction(
  '
  NumericVector myVett(NumericVector x = NumericVector::create(0) )
 {
  if(x.size() == 0) x = NumericVector::create(_["a"] = 1.0, _["b"] = 2.0);
  return x;
 }
 '
)

myVett()

a b
1 2



Thanks,

Matteo
On Wed, Aug 14, 2013 at 2:41 PM, JJ Allaire <jj.allaire at gmail.com> wrote:

            
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20130814/7b1bd615/attachment.html>
#
On 14 August 2013 at 15:57, Matteo Fasiolo wrote:
| ?thank you very much for your replies. In the end a named vector is
| enough for my purpose:
| 
| cppFunction(
| ? '
| ? NumericVector myVett(NumericVector x = NumericVector::create(0) )
| ?{
| ? if(x.size() == 0) x = NumericVector::create(_["a"] = 1.0, _["b"] = 2.0);
| ? return x;
| ?}
| ?'?
| )

You don't even need the assignment in the function definition:

R> cppFunction("NumericVector matteo(NumericVector x) { if (x.size()==0) x = NumericVector(4); return(x); }")
R> matteo(vector())
[1] 0 0 0 0
R> matteo(1:3)
[1] 1 2 3
R> 

Dirk
2 days later
#
Le 14/08/13 16:25, Dirk Eddelbuettel a ?crit :
This might not stay valid code when we put more control for dispatch, 
argument recognition etc.

Some of that is in place with Rcpp::is, the rest will follow. We might 
lose things like this, but we will gain more useful things instead.
And we never said passing 0 arguments to a function needing one works, 
so it is not a documented feature.

Romain
#
Hi Romain,
On 16 August 2013 at 19:24, Romain Francois wrote:
| Le 14/08/13 16:25, Dirk Eddelbuettel a ?crit :
| >
| >
| > On 14 August 2013 at 15:57, Matteo Fasiolo wrote:
| > |  thank you very much for your replies. In the end a named vector is
| > | enough for my purpose:
| > |
| > | cppFunction(
| > |   '
| > |   NumericVector myVett(NumericVector x = NumericVector::create(0) )
| > |  {
| > |   if(x.size() == 0) x = NumericVector::create(_["a"] = 1.0, _["b"] = 2.0);
| > |   return x;
| > |  }
| > |  '
| > | )
| >
| > You don't even need the assignment in the function definition:
| >
| > R> cppFunction("NumericVector matteo(NumericVector x) { if (x.size()==0) x = NumericVector(4); return(x); }")
| > R> matteo(vector())
| > [1] 0 0 0 0
| > R> matteo(1:3)
| > [1] 1 2 3
| > R>
| >
| > Dirk
| 
| This might not stay valid code when we put more control for dispatch, 
| argument recognition etc.
| 
| Some of that is in place with Rcpp::is, the rest will follow. We might 
| lose things like this, but we will gain more useful things instead.
| And we never said passing 0 arguments to a function needing one works, 
| so it is not a documented feature.

I am missing something here.  

I wrote a function with one argument, I supply one argument in each of the
two calls -- the argument just happens to be an empty vector precisiely
because I test for size in the function body.

I don't see anything fishy or questionable here.  

Dirk
#
Le 16/08/13 19:33, Dirk Eddelbuettel a ?crit :
It was me who missed something actually. I though you called

matteo()

To my defense, I'm still recovering from an insolation.

  
    
#
On 16 August 2013 at 19:38, Romain Francois wrote:
| It was me who missed something actually. I though you called
| 
| matteo()

No worries. I would have called that usage pattern out too!
 
| To my defense, I'm still recovering from an insolation.

Hope you get better soon.  At least you have the weekend back at home.

Dirk