├── .Rbuildignore ├── .editorconfig ├── .github └── workflows │ └── ci.yaml ├── .gitignore ├── ChangeLog ├── DESCRIPTION ├── NAMESPACE ├── R ├── RcppDataFrame.R ├── RcppDateExample.R ├── RcppExports.R ├── RcppListExample.R ├── RcppMatrixExample.R ├── RcppRNGs.R ├── RcppStringVectorExample.R └── RcppVectorExample.R ├── README.md ├── TODO ├── cleanup ├── inst └── NEWS.Rd ├── man ├── RcppDataFrame.Rd ├── RcppDateExample.Rd ├── RcppExamples-package.Rd ├── RcppListExample.Rd ├── RcppMatrixExample.Rd ├── RcppNumericVectorExample.Rd ├── RcppRNGsExample.Rd ├── RcppStringVectorExample.Rd └── factor2char.Rd └── src ├── DataFrameExample.cpp ├── DateExample.cpp ├── FactorExample.cpp ├── ListExample.cpp ├── Makevars ├── Makevars.win ├── MatrixExample.cpp ├── NumericVectorExample.cpp ├── RNGs.cpp ├── RcppExports.cpp └── StringVectorExample.cpp /.Rbuildignore: -------------------------------------------------------------------------------- 1 | .gitignore 2 | .git 3 | .RS_Store 4 | ^\.travis\.yml$ 5 | ^.*\.Rproj$ 6 | ^\.Rproj\.user$ 7 | ^.*\.tar\.gz$ 8 | ^\.github 9 | \.editorconfig$ 10 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # EditorConfig is awesome: http://EditorConfig.org 2 | 3 | # top-most EditorConfig file 4 | root = true 5 | 6 | # Unix-style newlines with a newline ending every file 7 | [*] 8 | end_of_line = lf 9 | insert_final_newline = true 10 | trim_trailing_whitespace = true 11 | 12 | # Matches multiple files with brace expansion notation 13 | # 4 space indentation 14 | [*.{c,cpp,h,hpp,R,r}] 15 | indent_style = space 16 | indent_size = 4 17 | 18 | # Tab indentation (no size specified) 19 | [Makefile] 20 | indent_style = tab 21 | 22 | -------------------------------------------------------------------------------- /.github/workflows/ci.yaml: -------------------------------------------------------------------------------- 1 | # Run CI for R using https://eddelbuettel.github.io/r-ci/ 2 | 3 | name: ci 4 | 5 | on: 6 | push: 7 | pull_request: 8 | 9 | env: 10 | _R_CHECK_FORCE_SUGGESTS_: "false" 11 | 12 | jobs: 13 | ci: 14 | strategy: 15 | matrix: 16 | include: 17 | #- {os: macOS-latest} 18 | - {os: ubuntu-latest} 19 | 20 | runs-on: ${{ matrix.os }} 21 | 22 | steps: 23 | - name: Checkout 24 | uses: actions/checkout@v4 25 | 26 | - name: Setup 27 | uses: eddelbuettel/github-actions/r-ci@master 28 | 29 | - name: Dependencies 30 | run: ./run.sh install_deps 31 | 32 | - name: Test 33 | run: ./run.sh run_tests 34 | 35 | #- name: Coverage 36 | # if: ${{ matrix.os == 'ubuntu-latest' }} 37 | # run: ./run.sh coverage 38 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .Rproj.user 2 | .Rhistory 3 | .RData 4 | .Ruserdata 5 | src/*.o 6 | src/*.so 7 | src/*.dll 8 | -------------------------------------------------------------------------------- /ChangeLog: -------------------------------------------------------------------------------- 1 | 2025-03-17 Dirk Eddelbuettel 2 | 3 | * DESCRIPTION (Version, Date): Release 0.1.10 4 | 5 | 2025-02-20 Dirk Eddelbuettel 6 | 7 | * DESCRIPTION (Version, Date): Roll micro version and date 8 | 9 | * .github/workflows/ci.yaml (jobs): Use r-ci with included bootstrap 10 | 11 | * DESCRIPTION (Depends): Remove ancient versioned Depends on R 12 | greater or equal than version 2.15.1 -- dated June 2012 13 | 14 | 2024-09-13 Dirk Eddelbuettel 15 | 16 | * DESCRIPTION (Authors@R): Added 17 | 18 | 2024-05-22 Dirk Eddelbuettel 19 | 20 | * DESCRIPTION (Version, Date): Roll minor version 21 | 22 | * src/FactorExample.cpp: Add 'cstint' header needed on Windows 23 | 24 | 2024-05-21 Dirk Eddelbuettel 25 | 26 | * README.md: Use tinyverse.netlify.app for dependency badge 27 | 28 | 2024-03-04 Dirk Eddelbuettel 29 | 30 | * .github/workflows/ci.yaml (jobs): Update to actions/checkout@v4, 31 | add r-ci-setup actions 32 | 33 | 2023-04-08 Dirk Eddelbuettel 34 | 35 | * src/Makevars: No longer set a compilation standard, small edits 36 | * src/Makevars.win: Ditto 37 | 38 | 2023-03-28 Dirk Eddelbuettel 39 | 40 | * src/FactorExample.cpp: Renamed 41 | 42 | 2023-03-27 Dirk Eddelbuettel 43 | 44 | * src/makeFactor.cpp (factor2char, char2factor): Add two simple to 45 | and from factor converters 46 | * man/factor2char.Rd: Documentation 47 | * src/RcppExports.cpp: Updated 48 | * R/RcppExports.R: Updated 49 | * NAMESPACE: Idem 50 | 51 | * .editorconfig: Added 52 | 53 | 2022-11-14 Dirk Eddelbuettel 54 | 55 | * .github/workflows/ci.yaml (jobs): Update to actions/checkout@v3 56 | 57 | 2022-07-24 Dirk Eddelbuettel 58 | 59 | * DESCRIPTION (Version, Date): Roll minor version 60 | 61 | * README.md: Use https:// URLs, point to www.rcpp.org 62 | * man/RcppExamples-package.Rd: Use https:// URL 63 | * DESCRIPTION: Idem 64 | 65 | 2021-12-10 Dirk Eddelbuettel 66 | 67 | * README.md: Remove unused continuous integration artifact and badge 68 | 69 | 2021-05-09 Dirk Eddelbuettel 70 | 71 | * DESCRIPTION (URL): Add GitHub repo to URL field 72 | 73 | 2021-01-10 Dirk Eddelbuettel 74 | 75 | * .github/workflows/ci.yaml: Add CI runner using r-ci 76 | * README.md: Add new badge 77 | 78 | 2020-08-09 Dirk Eddelbuettel 79 | 80 | * DESCRIPTION (Version, Date): Roll minor version 81 | 82 | 2020-08-06 Dirk Eddelbuettel 83 | 84 | * src/DateExample.cpp (DateExample): Focus on new Date vector 85 | 86 | 2020-07-31 Dirk Eddelbuettel 87 | 88 | * README.md: Add a 'last commit' badge 89 | 90 | * .travis.yml: Update to 'bionic' and R 4.0.* 91 | 92 | 2019-08-24 Dirk Eddelbuettel 93 | 94 | * DESCRIPTION (Version, Date): Release 0.1.9 95 | 96 | 2019-08-22 Dirk Eddelbuettel 97 | 98 | * DESCRIPTION (Version, Date): Roll minor version 99 | 100 | * DESCRIPTION (Suggests): Remove RUnit as unused 101 | * .travis.yml: Ditto 102 | 103 | 2019-08-21 Dirk Eddelbuettel 104 | 105 | * README.md: Added dependencies badge 106 | 107 | 2018-09-22 Dirk Eddelbuettel 108 | 109 | * DESCRIPTION (Version, Date): New minor release 110 | 111 | * src/StringVectorExample.cpp (StringVectorExample): Updated with a 112 | local tolower<>() to not run afoul of the 'C++17 noexcept' warning 113 | 114 | * src/Makevars: No longer need -DRCPP_NEW_DATE_DATETIME_VECTORS 115 | * src/Makevars.win: Ditto 116 | 117 | * NAMESPACE: Add .registration=TRUE to useDynLib call 118 | 119 | * src/RcppExports.cpp: Regenerated (with registrations) 120 | * R/RcppExports.R: Also regenerated 121 | 122 | 2018-09-22 ChrisMuir 123 | 124 | * man/RcppStringVectorExample.Rd: Add missing parenthesis 125 | 126 | 2017-08-26 Dirk Eddelbuettel 127 | 128 | * .travis.yml (before_install): Use https for curl fetch 129 | 130 | 2017-01-02 Dirk Eddelbuettel 131 | 132 | * R/RcppDataFrame.R: (RcppDataFrame): Print but return invisibly 133 | 134 | 2017-01-02 Xikun Han <1159225156@qq.com> 135 | 136 | * R/RcppDataFrame.R: (RcppDataFrame): Avoid double printing of return 137 | 138 | 2016-12-02 Dirk Eddelbuettel 139 | 140 | * src/DateExample.cpp (DateExample): Examples take more advantage 141 | of 'new' Rcpp Date(time) classe; conditional code for old version 142 | 143 | 2016-11-25 Dirk Eddelbuettel 144 | 145 | * DESCRIPTION (URL): Correct package URL 146 | 147 | 2016-11-24 Dirk Eddelbuettel 148 | 149 | * DESCRIPTION (Version, Date): Release 0.1.8 150 | 151 | * src/DateExample.cpp (DateExample): Rewritten taking advantage of 152 | Rcpp 0.12.8 features and 'new' date(time) vectors 153 | 154 | * src/Makevars (PKG_CXXFLAGS): Turning of new datetime vectors via 155 | #define while they are still being phased in by Rcpp 156 | * src/Makevars.win: Ditto 157 | 158 | * R/RcppDateExample.R (RcppDateExample): Setting and resetting 159 | digits.secs option 160 | 161 | * README.md: Use canonical URLs 162 | 163 | * .travis.yml: Switch to using run.sh for Travis CI 164 | 165 | 2016-01-30 Dirk Eddelbuettel 166 | 167 | * DESCRIPTION (BugReports): Added BugReports: URL 168 | 169 | 2016-01-23 Dirk Eddelbuettel 170 | 171 | * DESCRIPTION (Version): New version 0.1.7 172 | 173 | * DESCRIPTION: Refreshed with minor edits 174 | 175 | * DESCRIPTION: Import: rather than Depends: on Rcpp 176 | * NAMESPACE: Idem 177 | 178 | * README.md: Added a short README 179 | 180 | * src/*: Edited and updated adding 'const &' in several interfaces 181 | * R/*: Edited and Updated 182 | * man/*: Edited and Updated with a few minor corrections 183 | 184 | 2016-01-22 Dirk Eddelbuettel 185 | 186 | * .travis.yml: Added to support Travis CI 187 | * .Rbuildignore: Added 188 | 189 | 2013-09-17 Romain Francois 190 | 191 | * R/*: Updated to reflect newer Rcpp features 192 | * src/*: Idem 193 | * man/*: Corresponding documentation 194 | 195 | 2013-01-15 Dirk Eddelbuettel 196 | 197 | * DESCRIPTION (Version): New version 0.1.6 198 | 199 | * DESCRIPTION (Description): Mention http://gallery.rcpp.org 200 | * man/RcppExamples-package.Rd: Idem 201 | 202 | * inst/NEWS.Rd: Moved from directory above per CRAN request 203 | 204 | 2012-12-27 Dirk Eddelbuettel 205 | 206 | * DESCRIPTION (Version): New version 0.1.5 207 | 208 | * DESCRIPTION (Depends, Imports): No longer use RcppClassic 209 | * NAMESPACE: Updated accordingly 210 | 211 | * R/RcppDateExample.R (RcppDateExample): No longer call 'classic' 212 | example moved to new package RcppClassicExamples 213 | * R/RcppMatrixExample.R (RcppMatrixExample): Idem 214 | * R/RcppParamsExample.R (RcppParamsExample): Idem 215 | * R/RcppStringVectorExample.R (RcppStringVectorExample): Idem 216 | * R/RcppVectorExample.R (RcppVectorExample): Idem 217 | 218 | * man/RcppExamples-package.Rd: Update Description: 219 | 220 | * src/Makevars (PKG_LIBS): Remove RcppClassic use 221 | * src/Makevars.win (PKG_LIBS): Idem 222 | 223 | * src/newRcppMatrixExample.cpp: Corrected header use 224 | * src/newRcppVectorExample.cpp: Idem 225 | 226 | 2012-08-09 Dirk Eddelbuettel 227 | 228 | * DESCRIPTION: Release 0.1.4 229 | 230 | * src/RcppRNGs.cpp: New example of generating RNG draws 231 | * R/RcppRNGs.R: New R function to call new example 232 | * man/RcppRNGs.Rd: New manual page for new example 233 | 234 | * NEWS.Rd: Converted from NEWS 235 | 236 | * DESCRIPTION: Changed Maintainer: to single person per CRAN Policy 237 | 238 | 2011-12-28 Dirk Eddelbuettel 239 | 240 | * DESCRIPTION: Release 0.1.3 241 | 242 | * src/newRcppDateExample.cpp: switch from std::cout to the new 243 | Rcpp::Rcout device available since Rcpp 0.9.8 244 | * src/classicRcppDateExample.cpp: idem 245 | 246 | * DESCRIPTION: Depends on Rcpp (>= 0.9.9) for Rcpp::Rcout 247 | 248 | 2011-04-08 Dirk Eddelbuettel 249 | 250 | * R/RcppDataFrame.R: Added new example for Rcpp::DataFrame 251 | * src/RcppDataFrame.cpp: C++ source for new example 252 | * man/RcppDataFrame.Rd: Documentation 253 | 254 | * man/RcppParams.Rd: Small change to suppres a warning 255 | 256 | 2010-12-20 Dirk Eddelbuettel 257 | 258 | * DESCRIPTION: Release 0.1.2 259 | 260 | * src/newRcppDateExample.cpp (newRcppDateExample): New API example 261 | * src/newRcppParamsExample.cpp (newRcppParamsExample): dito 262 | 263 | * R/RcppDateExample.R (RcppDateExample): Call new API example 264 | * R/RcppParamsExample.R (RcppParamsExample): dito 265 | 266 | * src/*: Split source files into 'new*.cpp' and 'classic*.cpp' as 267 | they use a different include header file anyway 268 | 269 | 2010-12-03 Romain Francois 270 | 271 | * DESCRIPTION: depending on RcppClassic 272 | 273 | * src/*.cpp: using include 274 | 275 | * src/Makevars: link against the RcppClassic library 276 | 277 | * src/Makevars.win : link against the RcppClassic library 278 | 279 | 2010-11-19 Dirk Eddelbuettel 280 | 281 | * inst/ChangeLog: moved to top-level enabling 'C-x 4 a' lookups 282 | 283 | 2010-11-19 Romain Francois 284 | 285 | * src/Makevars: apply new recommended PKG_LIBS declaration 286 | 287 | * DESCRIPTION: removed SystemRequirements: GNU make 288 | 289 | 2010-07-29 Romain Francois 290 | 291 | * src/RcppMatrixExample.cpp: disambiguate versions of sqrt 292 | 293 | 2010-07-29 Dirk Eddelbuettel 294 | 295 | * DESCRIPTION: Release 0.1.1 296 | 297 | * TODO: Expanded noting need for additional documentation 298 | 299 | 2010-07-29 Romain Francois 300 | 301 | * TODO: Added, noting need to emphasize new API 302 | 303 | * man/*: Minor edits noting that the classic API is not recommended 304 | for new code 305 | 306 | 2010-07-28 Dirk Eddelbuettel 307 | 308 | * DESCRIPTION: Use LinkingTo: Rcpp 309 | * DESCRIPTION: Update Depends: on R and Rcpp to newer version 310 | 311 | * src/Makevars: Simplified thanks to 'LinkingTo: Rcpp' 312 | 313 | 2010-05-25 Romain Francois 314 | 315 | * src/*.cpp: use BEGIN_RCPP/END_RCPP in new api examples and use 316 | List::create instead of Pairlist 317 | 318 | * src/Makevars.win: apply Brian Ripley's advice from R-devel thread 319 | to anticipate changes in R 2.12.0 320 | 321 | 2010-03-10 Dirk Eddelbuettel 322 | 323 | * DESCRIPTION: Release 0.1.0 for the first CRAN upload 324 | 325 | * src/Makevars.win: Switch back from backticks to $(shell ...) 326 | which should be fine as GNU make is mandated on Windows anyway 327 | 328 | 2010-03-07 Romain Francois 329 | 330 | * src/RcppVectorExample.cpp: use a new vector for the output 331 | * src/RcppStringVectorExample.cpp: rework the new api version 332 | using the StringTransformer class (depends on Rcpp >= 0.7.7.15) 333 | 334 | 2010-03-04 Dirk Eddelbuettel 335 | 336 | * src/RcppMatrixExample.cpp: Added 'classic' + 'new' API examples 337 | * R/RcppMatrixExample.R: idem 338 | * src/RcppStringVectorExample.cpp: idem 339 | * R/RcppStringVectorExample.R: idem 340 | 341 | 2010-03-03 Dirk Eddelbuettel 342 | 343 | * src/RcppVectorExample.cpp: Added 'new' API example 344 | * R/RcppVectorExample.R: Added argument 'api' 345 | 346 | 2010-02-27 Dirk Eddelbuettel 347 | 348 | * src/RcppDateExample.cpp: Carved out of RcppExample.cpp 349 | * src/RcppParamsExample.cpp: idem 350 | * src/RcppVectorExample.cpp: idem 351 | * src/RcppExample.cpp: fixed indentation 352 | 353 | 2010-02-16 Dirk Eddelbuettel 354 | 355 | * New package, started from Rcpp 0.7.7 356 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: RcppExamples 2 | Title: Examples using 'Rcpp' to Interface R and C++ 3 | Version: 0.1.10 4 | Date: 2025-03-17 5 | Authors@R: c(person("Dirk", "Eddelbuettel", role = c("aut", "cre"), email = "edd@debian.org", 6 | comment = c(ORCID = "0000-0001-6419-907X")), 7 | person("Romain", "Francois", role = "aut", 8 | comment = c(ORCID = "0000-0002-2444-4226"))) 9 | Description: Examples for Seamless R and C++ integration 10 | The 'Rcpp' package contains a C++ library that facilitates the integration of 11 | R and C++ in various ways. This package provides some usage examples. 12 | Note that the documentation in this package currently does not cover all the 13 | features in the package. The site regroups a large 14 | number of examples for 'Rcpp'. 15 | Imports: Rcpp 16 | LinkingTo: Rcpp 17 | URL: https://github.com/eddelbuettel/rcppexamples, https://dirk.eddelbuettel.com/code/rcpp.examples.html 18 | BugReports: https://github.com/eddelbuettel/rcppexamples/issues 19 | License: GPL (>= 2) 20 | RoxygenNote: 6.0.1 21 | -------------------------------------------------------------------------------- /NAMESPACE: -------------------------------------------------------------------------------- 1 | useDynLib("RcppExamples", .registration=TRUE) 2 | 3 | export("RcppDataFrame", 4 | "RcppDateExample", 5 | "RcppListExample", 6 | "RcppNumericVectorExample", 7 | "RcppMatrixExample", 8 | "RcppRNGsExample", 9 | "RcppStringVectorExample", 10 | "factor2char", 11 | "char2factor") 12 | 13 | importFrom("Rcpp", "evalCpp") 14 | -------------------------------------------------------------------------------- /R/RcppDataFrame.R: -------------------------------------------------------------------------------- 1 | ## RcppDataFrame.R: DataFrame example 2 | ## 3 | ## Copyright (C) 2011 - 2017 Dirk Eddelbuettel and Romain Francois 4 | ## 5 | ## This file is part of RcppExamples. 6 | ## 7 | ## RcppExamples is free software: you can redistribute it and/or modify it 8 | ## under the terms of the GNU General Public License as published by 9 | ## the Free Software Foundation, either version 2 of the License, or 10 | ## (at your option) any later version. 11 | ## 12 | ## RcppExamples is distributed in the hope that it will be useful, but 13 | ## WITHOUT ANY WARRANTY; without even the implied warranty of 14 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | ## GNU General Public License for more details. 16 | ## 17 | ## You should have received a copy of the GNU General Public License 18 | ## along with RcppExamples. If not, see . 19 | 20 | RcppDataFrame <- function() { 21 | 22 | ## create a simple data.frame 23 | ## here we enforce strings, factors can be used too 24 | D <- data.frame(a=1:3, 25 | b=LETTERS[1:3], 26 | c=as.Date("2011-01-01")+0:2, 27 | stringsAsFactors=FALSE) 28 | cat("Original data frame before call:\n") 29 | print(D) 30 | 31 | ## Make the call... 32 | val <- DataFrameExample(D) 33 | 34 | cat("\nAfter call, original and new data frames:\n") 35 | print(val) 36 | 37 | invisible(val) 38 | } 39 | -------------------------------------------------------------------------------- /R/RcppDateExample.R: -------------------------------------------------------------------------------- 1 | 2 | ## RcppDateExample.R: RcppDate example 3 | ## 4 | ## Copyright (C) 2008 Dirk Eddelbuettel 5 | ## Copyright (C) 2009 - 2016 Dirk Eddelbuettel and Romain Francois 6 | ## 7 | ## This file is part of RcppExamples. 8 | ## 9 | ## RcppExamples is free software: you can redistribute it and/or modify it 10 | ## under the terms of the GNU General Public License as published by 11 | ## the Free Software Foundation, either version 2 of the License, or 12 | ## (at your option) any later version. 13 | ## 14 | ## RcppExamples is distributed in the hope that it will be useful, but 15 | ## WITHOUT ANY WARRANTY; without even the implied warranty of 16 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | ## GNU General Public License for more details. 18 | ## 19 | ## You should have received a copy of the GNU General Public License 20 | ## along with RcppExamples. If not, see . 21 | 22 | RcppDateExample <- function(dv, dtv) { 23 | 24 | ## Check that params is properly set. 25 | if (missing(dv)) { 26 | cat("\nIn R, setting default argument for dv\n") 27 | dv <- Sys.Date() + -2:2 28 | } 29 | 30 | if (missing(dtv)) { 31 | cat("\nIn R, setting default argument for dtv\n") 32 | dtv <- Sys.time() + (-2:2)*0.5 33 | } 34 | 35 | optdig <- getOption("digits.secs") 36 | options(digits.secs=3) 37 | 38 | ## Make the call... 39 | val <- DateExample(dv, dtv) 40 | 41 | options(digits.secs=optdig) 42 | 43 | val 44 | } 45 | 46 | -------------------------------------------------------------------------------- /R/RcppExports.R: -------------------------------------------------------------------------------- 1 | # Generated by using Rcpp::compileAttributes() -> do not edit by hand 2 | # Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393 3 | 4 | DataFrameExample <- function(DF) { 5 | .Call(`_RcppExamples_DataFrameExample`, DF) 6 | } 7 | 8 | DateExample <- function(dv, dtv) { 9 | .Call(`_RcppExamples_DateExample`, dv, dtv) 10 | } 11 | 12 | ListExamples <- function(rparam) { 13 | .Call(`_RcppExamples_ListExamples`, rparam) 14 | } 15 | 16 | MatrixExample <- function(orig) { 17 | .Call(`_RcppExamples_MatrixExample`, orig) 18 | } 19 | 20 | NumericVectorExample <- function(orig) { 21 | .Call(`_RcppExamples_NumericVectorExample`, orig) 22 | } 23 | 24 | RcppRNGs <- function(n) { 25 | .Call(`_RcppExamples_RcppRNGs`, n) 26 | } 27 | 28 | StringVectorExample <- function(orig) { 29 | .Call(`_RcppExamples_StringVectorExample`, orig) 30 | } 31 | 32 | #' Convert Index and String Vector into CharacterVector, and vice versa 33 | #' 34 | #' These two functions are an illustration of how \code{as.character} and 35 | #' \code{as.factor} may be reimplemented at the C++ level. 36 | #' @param iv A Integer Vector corresponding to numeric representation of the factor 37 | #' This vector is also expected to have an attribute \sQuote{levels} with the factor 38 | #' levels 39 | #' @return A Character Vector which at each position contains the level value of the 40 | #' corresponding index, or a Factor, depending on the function 41 | #' @examples 42 | #' f <- as.factor(c("red", "green", "blue", "red")) 43 | #' factor2char(f); 44 | factor2char <- function(iv) { 45 | .Call(`_RcppExamples_factor2char`, iv) 46 | } 47 | 48 | #' @rdname factor2char 49 | #' @param sv A String Vector 50 | #' @examples 51 | #' f <- as.factor(c("red", "green", "blue", "red")) 52 | #' v <- factor2char(f); 53 | #' char2factor(v) 54 | char2factor <- function(sv) { 55 | .Call(`_RcppExamples_char2factor`, sv) 56 | } 57 | 58 | -------------------------------------------------------------------------------- /R/RcppListExample.R: -------------------------------------------------------------------------------- 1 | 2 | ## RcppListExample.R: RcppParams example 3 | ## 4 | ## Copyright (C) 2008 Dirk Eddelbuettel 5 | ## Copyright (C) 2009 - 2016 Dirk Eddelbuettel and Romain Francois 6 | ## 7 | ## This file is part of RcppExamples. 8 | ## 9 | ## RcppExamples is free software: you can redistribute it and/or modify it 10 | ## under the terms of the GNU General Public License as published by 11 | ## the Free Software Foundation, either version 2 of the License, or 12 | ## (at your option) any later version. 13 | ## 14 | ## RcppExamples is distributed in the hope that it will be useful, but 15 | ## WITHOUT ANY WARRANTY; without even the implied warranty of 16 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | ## GNU General Public License for more details. 18 | ## 19 | ## You should have received a copy of the GNU General Public License 20 | ## along with RcppExamples. If not, see . 21 | 22 | RcppListExample <- function(params) { 23 | 24 | ## Check that params is properly set. 25 | if (missing(params)) { 26 | cat("\nIn R, setting default argument for params\n") 27 | params <- list(method='BFGS', 28 | tolerance=1.0e-8, 29 | maxIter=1000, 30 | startDate=as.Date('2006-7-15')) 31 | } 32 | 33 | ## Make the call... 34 | val <- ListExamples(params) 35 | val 36 | } 37 | 38 | -------------------------------------------------------------------------------- /R/RcppMatrixExample.R: -------------------------------------------------------------------------------- 1 | 2 | ## RcppMatrixExample.R: RcppMatrix example 3 | ## 4 | ## Copyright (C) 2008 Dirk Eddelbuettel 5 | ## Copyright (C) 2009 - 2016 Dirk Eddelbuettel and Romain Francois 6 | ## 7 | ## This file is part of RcppExamples. 8 | ## 9 | ## RcppExamples is free software: you can redistribute it and/or modify it 10 | ## under the terms of the GNU General Public License as published by 11 | ## the Free Software Foundation, either version 2 of the License, or 12 | ## (at your option) any later version. 13 | ## 14 | ## RcppExamples is distributed in the hope that it will be useful, but 15 | ## WITHOUT ANY WARRANTY; without even the implied warranty of 16 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | ## GNU General Public License for more details. 18 | ## 19 | ## You should have received a copy of the GNU General Public License 20 | ## along with RcppExamples. If not, see . 21 | 22 | RcppMatrixExample <- function(mat=matrix(seq(1,9)^2, ncol=3)) { 23 | ## Make the call... 24 | val <- MatrixExample(mat) 25 | val 26 | } 27 | -------------------------------------------------------------------------------- /R/RcppRNGs.R: -------------------------------------------------------------------------------- 1 | ## RcppRNGs.R: RNGs example 2 | ## 3 | ## Copyright (C) 2012 - 2016 Dirk Eddelbuettel and Romain Francois 4 | ## 5 | ## This file is part of RcppExamples. 6 | ## 7 | ## RcppExamples is free software: you can redistribute it and/or modify it 8 | ## under the terms of the GNU General Public License as published by 9 | ## the Free Software Foundation, either version 2 of the License, or 10 | ## (at your option) any later version. 11 | ## 12 | ## RcppExamples is distributed in the hope that it will be useful, but 13 | ## WITHOUT ANY WARRANTY; without even the implied warranty of 14 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | ## GNU General Public License for more details. 16 | ## 17 | ## You should have received a copy of the GNU General Public License 18 | ## along with RcppExamples. If not, see . 19 | 20 | RcppRNGsExample <- function(n) { 21 | df <- RcppRNGs(n) # make the call... 22 | df 23 | } 24 | 25 | -------------------------------------------------------------------------------- /R/RcppStringVectorExample.R: -------------------------------------------------------------------------------- 1 | 2 | ## RcppStringVectorExample.R: Rcpp R/C++ interface class library 3 | ## 4 | ## Copyright (C) 2008 Dirk Eddelbuettel 5 | ## Copyright (C) 2009 - 2016 Dirk Eddelbuettel and Romain Francois 6 | ## 7 | ## This file is part of RcppExamples. 8 | ## 9 | ## RcppExamples is free software: you can redistribute it and/or modify it 10 | ## under the terms of the GNU General Public License as published by 11 | ## the Free Software Foundation, either version 2 of the License, or 12 | ## (at your option) any later version. 13 | ## 14 | ## RcppExamples is distributed in the hope that it will be useful, but 15 | ## WITHOUT ANY WARRANTY; without even the implied warranty of 16 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | ## GNU General Public License for more details. 18 | ## 19 | ## You should have received a copy of the GNU General Public License 20 | ## along with RcppExamples. If not, see . 21 | 22 | RcppStringVectorExample <- function(vec=c("Tick", "Tack", "Tock")) { 23 | ## Make the call... 24 | val <- StringVectorExample(vec) 25 | val 26 | } 27 | -------------------------------------------------------------------------------- /R/RcppVectorExample.R: -------------------------------------------------------------------------------- 1 | 2 | ## RcppVectorExample.R: RcppVector example 3 | ## 4 | ## Copyright (C) 2008 Dirk Eddelbuettel 5 | ## Copyright (C) 2009 - 2016 Dirk Eddelbuettel and Romain Francois 6 | ## 7 | ## This file is part of RcppExamples. 8 | ## 9 | ## RcppExamples is free software: you can redistribute it and/or modify it 10 | ## under the terms of the GNU General Public License as published by 11 | ## the Free Software Foundation, either version 2 of the License, or 12 | ## (at your option) any later version. 13 | ## 14 | ## RcppExamples is distributed in the hope that it will be useful, but 15 | ## WITHOUT ANY WARRANTY; without even the implied warranty of 16 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | ## GNU General Public License for more details. 18 | ## 19 | ## You should have received a copy of the GNU General Public License 20 | ## along with RcppExamples. If not, see . 21 | 22 | RcppNumericVectorExample <- function(vec=seq(1,9)^2) { 23 | 24 | ## Make the call... 25 | val <- NumericVectorExample(vec) 26 | val 27 | } 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## RcppExamples: Simple Examples for Rcpp 2 | 3 | [![CI](https://github.com/eddelbuettel/rcppexamples/workflows/ci/badge.svg)](https://github.com/eddelbuettel/rcppexamples/actions?query=workflow%3Aci) 4 | [![License](https://img.shields.io/badge/license-GPL%20%28%3E=%202%29-brightgreen.svg?style=flat)](https://www.gnu.org/licenses/gpl-2.0.html) 5 | [![CRAN](https://www.r-pkg.org/badges/version/RcppExamples)](https://cran.r-project.org/package=RcppExamples) 6 | [![Dependencies](https://tinyverse.netlify.app/badge/RcppExamples)](https://cran.r-project.org/package=RcppExamples) 7 | [![Downloads](https://cranlogs.r-pkg.org/badges/RcppExamples?color=brightgreen)](https://www.r-pkg.org/pkg/RcppExamples) 8 | [![Last Commit](https://img.shields.io/github/last-commit/eddelbuettel/rcppexamples)](https://github.com/eddelbuettel/rcppexamples) 9 | 10 | ### About 11 | 12 | This package regroups a couple of very simple examples for [Rcpp](https://www.rcpp.org). 13 | 14 | A large number of more comprehensive examples are provided by the [Rcpp Gallery](https://gallery.rcpp.org). 15 | 16 | ### Installation 17 | 18 | As the package is on [CRAN](https://cran.r-project.org), we can use the common approach of 19 | 20 | ```r 21 | install.packages("RcppExamples") 22 | ``` 23 | 24 | ### Author 25 | 26 | Dirk Eddelbuettel and Romain Francois 27 | 28 | ### License 29 | 30 | GPL (>= 2) 31 | -------------------------------------------------------------------------------- /TODO: -------------------------------------------------------------------------------- 1 | 2 | o Add more documentation for currently missing new API functionality 3 | 4 | o Reorganize exisiting Rd files so that RcppExamples emphasizes the new 5 | API instead of the old Rcpp API 6 | 7 | -------------------------------------------------------------------------------- /cleanup: -------------------------------------------------------------------------------- 1 | 2 | rm -f src/*.o src/*.so */*~ *~ 3 | -------------------------------------------------------------------------------- /inst/NEWS.Rd: -------------------------------------------------------------------------------- 1 | \name{NEWS} 2 | \title{News for Package \pkg{RcppExamples}} 3 | \newcommand{\ghpr}{\href{https://github.com/eddelbuettel/rcppexamples/pull/#1}{##1}} 4 | \newcommand{\ghit}{\href{https://github.com/eddelbuettel/rcppexamples/issues/#1}{##1}} 5 | 6 | \section{Changes in RcppExamples version 0.1.10 (2025-03-17)}{ 7 | \itemize{ 8 | \item Simplified \code{DateExample} by removing unused API code 9 | \item Added a new \code{FactorExample} with conversion to and from 10 | character vectors 11 | \item Updated and modernised continuous integrations multiple times 12 | \item Updated a few documentation links 13 | \item Updated build configuration 14 | \item Updated README.md badges and URLs 15 | \item No longer need to set a C++ compilation standard 16 | } 17 | } 18 | 19 | \section{Changes in RcppExamples version 0.1.9 (2019-08-24)}{ 20 | \itemize{ 21 | \item Extended \code{DateExample} to use more new Rcpp features 22 | \item Do not print \code{DataFrame} result twice (Xikun Han in \ghpr{3}) 23 | \item Missing parenthesis added in man page (Chris Muir in \ghpr{5}) 24 | \item Rewrote \code{StringVectorExample} slightly to not run afould the 25 | \code{-Wnoexcept-type} warning for C++17-related name mangling changes 26 | \item Updated \code{NAMESPACE} and \code{RcppExports.cpp} to add registration 27 | \item Removed the no-longer-needed \code{#define} for new Datetime vectors 28 | } 29 | } 30 | 31 | \section{Changes in RcppExamples version 0.1.8 (2016-11-24)}{ 32 | \itemize{ 33 | \item Updated \code{DateExample} to show vector addition available under 34 | Rcpp 0.12.8 when the (currently still phased in and optional) new 35 | Date(time) classes are used via the define in \code{src/Makevars{,.win}}; 36 | with fallback code for older versions 37 | \item Other minor edits to \code{DESCRIPTION} and \code{README.md} 38 | } 39 | } 40 | 41 | \section{Changes in RcppExamples version 0.1.7 (2016-01-23)}{ 42 | \itemize{ 43 | \item All examples were updated to use \emph{Rcpp Attributes} 44 | and (where possible) use \code{const &} interfaces. 45 | \item Updated \code{DESCRIPTION} for current \code{R CMD check} 46 | standards 47 | \item The \CRANpkg{Rcpp} package is now imported rather than depended 48 | upon. 49 | \item Added \code{README.md} as well as \code{.travis.yml}. 50 | \item Also updated and refreshed all manual pages and R files. 51 | } 52 | } 53 | 54 | \section{Changes in RcppExamples version 0.1.6 (2013-01-15)}{ 55 | \itemize{ 56 | \item Moved \code{NEWS.Rd} from top-level directory to correct 57 | location \code{inst/} per CRAN maintainer suggestion 58 | } 59 | } 60 | 61 | \section{Changes in RcppExamples version 0.1.5 (2012-12-27)}{ 62 | \itemize{ 63 | \item Moved all examples using \CRANpkg{RcppClassic} to a new package 64 | \CRANpkg{RcppClassicExamples} 65 | \item Various minor small updates 66 | } 67 | } 68 | 69 | \section{Changes in RcppExamples version 0.1.4 (2012-08-09)}{ 70 | \itemize{ 71 | \item Added new example for Rcpp sugar and vectorised draws of RNGs 72 | \item Minor updates to reflect newer CRAN Policy 73 | } 74 | } 75 | \section{Changes in RcppExamples version 0.1.3 (2011-12-28)}{ 76 | \itemize{ 77 | \item Added new example for Rcpp::DataFrame 78 | \item Switched two examples from using std::cout (which 'Writing R 79 | Extensions' recommends agains) to the new Rcpp::Rcout device 80 | \item Minor .Rd correction, suppressing one warning 81 | } 82 | } 83 | \section{Changes in RcppExamples version 0.1.2 (2010-12-20)}{ 84 | \itemize{ 85 | \item Updated src/Makevars 86 | \item Now depends also on RcppClassic so that we can keep continue to show 87 | examples using the classic API 88 | \item Added examples for Rcpp::Date, Rcpp::Datetime and Rcpp::List 89 | } 90 | } 91 | \section{Changes in RcppExamples version 0.1.1 (2010-07-29)}{ 92 | \itemize{ 93 | \item Minor update, no new examples or documentation added yet 94 | } 95 | } 96 | \section{Changes in RcppExamples version 0.1.0 (2010-03-10)}{ 97 | \itemize{ 98 | \item Initial release as a package, carved out of Rcpp 0.7.7 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /man/RcppDataFrame.Rd: -------------------------------------------------------------------------------- 1 | \name{RcppDataFrame} 2 | \alias{RcppDataFrame} 3 | \title{Rcpp::DataFrame example for Rcpp} 4 | \description{ 5 | A \code{DataFrame} can be passed C++ and can be instantiated as a 6 | corresponding C++ object using the Rcpp API. 7 | 8 | This example shows (in the corresponding C++ code) how to access, 9 | modify and create a data frame. 10 | } 11 | \details{ 12 | Usage of \code{Rcpp::DataFrame} is fully defined in 13 | the respective header file. 14 | 15 | The C++ source file corresponding to the this function does the 16 | following: 17 | 18 | \preformatted{% 19 | // we receive a 'DF' data.frame object 20 | // and access each column by name 21 | Rcpp::IntegerVector a = DF["a"]; 22 | Rcpp::CharacterVector b = DF["b"]; 23 | Rcpp::DateVector c = DF["c"]; 24 | 25 | // do something 26 | a[2] = 42; 27 | b[1] = "foo"; 28 | c[0] = c[0] + 7; // move up a week 29 | 30 | // create a new data frame 31 | Rcpp::DataFrame NDF = 32 | Rcpp::DataFrame::create(Rcpp::Named("a")=a, 33 | Rcpp::Named("b")=b, 34 | Rcpp::Named("c")=c); 35 | 36 | // and return old and new in list 37 | return(Rcpp::List::create(Rcpp::Named("origDataFrame")=DF, 38 | Rcpp::Named("newDataFrame")=NDF)); 39 | } 40 | } 41 | \author{Dirk Eddelbuettel and Romain Francois} 42 | \examples{ 43 | \dontrun{ 44 | RcppDataFrame() 45 | } 46 | } 47 | \keyword{programming} 48 | \keyword{interface} 49 | -------------------------------------------------------------------------------- /man/RcppDateExample.Rd: -------------------------------------------------------------------------------- 1 | \name{RcppDateExample} 2 | \alias{RcppDateExample} 3 | \title{C++ classes for interfacing date and datetime R objects} 4 | \description{ 5 | Rcpp has the classes \code{Rcpp::Date}, \code{Rcpp::Datetime}, 6 | \code{Rcpp::DateVector} and \code{Rcpp::DatetimeVector}. 7 | } 8 | \details{ 9 | 10 | In the \code{C++} code for the \code{RcppDateExample.cpp} file: 11 | 12 | \preformatted{% 13 | // [[Rcpp::export]] 14 | List DateExample(DateVector & dv, DatetimeVector & dtv) { 15 | Function formatDate("format.Date"); 16 | Function formatDatetime("format.POSIXct"); 17 | 18 | Rprintf("\nIn C++, seeing the following date value\n"); 19 | for (int i=0; i(formatDate(wrap(dv[i]))) << std::endl; 21 | dv[i] = dv[i] + 7; // shift a week 22 | } 23 | Rprintf("\nIn C++, seeing the following datetime value\n"); 24 | for (int i=0; i(formatDatetime(wrap(dtv[i]))) << std::endl; 26 | dtv[i] = dtv[i] + 0.250; // shift 250 millisec 27 | } 28 | 29 | // Build result set to be returned as a list to R. 30 | return List::create(Named("date", dv), 31 | Named("datetime", dtv)); 32 | } 33 | 34 | } 35 | } 36 | \references{ 37 | \emph{Writing R Extensions}, available at \url{https://www.r-project.org}. 38 | } 39 | \author{Dominick Samperi wrote the initial versions of Rcpp (and 40 | RcppTemplate) during 2005 and 2006. Dirk Eddelbuettel made some 41 | additions, and became maintainer in 2008. Dirk Eddelbuettel and Romain 42 | Francois have been extending Rcpp since 2009. 43 | } 44 | \examples{ 45 | 46 | # set up date and datetime vectors 47 | dvec <- Sys.Date() + -2:2 48 | dtvec <- Sys.time() + (-2:2)*0.5 49 | 50 | # call the underlying C++ function 51 | result <- RcppDateExample(dvec, dtvec) 52 | 53 | # inspect returned object 54 | result 55 | } 56 | \keyword{programming} 57 | \keyword{interface} 58 | 59 | -------------------------------------------------------------------------------- /man/RcppExamples-package.Rd: -------------------------------------------------------------------------------- 1 | \name{RcppExamples-package} 2 | \alias{RcppExamples-package} 3 | \alias{RcppExamples} 4 | \docType{package} 5 | \title{ 6 | Examples for the Rcpp R/C++ Interface library 7 | } 8 | \description{ 9 | This package shows some simple examples for the use of \pkg{Rcpp}. 10 | 11 | It can also serve as a working template to create packages that use 12 | \pkg{Rcpp} to interface C++ code or libraries. 13 | } 14 | \details{ 15 | The \pkg{Rcpp} package provides a number of C++ classes that ease 16 | access to C++ from R. This comprises both passing parameters to 17 | functions, as well as returning results back from C++ to R. 18 | 19 | The \pkg{RcppExamples} package provides some simple examples for use 20 | of \pkg{Rcpp}. At this point the documentation is not complete in the 21 | sense of not covering all accessible classes. However, several basic 22 | use cases are illustrated, 23 | } 24 | \author{Dominick Samperi wrote the initial versions of Rcpp (and 25 | RcppTemplate) during 2005 and 2006. Dirk Eddelbuettel made some 26 | additions, and became maintainer in 2008. Dirk Eddelbuettel and Romain 27 | Francois have been extending Rcpp since 2009. 28 | } 29 | \seealso{ 30 | The \url{https://gallery.rcpp.org} site regroups a number of examples. 31 | } 32 | \keyword{package} 33 | 34 | -------------------------------------------------------------------------------- /man/RcppListExample.Rd: -------------------------------------------------------------------------------- 1 | \name{RcppListExample} 2 | \alias{RcppListExample} 3 | \title{Examples of uses of List} 4 | \description{ 5 | \code{List} is an \code{Rcpp} class that can be used to manipulate R lists. 6 | } 7 | \arguments{ 8 | \item{params}{A heterogeneous list specifying \code{method} (string), 9 | \code{tolerance} (double), \code{maxIter} (int) and \code{startDate} 10 | (Date in R, RcppDate in C++).} 11 | } 12 | \value{ 13 | \code{RcppListExample} returns a list containing: 14 | \item{method}{string input paramter} 15 | \item{tolerance}{double input paramter} 16 | \item{maxIter}{int input parameter} 17 | \item{startDate}{Date type with starting date} 18 | \item{params}{input parameter list (this is redundant because we 19 | returned the input parameters above)} 20 | } 21 | \references{ 22 | \emph{Writing R Extensions}, available at \url{https://www.r-project.org}. 23 | } 24 | \author{Dominick Samperi wrote the initial versions of Rcpp (and 25 | RcppTemplate) during 2005 and 2006. Dirk Eddelbuettel made some 26 | additions, and became maintainer in 2008. Dirk Eddelbuettel and Romain 27 | Francois have been extending Rcpp since 2009. 28 | } 29 | \examples{ 30 | 31 | # set up some value 32 | params <- list(method='BFGS', 33 | tolerance=1.0e-5, 34 | maxIter=100, 35 | startDate=as.Date('2006-7-15')) 36 | 37 | # call the underlying C++ function 38 | result <- RcppListExample(params) 39 | 40 | # inspect returned object 41 | result 42 | 43 | } 44 | \keyword{programming} 45 | \keyword{interface} 46 | -------------------------------------------------------------------------------- /man/RcppMatrixExample.Rd: -------------------------------------------------------------------------------- 1 | \name{RcppMatrixExample} 2 | \alias{RcppMatrixExample} 3 | \title{Example of using Rcpp NumericMatrix} 4 | \description{ 5 | The \code{NumericMatrix} class represents numeric matrices 6 | } 7 | \details{ 8 | 9 | The \code{C++} code presented in the \code{MatrixExample.cpp} file: 10 | 11 | \preformatted{% 12 | #include 13 | #include 14 | 15 | // suncc needs help to disambiguate between sqrt( float ) and sqrt(double) 16 | inline static double sqrt_double(double x) { return ::sqrt(x); } 17 | 18 | using namespace Rcpp; 19 | 20 | // [[Rcpp::export]] 21 | List MatrixExample(const NumericMatrix & orig) { 22 | NumericMatrix mat(orig.nrow(), orig.ncol()); 23 | 24 | // we could query size via 25 | // int n = mat.nrow(), k=mat.ncol(); 26 | // and loop over the elements, but using the STL is so much nicer 27 | // so we use a STL transform() algorithm on each element 28 | std::transform(orig.begin(), orig.end(), mat.begin(), sqrt_double ); 29 | 30 | return List::create(Named("result") = mat, 31 | Named("original") = orig); 32 | } 33 | 34 | } 35 | } 36 | \references{ 37 | \emph{Writing R Extensions}, available at \url{https://www.r-project.org}. 38 | } 39 | \author{Dominick Samperi wrote the initial versions of Rcpp (and 40 | RcppTemplate) during 2005 and 2006. Dirk Eddelbuettel made some 41 | additions, and became maintainer in 2008. Dirk Eddelbuettel and Romain 42 | Francois have been extending Rcpp since 2009. 43 | } 44 | \examples{ 45 | 46 | M <- matrix((1:16)^2, 4) 47 | RcppMatrixExample(M) 48 | 49 | } 50 | \keyword{programming} 51 | \keyword{interface} 52 | 53 | -------------------------------------------------------------------------------- /man/RcppNumericVectorExample.Rd: -------------------------------------------------------------------------------- 1 | \name{RcppNumericVectorExample} 2 | \alias{RcppNumericVectorExample} 3 | \title{Rcpp NumericVector example} 4 | \description{ 5 | Example on how to use a NumericVector and manipulate it with the STL. 6 | } 7 | \details{ 8 | \preformatted{% 9 | NumericVector orig ; // from R 10 | NumericVector vec(orig.size()); // create a target vector of the same size 11 | 12 | // we could query size via 13 | // int n = vec.size(); 14 | // and loop over the vector, but using the STL is so much nicer 15 | // so we use a STL transform() algorithm on each element 16 | std::transform(orig.begin(), orig.end(), vec.begin(), sqrt_double ); 17 | 18 | return List::create(Named("result") = vec, 19 | Named("original") = orig); 20 | } 21 | 22 | As shown in the example section, provided the seed is reset, the exact 23 | same draws can be obtained in R itself -- which is important for reproducibility. 24 | } 25 | \author{Dirk Eddelbuettel and Romain Francois} 26 | \examples{ 27 | RcppNumericVectorExample(seq(1,9)^2) 28 | } 29 | \keyword{programming} 30 | \keyword{interface} 31 | -------------------------------------------------------------------------------- /man/RcppRNGsExample.Rd: -------------------------------------------------------------------------------- 1 | \name{RcppRNGsExample} 2 | \alias{RcppRNGsExample} 3 | \title{Rcpp RNGs example} 4 | \description{ 5 | Rcpp sugar provides numerous p/q/d/r functions for numerous distributions. 6 | 7 | This example shows (in the corresponding C++ code) how to draw from 8 | three different distributions and returns a data frame. 9 | } 10 | \details{ 11 | The various header file, and the Rcpp sugar vignette, provide full 12 | documentation for Rcpp sugar. 13 | 14 | The C++ source file corresponding to the this function does the 15 | following: 16 | 17 | \preformatted{% 18 | int n; // length passed in from R 19 | 20 | NumericVector rn = rnorm(n); 21 | NumericVector rt = rt(n, 1.0); 22 | NumericVector rp = rpois(n, 1.0); 23 | 24 | // create a new data frame to return drawns 25 | return DataFrame::create(Named("rnorm") = rn, 26 | Named("rt") = rt, 27 | Named("rpois") = rp); 28 | } 29 | 30 | As shown in the example section, provided the seed is reset, the exact 31 | same draws can be obtained in R itself -- which is important for reproducibility. 32 | } 33 | \author{Dirk Eddelbuettel and Romain Francois} 34 | \examples{ 35 | set.seed(42) 36 | X <- RcppRNGsExample(10L) 37 | set.seed(42) 38 | Y <- data.frame(rnorm=rnorm(10),rt=rt(10,1),rpois=rpois(10,1)) 39 | all.equal(X,Y) 40 | } 41 | \keyword{programming} 42 | \keyword{interface} 43 | -------------------------------------------------------------------------------- /man/RcppStringVectorExample.Rd: -------------------------------------------------------------------------------- 1 | \name{RcppStringVectorExample} 2 | \alias{RcppStringVectorExample} 3 | \title{Example of using Rcpp StringVector (aka CharacterVector) } 4 | \description{ 5 | The \code{StringVector} (aka \code{CharacterVector}) 6 | class represents character vectors. 7 | } 8 | \details{ 9 | 10 | The \code{C++} code presented in the \code{StringVectorExample.cpp} file: 11 | 12 | \preformatted{% 13 | #include 14 | using namespace Rcpp ; 15 | 16 | // [[Rcpp::export]] 17 | List StringVectorExample(const StringVector & orig) { 18 | StringVector vec(orig.size()); 19 | 20 | std::transform(orig.begin(), orig.end(), vec.begin(), 21 | make_string_transformer(tolower)); 22 | 23 | return List::create(Named("result") = vec, 24 | Named("original") = orig); 25 | } 26 | 27 | } 28 | } 29 | \references{ 30 | \emph{Writing R Extensions}, available at \url{https://www.r-project.org}. 31 | } 32 | \author{Dominick Samperi wrote the initial versions of Rcpp (and 33 | RcppTemplate) during 2005 and 2006. Dirk Eddelbuettel made some 34 | additions, and became maintainer in 2008. Dirk Eddelbuettel and Romain 35 | Francois have been extending Rcpp since 2009. 36 | } 37 | \examples{ 38 | RcppStringVectorExample(c("Tick", "Tack", "Tock")) 39 | } 40 | \keyword{programming} 41 | \keyword{interface} 42 | -------------------------------------------------------------------------------- /man/factor2char.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/RcppExports.R 3 | \name{factor2char} 4 | \alias{factor2char} 5 | \alias{char2factor} 6 | \title{Convert Index and String Vector into CharacterVector, and vice versa} 7 | \usage{ 8 | factor2char(iv) 9 | 10 | char2factor(sv) 11 | } 12 | \arguments{ 13 | \item{iv}{A Integer Vector corresponding to numeric representation of the factor 14 | This vector is also expected to have an attribute \sQuote{levels} with the factor 15 | levels} 16 | 17 | \item{sv}{A String Vector} 18 | } 19 | \value{ 20 | A Character Vector which at each position contains the level value of the 21 | corresponding index, or a Factor, depending on the function 22 | } 23 | \description{ 24 | These two functions are an illustration of how \code{as.character} and 25 | \code{as.factor} may be reimplemented at the C++ level. 26 | } 27 | \examples{ 28 | f <- as.factor(c("red", "green", "blue", "red")) 29 | factor2char(f); 30 | f <- as.factor(c("red", "green", "blue", "red")) 31 | v <- factor2char(f); 32 | char2factor(v) 33 | } 34 | -------------------------------------------------------------------------------- /src/DataFrameExample.cpp: -------------------------------------------------------------------------------- 1 | // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*- 2 | // 3 | // DataFrame.cpp: data frame example 4 | // 5 | // Copyright (C) 2011 - 2016 Dirk Eddelbuettel and Romain Francois 6 | // 7 | // This file is part of RcppExamples. 8 | // 9 | // RcppExamples is free software: you can redistribute it and/or modify it 10 | // under the terms of the GNU General Public License as published by 11 | // the Free Software Foundation, either version 2 of the License, or 12 | // (at your option) any later version. 13 | // 14 | // RcppExamples is distributed in the hope that it will be useful, but 15 | // WITHOUT ANY WARRANTY; without even the implied warranty of 16 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | // GNU General Public License for more details. 18 | // 19 | // You should have received a copy of the GNU General Public License 20 | // along with RcppExamples. If not, see . 21 | 22 | #include 23 | using namespace Rcpp; 24 | 25 | // [[Rcpp::export]] 26 | List DataFrameExample(const DataFrame & DF) { 27 | 28 | // access each column by name 29 | IntegerVector a = DF["a"]; 30 | CharacterVector b = DF["b"]; 31 | DateVector c = DF["c"]; 32 | 33 | // do something 34 | a[2] = 42; 35 | b[1] = "foo"; 36 | c[0] = c[0] + 7; // move up a week 37 | 38 | // create a new data frame 39 | DataFrame NDF = DataFrame::create(Named("a")=a, 40 | Named("b")=b, 41 | Named("c")=c); 42 | 43 | // and return old and new in list 44 | return List::create(Named("origDataFrame") = DF, 45 | Named("newDataFrame") = NDF); 46 | } 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /src/DateExample.cpp: -------------------------------------------------------------------------------- 1 | // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*- 2 | // 3 | // DateExample.cpp: RcppDate example 4 | // 5 | // Copyright (C) 2009 - 2020 Dirk Eddelbuettel and Romain Francois 6 | // 7 | // This file is part of RcppExamples. 8 | // 9 | // RcppExamples is free software: you can redistribute it and/or modify it 10 | // under the terms of the GNU General Public License as published by 11 | // the Free Software Foundation, either version 2 of the License, or 12 | // (at your option) any later version. 13 | // 14 | // RcppExamples is distributed in the hope that it will be useful, but 15 | // WITHOUT ANY WARRANTY; without even the implied warranty of 16 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | // GNU General Public License for more details. 18 | // 19 | // You should have received a copy of the GNU General Public License 20 | // along with RcppExamples. If not, see . 21 | 22 | #include 23 | using namespace Rcpp; 24 | 25 | // [[Rcpp::export]] 26 | List DateExample(DateVector & dv, DatetimeVector & dtv) { 27 | // Support for this changed with Rcpp 0.12.8 but is still optional 28 | // Support for << redirection added added with 0.12.8.2 and later 29 | Rcout << "\nIn C++, seeing the following date values before/after adding a week:\n" 30 | << dv << std::endl; 31 | dv = dv + 7; // shift a week 32 | Rcout << dv << std::endl; 33 | 34 | Rcout << "\nIn C++, seeing the following datetime values before/after adding a quarter second:\n" 35 | << dtv << std::endl; 36 | dtv = dtv + 0.250; // shift 250 millisec 37 | Rcout << dtv << std::endl; 38 | 39 | // Build result set to be returned as a list to R. 40 | return List::create(Named("date", dv), 41 | Named("datetime", dtv)); 42 | } 43 | -------------------------------------------------------------------------------- /src/FactorExample.cpp: -------------------------------------------------------------------------------- 1 | 2 | // FactorExample.cpp: Factor example 3 | // 4 | // Copyright (C) 2023 - 2024 Dirk Eddelbuettel 5 | // 6 | // This file is part of RcppExamples. 7 | // 8 | // RcppExamples is free software: you can redistribute it and/or modify it 9 | // under the terms of the GNU General Public License as published by 10 | // the Free Software Foundation, either version 2 of the License, or 11 | // (at your option) any later version. 12 | // 13 | // RcppExamples is distributed in the hope that it will be useful, but 14 | // WITHOUT ANY WARRANTY; without even the implied warranty of 15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | // GNU General Public License for more details. 17 | // 18 | // You should have received a copy of the GNU General Public License 19 | // along with RcppExamples. If not, see . 20 | 21 | #include 22 | #include // for int32_t, needed on Windoze 23 | 24 | // Adapted with love from R's own src/coerce.c 25 | 26 | //' Convert Index and String Vector into CharacterVector, and vice versa 27 | //' 28 | //' These two functions are an illustration of how \code{as.character} and 29 | //' \code{as.factor} may be reimplemented at the C++ level. 30 | //' @param iv A Integer Vector corresponding to numeric representation of the factor 31 | //' This vector is also expected to have an attribute \sQuote{levels} with the factor 32 | //' levels 33 | //' @return A Character Vector which at each position contains the level value of the 34 | //' corresponding index, or a Factor, depending on the function 35 | //' @examples 36 | //' f <- as.factor(c("red", "green", "blue", "red")) 37 | //' factor2char(f); 38 | // [[Rcpp::export]] 39 | Rcpp::CharacterVector factor2char(Rcpp::IntegerVector iv) { 40 | // could add a check here but it will throw appropriately 41 | std::vector cv = iv.attr("levels"); 42 | R_xlen_t n = iv.size(); 43 | Rcpp::CharacterVector ans(n); 44 | R_xlen_t nl = cv.size(); 45 | for (R_xlen_t i=0; i= 1 && ii <= nl) 50 | ans[i] = cv[ii - 1]; 51 | else 52 | Rcpp::stop("Malformed factor"); 53 | } 54 | return ans; 55 | } 56 | 57 | //' @rdname factor2char 58 | //' @param sv A String Vector 59 | //' @examples 60 | //' f <- as.factor(c("red", "green", "blue", "red")) 61 | //' v <- factor2char(f); 62 | //' char2factor(v) 63 | // [[Rcpp::export]] 64 | Rcpp::IntegerVector char2factor(std::vector sv) { 65 | // Use a set to keep track of what we have already seen 66 | std::set ss; 67 | for (auto& s: sv) { 68 | if (ss.find(s) == ss.end()) { 69 | //Rcpp::Rcout << "Inserting " << s << std::endl; 70 | ss.insert(s); 71 | } 72 | } 73 | 74 | // Map strings to position, also fill vector for levels 75 | std::unordered_map mp; 76 | std::vector vv; 77 | int32_t pos = 0; 78 | for (auto& s: ss) { 79 | mp[s] = ++pos; 80 | //Rcpp::Rcout << "Mapping " << s << " to " << pos << std::endl; 81 | vv.push_back(s); 82 | } 83 | 84 | // Put it back into an integer vector mapping each string to its pos 85 | size_t n = sv.size(); 86 | Rcpp::IntegerVector iv(n); 87 | for (size_t i=0; i. 21 | 22 | #include 23 | using namespace Rcpp; 24 | 25 | // [[Rcpp::export]] 26 | List ListExamples(const List & rparam) { 27 | 28 | // accessing all list elements by name 29 | std::string method = as(rparam["method"]); 30 | double tolerance = as(rparam["tolerance"]); 31 | int maxIter = as(rparam["maxIter"]); 32 | Date startDate = Date(as(rparam["startDate"])); // ctor from int 33 | 34 | Rprintf("\nIn C++, seeing the following value\n"); 35 | Rprintf("Method argument : %s\n", method.c_str()); 36 | Rprintf("Tolerance argument : %f\n", tolerance); 37 | Rprintf("MaxIter argument : %d\n", maxIter); 38 | Rprintf("Start date argument: %04d-%02d-%02d\n", 39 | startDate.getYear(), startDate.getMonth(), startDate.getDay()); 40 | 41 | return List::create(Named("method", method), 42 | Named("tolerance", tolerance), 43 | Named("maxIter", maxIter), 44 | Named("startDate", startDate), 45 | Named("params", rparam)); 46 | 47 | } 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /src/Makevars: -------------------------------------------------------------------------------- 1 | ## Emacs please make this a -*- mode: Makefile; -*- 2 | ## 3 | ## We could set particular variables here. Examples are 4 | ## PKG_LIBS for external libraries 5 | ## PKG_CPPFLAGS for compilation preprocessor flags 6 | ## PKG_CXXFLAGS for additional compiler flags 7 | ## CXX_STD to select a compilation standard 8 | ## But for standard builds without external dependencies, nothing is needed 9 | -------------------------------------------------------------------------------- /src/Makevars.win: -------------------------------------------------------------------------------- 1 | ## Emacs please make this a -*- mode: Makefile; -*- 2 | ## 3 | ## We could set particular variables for Windows here. Examples are 4 | ## PKG_LIBS for external libraries 5 | ## PKG_CPPFLAGS for compilation preprocessor flags 6 | ## PKG_CXXFLAGS for additional compiler flags 7 | ## CXX_STD to select a compilation standard 8 | ## But for standard builds without external dependencies, nothing is needed 9 | -------------------------------------------------------------------------------- /src/MatrixExample.cpp: -------------------------------------------------------------------------------- 1 | // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*- 2 | // 3 | // RcppMatrixExample.cpp: RcppMatrix example 4 | // 5 | // Copyright (C) 2009 - 2016 Dirk Eddelbuettel and Romain Francois 6 | // 7 | // This file is part of RcppExamples. 8 | // 9 | // RcppExamples is free software: you can redistribute it and/or modify it 10 | // under the terms of the GNU General Public License as published by 11 | // the Free Software Foundation, either version 2 of the License, or 12 | // (at your option) any later version. 13 | // 14 | // RcppExamples is distributed in the hope that it will be useful, but 15 | // WITHOUT ANY WARRANTY; without even the implied warranty of 16 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | // GNU General Public License for more details. 18 | // 19 | // You should have received a copy of the GNU General Public License 20 | // along with RcppExamples. If not, see . 21 | 22 | #include 23 | #include 24 | 25 | // suncc needs help to disambiguate between sqrt( float ) and sqrt(double) 26 | inline static double sqrt_double(double x) { return ::sqrt(x); } 27 | 28 | using namespace Rcpp; 29 | 30 | // [[Rcpp::export]] 31 | List MatrixExample(const NumericMatrix & orig) { 32 | NumericMatrix mat(orig.nrow(), orig.ncol()); 33 | 34 | // we could query size via 35 | // int n = mat.nrow(), k=mat.ncol(); 36 | // and loop over the elements, but using the STL is so much nicer 37 | // so we use a STL transform() algorithm on each element 38 | std::transform(orig.begin(), orig.end(), mat.begin(), sqrt_double); 39 | 40 | return List::create(Named("result") = mat, 41 | Named("original") = orig); 42 | } 43 | 44 | -------------------------------------------------------------------------------- /src/NumericVectorExample.cpp: -------------------------------------------------------------------------------- 1 | // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*- 2 | // 3 | // NumericVectorExample.cpp: RcppVector example 4 | // 5 | // Copyright (C) 2005 - 2006 Dominick Samperi 6 | // Copyright (C) 2008 Dirk Eddelbuettel 7 | // Copyright (C) 2009 - 2016 Dirk Eddelbuettel and Romain Francois 8 | // 9 | // This file is part of RcppExamples. 10 | // 11 | // RcppExamples is free software: you can redistribute it and/or modify it 12 | // under the terms of the GNU General Public License as published by 13 | // the Free Software Foundation, either version 2 of the License, or 14 | // (at your option) any later version. 15 | // 16 | // RcppExamples is distributed in the hope that it will be useful, but 17 | // WITHOUT ANY WARRANTY; without even the implied warranty of 18 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 | // GNU General Public License for more details. 20 | // 21 | // You should have received a copy of the GNU General Public License 22 | // along with RcppExamples. If not, see . 23 | 24 | #include 25 | #include 26 | 27 | using namespace Rcpp; 28 | 29 | // suncc needs help to disambiguate between sqrt( float ) and sqrt(double) 30 | inline static double sqrt_double( double x ){ return ::sqrt( x ); } 31 | 32 | // [[Rcpp::export]] 33 | List NumericVectorExample(const NumericVector & orig) { 34 | NumericVector vec(orig.size()); // create a target vector of the same size 35 | 36 | // we could query size via 37 | // int n = vec.size(); 38 | // and loop over the vector, but using the STL is so much nicer 39 | // so we use a STL transform() algorithm on each element 40 | std::transform(orig.begin(), orig.end(), vec.begin(), sqrt_double); 41 | 42 | return List::create(Named("result") = vec, 43 | Named("original") = orig); 44 | } 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /src/RNGs.cpp: -------------------------------------------------------------------------------- 1 | // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*- 2 | // 3 | // RcppRNGs.cpp: RNG example 4 | // 5 | // Copyright (C) 2012 - 2016 Dirk Eddelbuettel and Romain Francois 6 | // 7 | // This file is part of RcppExamples. 8 | // 9 | // RcppExamples is free software: you can redistribute it and/or modify it 10 | // under the terms of the GNU General Public License as published by 11 | // the Free Software Foundation, either version 2 of the License, or 12 | // (at your option) any later version. 13 | // 14 | // RcppExamples is distributed in the hope that it will be useful, but 15 | // WITHOUT ANY WARRANTY; without even the implied warranty of 16 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | // GNU General Public License for more details. 18 | // 19 | // You should have received a copy of the GNU General Public License 20 | // along with RcppExamples. If not, see . 21 | 22 | #include 23 | using namespace Rcpp; 24 | 25 | // [[Rcpp::export]] 26 | DataFrame RcppRNGs(const int n) { 27 | NumericVector xn = rnorm(n); 28 | NumericVector xt = rt(n, 1.0); 29 | NumericVector xp = rpois(n, 1.0); 30 | 31 | // create a new data frame to return drawns 32 | return DataFrame::create(Named("rnorm") = xn, 33 | Named("rt") = xt, 34 | Named("rpois") = xp); 35 | } 36 | 37 | -------------------------------------------------------------------------------- /src/RcppExports.cpp: -------------------------------------------------------------------------------- 1 | // Generated by using Rcpp::compileAttributes() -> do not edit by hand 2 | // Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393 3 | 4 | #include 5 | 6 | using namespace Rcpp; 7 | 8 | #ifdef RCPP_USE_GLOBAL_ROSTREAM 9 | Rcpp::Rostream& Rcpp::Rcout = Rcpp::Rcpp_cout_get(); 10 | Rcpp::Rostream& Rcpp::Rcerr = Rcpp::Rcpp_cerr_get(); 11 | #endif 12 | 13 | // DataFrameExample 14 | List DataFrameExample(const DataFrame& DF); 15 | RcppExport SEXP _RcppExamples_DataFrameExample(SEXP DFSEXP) { 16 | BEGIN_RCPP 17 | Rcpp::RObject rcpp_result_gen; 18 | Rcpp::RNGScope rcpp_rngScope_gen; 19 | Rcpp::traits::input_parameter< const DataFrame& >::type DF(DFSEXP); 20 | rcpp_result_gen = Rcpp::wrap(DataFrameExample(DF)); 21 | return rcpp_result_gen; 22 | END_RCPP 23 | } 24 | // DateExample 25 | List DateExample(DateVector& dv, DatetimeVector& dtv); 26 | RcppExport SEXP _RcppExamples_DateExample(SEXP dvSEXP, SEXP dtvSEXP) { 27 | BEGIN_RCPP 28 | Rcpp::RObject rcpp_result_gen; 29 | Rcpp::RNGScope rcpp_rngScope_gen; 30 | Rcpp::traits::input_parameter< DateVector& >::type dv(dvSEXP); 31 | Rcpp::traits::input_parameter< DatetimeVector& >::type dtv(dtvSEXP); 32 | rcpp_result_gen = Rcpp::wrap(DateExample(dv, dtv)); 33 | return rcpp_result_gen; 34 | END_RCPP 35 | } 36 | // ListExamples 37 | List ListExamples(const List& rparam); 38 | RcppExport SEXP _RcppExamples_ListExamples(SEXP rparamSEXP) { 39 | BEGIN_RCPP 40 | Rcpp::RObject rcpp_result_gen; 41 | Rcpp::RNGScope rcpp_rngScope_gen; 42 | Rcpp::traits::input_parameter< const List& >::type rparam(rparamSEXP); 43 | rcpp_result_gen = Rcpp::wrap(ListExamples(rparam)); 44 | return rcpp_result_gen; 45 | END_RCPP 46 | } 47 | // MatrixExample 48 | List MatrixExample(const NumericMatrix& orig); 49 | RcppExport SEXP _RcppExamples_MatrixExample(SEXP origSEXP) { 50 | BEGIN_RCPP 51 | Rcpp::RObject rcpp_result_gen; 52 | Rcpp::RNGScope rcpp_rngScope_gen; 53 | Rcpp::traits::input_parameter< const NumericMatrix& >::type orig(origSEXP); 54 | rcpp_result_gen = Rcpp::wrap(MatrixExample(orig)); 55 | return rcpp_result_gen; 56 | END_RCPP 57 | } 58 | // NumericVectorExample 59 | List NumericVectorExample(const NumericVector& orig); 60 | RcppExport SEXP _RcppExamples_NumericVectorExample(SEXP origSEXP) { 61 | BEGIN_RCPP 62 | Rcpp::RObject rcpp_result_gen; 63 | Rcpp::RNGScope rcpp_rngScope_gen; 64 | Rcpp::traits::input_parameter< const NumericVector& >::type orig(origSEXP); 65 | rcpp_result_gen = Rcpp::wrap(NumericVectorExample(orig)); 66 | return rcpp_result_gen; 67 | END_RCPP 68 | } 69 | // RcppRNGs 70 | DataFrame RcppRNGs(const int n); 71 | RcppExport SEXP _RcppExamples_RcppRNGs(SEXP nSEXP) { 72 | BEGIN_RCPP 73 | Rcpp::RObject rcpp_result_gen; 74 | Rcpp::RNGScope rcpp_rngScope_gen; 75 | Rcpp::traits::input_parameter< const int >::type n(nSEXP); 76 | rcpp_result_gen = Rcpp::wrap(RcppRNGs(n)); 77 | return rcpp_result_gen; 78 | END_RCPP 79 | } 80 | // StringVectorExample 81 | List StringVectorExample(const StringVector& orig); 82 | RcppExport SEXP _RcppExamples_StringVectorExample(SEXP origSEXP) { 83 | BEGIN_RCPP 84 | Rcpp::RObject rcpp_result_gen; 85 | Rcpp::RNGScope rcpp_rngScope_gen; 86 | Rcpp::traits::input_parameter< const StringVector& >::type orig(origSEXP); 87 | rcpp_result_gen = Rcpp::wrap(StringVectorExample(orig)); 88 | return rcpp_result_gen; 89 | END_RCPP 90 | } 91 | // factor2char 92 | Rcpp::CharacterVector factor2char(Rcpp::IntegerVector iv); 93 | RcppExport SEXP _RcppExamples_factor2char(SEXP ivSEXP) { 94 | BEGIN_RCPP 95 | Rcpp::RObject rcpp_result_gen; 96 | Rcpp::RNGScope rcpp_rngScope_gen; 97 | Rcpp::traits::input_parameter< Rcpp::IntegerVector >::type iv(ivSEXP); 98 | rcpp_result_gen = Rcpp::wrap(factor2char(iv)); 99 | return rcpp_result_gen; 100 | END_RCPP 101 | } 102 | // char2factor 103 | Rcpp::IntegerVector char2factor(std::vector sv); 104 | RcppExport SEXP _RcppExamples_char2factor(SEXP svSEXP) { 105 | BEGIN_RCPP 106 | Rcpp::RObject rcpp_result_gen; 107 | Rcpp::RNGScope rcpp_rngScope_gen; 108 | Rcpp::traits::input_parameter< std::vector >::type sv(svSEXP); 109 | rcpp_result_gen = Rcpp::wrap(char2factor(sv)); 110 | return rcpp_result_gen; 111 | END_RCPP 112 | } 113 | 114 | static const R_CallMethodDef CallEntries[] = { 115 | {"_RcppExamples_DataFrameExample", (DL_FUNC) &_RcppExamples_DataFrameExample, 1}, 116 | {"_RcppExamples_DateExample", (DL_FUNC) &_RcppExamples_DateExample, 2}, 117 | {"_RcppExamples_ListExamples", (DL_FUNC) &_RcppExamples_ListExamples, 1}, 118 | {"_RcppExamples_MatrixExample", (DL_FUNC) &_RcppExamples_MatrixExample, 1}, 119 | {"_RcppExamples_NumericVectorExample", (DL_FUNC) &_RcppExamples_NumericVectorExample, 1}, 120 | {"_RcppExamples_RcppRNGs", (DL_FUNC) &_RcppExamples_RcppRNGs, 1}, 121 | {"_RcppExamples_StringVectorExample", (DL_FUNC) &_RcppExamples_StringVectorExample, 1}, 122 | {"_RcppExamples_factor2char", (DL_FUNC) &_RcppExamples_factor2char, 1}, 123 | {"_RcppExamples_char2factor", (DL_FUNC) &_RcppExamples_char2factor, 1}, 124 | {NULL, NULL, 0} 125 | }; 126 | 127 | RcppExport void R_init_RcppExamples(DllInfo *dll) { 128 | R_registerRoutines(dll, NULL, CallEntries, NULL, NULL); 129 | R_useDynamicSymbols(dll, FALSE); 130 | } 131 | -------------------------------------------------------------------------------- /src/StringVectorExample.cpp: -------------------------------------------------------------------------------- 1 | // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*- 2 | // 3 | // RcppStringVectorExample.cpp: 4 | // 5 | // Copyright (C) 2009 - 2016 Dirk Eddelbuettel and Romain Francois 6 | // 7 | // This file is part of RcppExamples. 8 | // 9 | // RcppExamples is free software: you can redistribute it and/or modify it 10 | // under the terms of the GNU General Public License as published by 11 | // the Free Software Foundation, either version 2 of the License, or 12 | // (at your option) any later version. 13 | // 14 | // RcppExamples is distributed in the hope that it will be useful, but 15 | // WITHOUT ANY WARRANTY; without even the implied warranty of 16 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | // GNU General Public License for more details. 18 | // 19 | // You should have received a copy of the GNU General Public License 20 | // along with RcppExamples. If not, see . 21 | 22 | #include 23 | using namespace Rcpp; 24 | 25 | template T mytolower (T c) { 26 | static std::locale loc; 27 | return std::tolower(c, loc); 28 | } 29 | 30 | // [[Rcpp::export]] 31 | List StringVectorExample(const StringVector & orig) { 32 | StringVector vec(orig.size()); 33 | 34 | std::transform(orig.begin(), orig.end(), vec.begin(), 35 | make_string_transformer(mytolower) ); 36 | 37 | return List::create(Named("result") = vec, 38 | Named("original") = orig); 39 | } 40 | 41 | --------------------------------------------------------------------------------