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 <[email protected]> wrote:
> 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
> <[email protected]>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
>>
>> Am I doing something wrong? Thanks!
>>
>> _______________________________________________
>> Rcpp-devel mailing list
>> [email protected]
>> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
>>
>
>
_______________________________________________
Rcpp-devel mailing list
[email protected]
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel