Skip to content

Commit fceab1c

Browse files
committed
add cran install instructions + some flexibility for lists
1 parent ac1c493 commit fceab1c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+823
-2951
lines changed

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: cpp4r
22
Title: Header-Only 'C++' and 'R' Interface
3-
Version: 0.3.0
3+
Version: 0.3.1
44
Authors@R: c(
55
person("Mauricio", "Vargas Sepulveda", , "[email protected]", role = c("aut", "cre"),
66
comment = c(ORCID = "0000-0003-1017-7574")),
@@ -38,4 +38,4 @@ Config/Needs/cpp4r/register: brio, cli, decor, desc, glue, tibble, vctrs
3838
Config/testthat/edition: 3
3939
Encoding: UTF-8
4040
Roxygen: list(markdown = TRUE)
41-
RoxygenNote: 7.3.2
41+
RoxygenNote: 7.3.3

NEWS.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
# cpp4r 0.3.1
2+
3+
* Added support for implicit conversions for R lists
4+
15
# cpp4r 0.3.0
26

7+
* This is the first release on CRAN
38
* Added `as_logicals()` and `as_strings()` in the same style of `as_doubles()` and `as_integers()`
49
* Improved memory management for `r_vector` iterators
510
* Slightly faster than `cpp11`

README.Rmd

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,19 @@ Posit, it was mentioned that I should create my own fork to add the following fe
5151
Check the [documentation](https://cpp4r.org/) to get started using cpp4r in your scripts, particularly if you are new to
5252
C++ programming.
5353

54+
You can install cpp4r from CRAN:
55+
56+
```r
57+
install.packages("cpp4r", repos = "https://cloud.r-project.org")
58+
```
59+
60+
Or the development version from GitHub:
61+
62+
```r
63+
install.packages("remotes")
64+
remotes::install_github("pachadotdev/cpp4r")
65+
```
66+
5467
## Using cpp4r in a package
5568

5669
Create a new package with `cpp4r::pkg_template("~/path/to/mypkg")` and then edit the generated files.

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,19 @@ fork to add the following features:
4848
Check the [documentation](https://cpp4r.org/) to get started using cpp4r
4949
in your scripts, particularly if you are new to C++ programming.
5050

51+
You can install cpp4r from CRAN:
52+
53+
``` r
54+
install.packages("cpp4r", repos = "https://cloud.r-project.org")
55+
```
56+
57+
Or the development version from GitHub:
58+
59+
``` r
60+
install.packages("remotes")
61+
remotes::install_github("pachadotdev/cpp4r")
62+
```
63+
5164
## Using cpp4r in a package
5265

5366
Create a new package with `cpp4r::pkg_template("~/path/to/mypkg")` and

cpp4rtest/DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Suggests:
2020
xml2
2121
LazyData: true
2222
Roxygen: list(markdown = TRUE)
23-
RoxygenNote: 7.3.2
23+
RoxygenNote: 7.3.3
2424
Config/testthat/edition: 3
2525
Remotes:
2626
pachadotdev/cpp4r

cpp4rtest/R/cpp4r.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,14 @@ cpp4r_insert_ <- function(num_sxp) {
9292
.Call(`_cpp4rtest_cpp4r_insert_`, num_sxp)
9393
}
9494

95+
cpp4r_named_list_push_back_ <- function() {
96+
.Call(`_cpp4rtest_cpp4r_named_list_push_back_`)
97+
}
98+
99+
cpp4r_named_list_c_style_ <- function() {
100+
.Call(`_cpp4rtest_cpp4r_named_list_c_style_`)
101+
}
102+
95103
ordered_map_to_list_ <- function(x) {
96104
.Call(`_cpp4rtest_ordered_map_to_list_`, x)
97105
}

cpp4rtest/src/cpp4r.cpp

Lines changed: 108 additions & 92 deletions
Large diffs are not rendered by default.

cpp4rtest/src/lists.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
[[cpp4r::register]] list cpp4r_named_list_push_back_() {
2+
writable::list result;
3+
4+
double one = 1.0, two = 2.0, three = 3.0;
5+
// int four = 4;
6+
// bool five = false;
7+
// const char* six = "six";
8+
std::string seven = "seven";
9+
std::vector<int> eight = {8, 8, 8};
10+
std::vector<double> nine = {9.0, 9.0, 9.0};
11+
std::vector<std::string> ten = {"ten", "ten"};
12+
13+
result.push_back({"one"_nm = one}); // Using literal + assignment
14+
result.push_back(named_arg("two", two)); // Using named_arg constructor
15+
result.push_back({"three"_nm = three}); // Using literal + assignment
16+
result.push_back({"four"_nm = 4}); // Using literal + assignment
17+
result.push_back({"five"_nm = false}); // Using literal + assignment
18+
result.push_back({"six"_nm = "six"}); // Using literal + assignment
19+
result.push_back({"seven"_nm = std::string("seven")}); // Using literal + assignment
20+
result.push_back({"eight"_nm = std::vector<int>{8, 8, 8}}); // Using literal + assignment + std::vector
21+
result.push_back({"nine"_nm = std::vector<double>{9.0, 9.0, 9.0}}); // Using literal + assignment + std::vector
22+
result.push_back({"ten"_nm = std::vector<std::string>{"ten", "ten"}}); // Using literal + assignment + std::vector
23+
24+
return result;
25+
}
26+
27+
[[cpp4r::register]] list cpp4r_named_list_c_style_() {
28+
writable::list result(10); // Preallocate list of size 10
29+
30+
double one = 1.0, two = 2.0, three = 3.0;
31+
int four = 4;
32+
bool five = false;
33+
const char* six = "six";
34+
std::string seven = "seven";
35+
std::vector<int> eight = {8, 8, 8};
36+
std::vector<double> nine = {9.0, 9.0, 9.0};
37+
std::vector<std::string> ten = {"ten", "ten"};
38+
39+
result[0] = one;
40+
result[1] = two;
41+
result[2] = three;
42+
result[3] = four;
43+
result[4] = five;
44+
result[5] = six;
45+
result[6] = seven;
46+
result[7] = eight;
47+
result[8] = nine;
48+
result[9] = ten;
49+
50+
result.names() = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"};
51+
52+
return result;
53+
}

cpp4rtest/src/main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ using namespace cpp4r;
3434
#include "sum_int.h"
3535
#include "sum_Rcpp.h"
3636
#include "truncate.h"
37+
#include "lists.h"
3738

3839
#include "test-runner.h"
3940
#include "test-as.h"
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
test_that("creating named lists with push_back or C-style gives the same result", {
2+
res1 <- cpp4r_named_list_push_back_()
3+
res2 <- cpp4r_named_list_c_style_()
4+
5+
expect_equal(res1, res2)
6+
7+
expect_equal(res1[[1]], 1.0)
8+
expect_equal(res1[[2]], 2.0)
9+
expect_equal(res1[[3]], 3.0)
10+
expect_equal(res1[[4]], 4L)
11+
expect_equal(res1[[5]], FALSE)
12+
13+
expect_equal(res2[["six"]], "six")
14+
expect_equal(res2[["seven"]], "seven")
15+
expect_equal(res2[["eight"]], rep(8L, 3L))
16+
expect_equal(res2[["nine"]], rep(9.0, 3L))
17+
expect_equal(res2[["ten"]], rep("ten", 2L))
18+
})

0 commit comments

Comments
 (0)