Skip to content

Commit a00a60a

Browse files
committed
copy complex numbers/vectors/matrices from R to C++ and viceversa - replaces #427
1 parent b5e5e34 commit a00a60a

File tree

5 files changed

+29
-91
lines changed

5 files changed

+29
-91
lines changed

cpp11test/man/cpp11test-package.Rd

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/cpp_source.Rd

Lines changed: 3 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vignettes/converting.Rmd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ as_tibble(x, ".rows"_nm = num_rows, ".name_repair"_nm = name_repair);
119119
- Some parts of [Attributes](https://CRAN.R-project.org/package=Rcpp/vignettes/Rcpp-attributes.pdf)
120120
- No dependencies
121121
- No random number generator restoration
122+
- No support for roxygen2 comments
122123
- No interfaces
123124
124125
### RNGs

vignettes/cpp11.Rmd

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -349,35 +349,6 @@ For the remainder of this vignette C++ code will be presented stand-alone rather
349349
If you want to try compiling and/or modifying the examples you should paste them into a C++ source file that includes the elements described above.
350350
This is easy to do in RMarkdown by using `{cpp11}` instead of `{r}` at the beginning of your code blocks.
351351

352-
## Roxygen support
353-
354-
It is possible to use `roxygen2` to document your C++ functions. Here is an
355-
example of how to do this:
356-
357-
```{cpp11, eval = FALSE}
358-
/* roxygen start
359-
@title Mean of a numeric vector
360-
@param x A numeric vector
361-
@return The mean of the input vector
362-
@examples mean_cpp(1:10)
363-
@export
364-
roxygen end */
365-
[[cpp11::register]]
366-
double mean_roxygenised_cpp(doubles x) {
367-
int n = x.size();
368-
double total = 0;
369-
for(double value : x) {
370-
total += value;
371-
}
372-
return total / n;
373-
}
374-
```
375-
376-
Unlike R scripts, you need to use `/* roxygen start` and `roxygen end */` to
377-
delimit the roxygen comments. The logic behind this is that C++ compilers
378-
understand `/*` and `*/` as multi-linecomments, and therefore it is not required
379-
to prepend `#' ` to each line of the roxygen comments as in R scripts.
380-
381352
### Exercises
382353

383354
1. With the basics of C++ in hand, it's now a great time to practice by reading and writing some simple C++ functions. For each of the following functions, read the code and figure out what the corresponding base R function is. You might not understand every part of the code yet, but you should be able to figure out the basics of what the function does.
@@ -907,6 +878,8 @@ logicals duplicated_cpp(integers x) {
907878
}
908879
```
909880

881+
````{=html}
882+
<!--- TODO: Add `as_sexp()` support for maps
910883
### Map
911884
912885
A map is similar to a set, but instead of storing presence or absence, it can store additional data.
@@ -930,6 +903,8 @@ SEXP table_cpp(doubles x) {
930903
return as_sexp(counts);
931904
}
932905
```
906+
!-->
907+
````
933908

934909
### Exercises
935910

vignettes/motivations.Rmd

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -332,52 +332,7 @@ Doing this universally avoids many locale specific issues when dealing with Unic
332332

333333
Concretely cpp11 always uses `Rf_translateCharUTF8()` when obtaining `const char*` from `CHRSXP` objects and uses `Rf_mkCharCE(, CE_UTF8)` when creating new `CHRSXP` objects from `const char*` inputs.
334334

335-
Converting R Unicode Strings to C++ Strings:
336-
337-
```cpp
338-
#include <cpp11.hpp>
339-
#include <string>
340-
341-
[[cpp11::register]]
342-
std::string convert_to_utf8(cpp11::strings input) {
343-
std::string result;
344-
for (auto str : input) {
345-
result += cpp11::r_string(Rf_translateCharUTF8(str));
346-
}
347-
return result;
348-
}
349-
```
350-
351-
```r
352-
# hello + how are you? in Japanese and Spanish
353-
input <- c("\xe3\x81\x93\xe3\x82\x93\xe3\x81\xab\xe3\x81\xa1\xe3\x81\xaf",
354-
"\xc2\xbfC\xc3\xb3mo est\xc3\xa1s\x3f")
355-
356-
convert_to_utf8(input)
357-
358-
[1] "こんにちは¿Cómo estás?"
359-
```
360-
361-
Returning Unicode Strings from C++ to R:
362-
363-
```cpp
364-
#include <cpp11.hpp>
365-
#include <string>
366-
367-
[[cpp11::register]]
368-
cpp11::writable::strings return_utf8_string() {
369-
cpp11::writable::strings result;
370-
std::string utf8_str = "こんにちは"; // Hello in Japanese
371-
result.push_back(Rf_mkCharCE(utf8_str.c_str(), CE_UTF8));
372-
return result;
373-
}
374-
```
375-
376-
```r
377-
return_utf8_string()
378-
379-
[1] "こんにちは"
380-
```
335+
<!--TODO: unicode examples?-->
381336

382337
## C++11 features {#c11-features}
383338

0 commit comments

Comments
 (0)