Discussion:
[Rcpp-devel] Create scalar values in Rcpp
Avraham Adler
2018-12-10 16:24:03 UTC
Permalink
There are no scalars in R; there are vectors of length 1.

https://stat.ethz.ch/pipermail/r-help/2003-April/032010.html

Avi
Hi,
What's the canonical way of creating a scalar SEXP in Rcpp? E.g. I would
like to store a single numeric / integer / complex ... value. I can create
an array with 1 element, but I couldn't find a way to do it for a scalar
(and I figured out later that a scalar is *not* the same as a 1element
array even though they print the same).
Any pointers?
Thanks,
Wolf
_______________________________________________
Rcpp-devel mailing list
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
--
Sent from Gmail Mobile
Dirk Eddelbuettel
2018-12-10 17:06:20 UTC
Permalink
Jeff,

On 10 December 2018 at 08:43, Jeff Newmiller wrote:
| Your use of the term "array" in this context suggests you still don't have a firm grasp of R data types. R arrays are vectors with dim attributes. What you want is to create an R vector with 1 element, as shown in the quick reference. [1]

No need to shoot the messenger. I happen to know Wolf comes from C++ and
Pythonland where array is well defined term.

But yes, over here some things are different. But not everybody has spent
10+ (or 20+) years in R and S land as you and I.

Dirk
--
http://dirk.eddelbuettel.com | @eddelbuettel | ***@debian.org
Jeff Newmiller
2018-12-10 17:56:54 UTC
Permalink
I did not intend to shoot anyone... but my own learning curve led me to skip over the clear descriptions in the "Introduction to R" document because I thought I already understood the terminology. True, it happened a long time ago, but it took me an embarrassingly long time to grok this because the terminology seemed so familiar, and I searched on the wrong terms because of it.
Post by Dirk Eddelbuettel
Jeff,
| Your use of the term "array" in this context suggests you still don't
have a firm grasp of R data types. R arrays are vectors with dim
attributes. What you want is to create an R vector with 1 element, as
shown in the quick reference. [1]
No need to shoot the messenger. I happen to know Wolf comes from C++ and
Pythonland where array is well defined term.
But yes, over here some things are different. But not everybody has spent
10+ (or 20+) years in R and S land as you and I.
Dirk
--
Sent from my phone. Please excuse my brevity.
Dirk Eddelbuettel
2018-12-10 16:33:53 UTC
Permalink
On 10 December 2018 at 11:24, Avraham Adler wrote:
| There are no scalars in R; there are vectors of length 1.

Yep. An R feature, and gotcha if you come from somwhere else. See

R> a <- 1
R> is.vector(a)
[1] TRUE
R>

And while we can do this

R> Rcpp::cppFunction("double wolf(double x) { return x+x; }")
R> wolf(21.0)
[1] 42
R>

it involves a combination of casts and our template magic so make it SEXPs
which, as representations of R types, are always vectors.

Dirk
--
http://dirk.eddelbuettel.com | @eddelbuettel | ***@debian.org
Loading...