Hi
Im trying to use some C++ functions in R. However, these functions are
build around unsigned char* variables (1D array). I have for example a
large matrix in R that represents black (e.g. 1-bit image, 0 and 1s). I
cant find a solution to convert a character vector into unsigned char*
within Rcpp. Im new to Rcpp and C++ so I might have missed something
obvious here. Please direct me to resources on this topic (google did
not help much).
A start:
SEXP test(SEXP cM){
CharacterVector Vc(cM);
int n = Vr.size();
RawVector x(n); //is this the same as unsigned char* ?
//I tried to fill it with char*(Mr) but I
didnt succeed
}
Thanks,
Gustaf
Gustaf Granath (PhD)
Post doc
McMaster University
School of Geography & Earth Sciences
On 20 October 2014 at 15:17, Gustaf Granath wrote:
| Im trying to use some C++ functions in R. However, these functions are
| build around unsigned char* variables (1D array). I have for example a
| large matrix in R that represents black (e.g. 1-bit image, 0 and 1s). I
| cant find a solution to convert a character vector into unsigned char*
| within Rcpp. Im new to Rcpp and C++ so I might have missed something
Rcpp builds a bridge between R and C++, and supports (in C++) the types that
R supports. Eg you won't find 'float' but only 'double' as that is what R
does.
Here you will get character vectors easily. If they must be unsigned char,
you will probably have to cast them at the C++ level.
| obvious here. Please direct me to resources on this topic (google did
| not help much).
|
| A start:
|
| SEXP test(SEXP cM){
| CharacterVector Vc(cM);
| int n = Vr.size();
| RawVector x(n); //is this the same as unsigned char* ?
No, RawVector is for R 'raw' types -- see help("raw") in R,
Dirk
| //I tried to fill it with char*(Mr) but I
| didnt succeed
| }
|
| Thanks,
|
| Gustaf
|
| --
| Gustaf Granath (PhD)
| Post doc
| McMaster University
| School of Geography & Earth Sciences
|
| _______________________________________________
| 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
Le 20 oct. 2014 ? 21:54, Dirk Eddelbuettel <edd at debian.org> a ?crit :
Hi Gustaf,
On 20 October 2014 at 15:17, Gustaf Granath wrote:
| Im trying to use some C++ functions in R. However, these functions are
| build around unsigned char* variables (1D array). I have for example a
| large matrix in R that represents black (e.g. 1-bit image, 0 and 1s). I
| cant find a solution to convert a character vector into unsigned char*
| within Rcpp. Im new to Rcpp and C++ so I might have missed something
Rcpp builds a bridge between R and C++, and supports (in C++) the types that
R supports. Eg you won't find 'float' but only 'double' as that is what R
does.
Here you will get character vectors easily. If they must be unsigned char,
you will probably have to cast them at the C++ level.
| obvious here. Please direct me to resources on this topic (google did
| not help much).
|
| A start:
|
| SEXP test(SEXP cM){
| CharacterVector Vc(cM);
| int n = Vr.size();
| RawVector x(n); //is this the same as unsigned char* ?
No, RawVector is for R 'raw' types -- see help("raw") in R,
Guess what raw is !!!
demangle( "RawVector::stored_type" )
[1] "unsigned char"
Dirk
| //I tried to fill it with char*(Mr) but I
| didnt succeed
| }
|
| Thanks,
|
| Gustaf
|
| --
| Gustaf Granath (PhD)
| Post doc
| McMaster University
| School of Geography & Earth Sciences
|
| _______________________________________________
| 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
--
http://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org
Hi,
that's not going to fly.
A CharacterVector holds strings of arbitrary lengths, well technically it holds other R objects (SEXP) that hold pointers to strings.
A RawVector holds unsigned char.
Can you add some meat to your example, e.g. what you'd expect to go in, etc ...
Romain
Le 20 oct. 2014 ? 21:17, Gustaf Granath <gustaf.granath at gmail.com> a ?crit :
Hi
Im trying to use some C++ functions in R. However, these functions are build around unsigned char* variables (1D array). I have for example a large matrix in R that represents black (e.g. 1-bit image, 0 and 1s). I cant find a solution to convert a character vector into unsigned char* within Rcpp. Im new to Rcpp and C++ so I might have missed something obvious here. Please direct me to resources on this topic (google did not help much).
A start:
SEXP test(SEXP cM){
CharacterVector Vc(cM);
int n = Vr.size();
RawVector x(n); //is this the same as unsigned char* ?
//I tried to fill it with char*(Mr) but I didnt succeed
}
Thanks,
Gustaf
--
Gustaf Granath (PhD)
Post doc
McMaster University
School of Geography & Earth Sciences
On 20 October 2014 at 23:09, Romain Francois wrote:
| > Le 20 oct. 2014 ? 21:54, Dirk Eddelbuettel <edd at debian.org> a ?crit :
| > No, RawVector is for R 'raw' types -- see help("raw") in R,
|
| Guess what raw is !!!
|
| > demangle( "RawVector::stored_type" )
| [1] "unsigned char"
Touche, but for all uses I have seen (eg around serialization) raw was not
meant as the type Gustaf implied in his question. Then again Gustaf's post
was a little short on specifics ...
Dirk
On Mon, Oct 20, 2014 at 2:17 PM, Gustaf Granath <gustaf.granath at gmail.com>
wrote:
Hi
Im trying to use some C++ functions in R. However, these functions are
build around unsigned char* variables (1D array). I have for example a
large matrix in R that represents black (e.g. 1-bit image, 0 and 1s). I
cant find a solution to convert a character vector into unsigned char*
within Rcpp. Im new to Rcpp and C++ so I might have missed something
obvious here. Please direct me to resources on this topic (google did not
help much).
A start:
SEXP test(SEXP cM){
CharacterVector Vc(cM);
int n = Vr.size();
RawVector x(n); //is this the same as unsigned char* ?
//I tried to fill it with char*(Mr) but I
didnt succeed
}
I tend to go through the standard library as its less of a black box. Try
creating a std::string and appending your chars to that string. Then call
obj.c_str() to get the C-style array of chars. Or push the chars onto a
std::vector and do something like &obj[0] to get a 1-D array.
THK
Thanks,
Gustaf
--
Gustaf Granath (PhD)
Post doc
McMaster University
School of Geography & Earth Sciences
Hi all
Thanks for the replies. After some testing it seems like I dont need the
unsigned stuff and the functions seems to work fine with just a regular
int variable. The functions I wanted to use were just small parts of a
program and in that program bitmap images were loaded and image data
stored as a unsigned char* (for bitmaps unsigned char are often used due
to the 0-255 property I believe). I thought the simpliest solution was
to stick to the original set up (so replicating the variables) but it
seems that is not necessary which make things easier.
However, I got curious how to handle this if I need it.
First, I still dont get the difference between R raw and unsigned char
(if there is one). E.g.
this:
unsigned char a = 'b';
cout << hex << a << endl;
equals
> charToRaw("b")
[1] 62
So can unsigned char be used within Rcpp with RawVector?
For example, is it possible to go from int/char to raw using Rcpp? Or is
the recommended way to use unsigned char to go outside Rcpp as Tim
suggested?
Again, thanks for the input,
Gustaf
On 2014-10-21 10:27, Tim Keitt wrote:
On Mon, Oct 20, 2014 at 2:17 PM, Gustaf Granath
<gustaf.granath at gmail.com <mailto:gustaf.granath at gmail.com>> wrote:
Hi
Im trying to use some C++ functions in R. However, these functions
are build around unsigned char* variables (1D array). I have for
example a large matrix in R that represents black (e.g. 1-bit
image, 0 and 1s). I cant find a solution to convert a character
vector into unsigned char* within Rcpp. Im new to Rcpp and C++ so
I might have missed something obvious here. Please direct me to
resources on this topic (google did not help much).
A start:
SEXP test(SEXP cM){
CharacterVector Vc(cM);
int n = Vr.size();
RawVector x(n); //is this the same as unsigned char* ?
//I tried to fill it with char*(Mr)
but I didnt succeed
}
I tend to go through the standard library as its less of a black box.
Try creating a std::string and appending your chars to that string.
Then call obj.c_str() to get the C-style array of chars. Or push the
chars onto a std::vector and do something like &obj[0] to get a 1-D array.
THK
Thanks,
Gustaf
--
Gustaf Granath (PhD)
Post doc
McMaster University
School of Geography & Earth Sciences
On 21 October 2014 at 16:05, Gustaf Granath wrote:
| Thanks for the replies. After some testing it seems like I dont need the
| unsigned stuff and the functions seems to work fine with just a regular int
| variable.
Sounds good.
| The functions I wanted to use were just small parts of a program and
| in that program bitmap images were loaded and image data stored as a unsigned
| char* (for bitmaps unsigned char are often used due to the 0-255 property I
| believe). I thought the simpliest solution was to stick to the original set up
| (so replicating the variables) but it seems that is not necessary which make
| things easier.
|
| However, I got curious how to handle this if I need it.
|
| First, I still dont get the difference between R raw and unsigned char (if
| there is one). E.g.?
| this:
| unsigned char a = 'b';
| cout << hex << a << endl;
| equals
| > charToRaw("b")
| [1] 62
| So can unsigned char be used within Rcpp with RawVector?
Yes, what you get from something like charToRaw() should work seamlessly in
RawVector. [ But in all those years with R, I have found very little use for
'raw' outside of serialization. ]
| For example, is it possible to go from int/char to raw using Rcpp? Or is the
Rcpp is a priori for mapping R types to the closest corresponding C++ types.
So R 'raw' to Rcpp 'RawVector'. The conversion from raw to unsigned char is
not something R does so Rcpp does not immediately spring to mind either.
R has fewer basic types than C++ as the signed/unsigned duality does not
exist, and for numeric types R only knows one precision. So Rcpp can't
really translate between all combinations.
| recommended way to use unsigned char to go outside Rcpp as Tim suggested?
You can do that. Once you have a C++ object (and know its storage type), you
can operate on it via standard casts.
That said, Knuth's dictum about premature optmisation comes to mind. I'd try
to get it working first, and only then worry about efficiency.
Dirk
?
| Again, thanks for the input,
|
| Gustaf
|
|
| On 2014-10-21 10:27, Tim Keitt wrote:
|
|
|
| On Mon, Oct 20, 2014 at 2:17 PM, Gustaf Granath <gustaf.granath at gmail.com>
| wrote:
|
| Hi
| Im trying to use some C++ functions in R. However, these functions are
| build around unsigned char* variables (1D array). I have for example a
| large matrix in R that represents black (e.g. 1-bit image, 0 and 1s). I
| cant find a solution to convert a character vector into unsigned char*
| within Rcpp. Im new to Rcpp and C++ so I might have missed something
| obvious here. Please direct me to resources on this topic (google did
| not help much).
|
| A start:
|
| SEXP test(SEXP cM){
| ? CharacterVector Vc(cM);
| ? int n = Vr.size();
| ? RawVector x(n); //is this the same as unsigned char* ?
| ? ? ? ? ? ? ? ? ? ? ? ? ? ? //I tried to fill it with
| char*(Mr) but I didnt succeed
| }
|
|
| I tend to go through the standard library as its less of a black box. Try
| creating a std::string and appending your chars to that string. Then call
| obj.c_str() to get the C-style array of chars. Or push the chars onto a
| std::vector and do something like &obj[0] to get a 1-D array.
|
| THK
| ?
|
|
| Thanks,
|
| Gustaf
|
| --
| Gustaf Granath (PhD)
| Post doc
| McMaster University
| School of Geography & Earth Sciences
|
| _______________________________________________
| 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
|
|
|
|
| --
| http://www.keittlab.org/
|
|
| --
| Gustaf Granath (PhD)
| Post doc
| McMaster University
| School of Geography & Earth Sciences
|