├── .Rbuildignore ├── .gitignore ├── .vscode ├── c_cpp_properties.json ├── debug.R ├── launch.json └── tasks.json ├── DESCRIPTION ├── LICENSE ├── LICENSE.md ├── NAMESPACE ├── R ├── RcppExports.R ├── package.R └── test.R ├── README.md ├── VSCodeRcppDemo.Rproj ├── man └── calc_sum.Rd ├── src ├── .clang-format ├── .gitignore ├── RcppExports.cpp └── test.cpp └── tests ├── testthat.R └── testthat └── test-calc_sum.R /.Rbuildignore: -------------------------------------------------------------------------------- 1 | ^LICENSE\.md$ 2 | ^\.vscode$ 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .Rproj.user 2 | .Rhistory 3 | .RData 4 | .vscode/.* 5 | .vscode/vscode-R/ 6 | -------------------------------------------------------------------------------- /.vscode/c_cpp_properties.json: -------------------------------------------------------------------------------- 1 | { 2 | "configurations": [ 3 | { 4 | "name": "Linux", 5 | "includePath": [ 6 | "${workspaceFolder}/**", 7 | "${env:HOME}/R/x86_64-pc-linux-gnu-library/4.2/Rcpp/include", 8 | "/usr/share/R/include" 9 | ], 10 | "defines": [], 11 | "compilerPath": "/usr/bin/gcc", 12 | "cStandard": "c11", 13 | "cppStandard": "c++17" 14 | }, 15 | { 16 | "name": "Mac", 17 | "includePath": [ 18 | "${workspaceFolder}/**", 19 | "/Library/Frameworks/R.framework/Versions/4.2/Resources/library/Rcpp/include", 20 | "/Library/Frameworks/R.framework/Resources/include", 21 | "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include", 22 | "/usr/local/include" 23 | ], 24 | "defines": [], 25 | "macFrameworkPath": [ 26 | "/System/Library/Frameworks", 27 | "/Library/Frameworks" 28 | ], 29 | "compilerPath": "/usr/bin/clang", 30 | "cStandard": "c11", 31 | "cppStandard": "c++17" 32 | } 33 | ], 34 | "version": 4 35 | } 36 | -------------------------------------------------------------------------------- /.vscode/debug.R: -------------------------------------------------------------------------------- 1 | env <- Sys.getenv() 2 | envnames <- names(env) 3 | rnames <- envnames[startsWith(envnames, "R_")] 4 | cached_names <- rnames 5 | ld_lib_path <- Sys.getenv("LD_LIBRARY_PATH") 6 | if (ld_lib_path != "") { 7 | cached_names <- c("LD_LIBRARY_PATH", rnames) 8 | } 9 | writeLines(paste0(cached_names, "=", env[cached_names]), ".vscode/.env") 10 | -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | // Use IntelliSense to learn about possible attributes. 3 | // Hover to view descriptions of existing attributes. 4 | // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 5 | "version": "0.2.0", 6 | "configurations": [ 7 | { 8 | "name": "(gdb) Launch", 9 | "type": "cppdbg", 10 | "request": "launch", 11 | "program": "/usr/lib/R/bin/exec/R", 12 | "args": [ 13 | "--vanilla", 14 | "-e", 15 | "devtools::test()" 16 | ], 17 | "stopAtEntry": false, 18 | "envFile": "${workspaceFolder}/.vscode/.env", 19 | "cwd": "${workspaceFolder}", 20 | "externalConsole": false, 21 | "MIMode": "gdb", 22 | "setupCommands": [ 23 | { 24 | "description": "Enable pretty-printing for gdb", 25 | "text": "-enable-pretty-printing", 26 | "ignoreFailures": true 27 | } 28 | ], 29 | "preLaunchTask": "debug", 30 | "osx": { 31 | "program": "/Library/Frameworks/R.framework/Resources/bin/exec/R", 32 | "MIMode": "lldb" 33 | } 34 | } 35 | ] 36 | } 37 | -------------------------------------------------------------------------------- /.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | // See https://go.microsoft.com/fwlink/?LinkId=733558 3 | // for the documentation about the tasks.json format 4 | "version": "2.0.0", 5 | "tasks": [ 6 | { 7 | "label": "debug", 8 | "type": "shell", 9 | "command": "Rscript ${workspaceFolder}/.vscode/debug.R", 10 | "problemMatcher": [] 11 | } 12 | ] 13 | } 14 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: VSCodeRcppDemo 2 | Title: Writing and Debugging Rcpp in VSCode 3 | Version: 0.1 4 | Authors@R: person("Kun", "Ren", email = "renkun@outlook.com", role = c("aut", "cre")) 5 | Description: A demo package to demonstrate writing and debugging Rcpp in VSCode 6 | Depends: R (>= 3.4.0) 7 | License: MIT + file LICENSE 8 | Encoding: UTF-8 9 | LazyData: true 10 | ByteCompile: TRUE 11 | Suggests: testthat 12 | RoxygenNote: 7.2.3 13 | Roxygen: list(markdown = TRUE) 14 | LinkingTo: 15 | Rcpp 16 | Imports: 17 | Rcpp 18 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | YEAR: 2019 2 | COPYRIGHT HOLDER: Kun Ren 3 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # MIT License 2 | 3 | Copyright (c) 2019 Kun Ren 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /NAMESPACE: -------------------------------------------------------------------------------- 1 | # Generated by roxygen2: do not edit by hand 2 | 3 | export(calc_sum) 4 | importFrom(Rcpp,sourceCpp) 5 | useDynLib(VSCodeRcppDemo, .registration = TRUE) 6 | -------------------------------------------------------------------------------- /R/RcppExports.R: -------------------------------------------------------------------------------- 1 | # Generated by using Rcpp::compileAttributes() -> do not edit by hand 2 | # Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393 3 | 4 | #' Simple sum 5 | #' @param x a numeric vector 6 | #' @export 7 | calc_sum <- function(x) { 8 | .Call(`_VSCodeRcppDemo_calc_sum`, x) 9 | } 10 | 11 | -------------------------------------------------------------------------------- /R/package.R: -------------------------------------------------------------------------------- 1 | ## usethis namespace: start 2 | #' @useDynLib VSCodeRcppDemo, .registration = TRUE 3 | ## usethis namespace: end 4 | NULL 5 | 6 | ## usethis namespace: start 7 | #' @importFrom Rcpp sourceCpp 8 | ## usethis namespace: end 9 | NULL 10 | -------------------------------------------------------------------------------- /R/test.R: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/renkun-ken/vscode-rcpp-demo/ba141df807e4e26f743a611219d186fe93619083/R/test.R -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # vscode-rcpp-demo 2 | 3 | This project is a minimal example of writing and debugging [Rcpp](http://www.rcpp.org/) in VSCode. 4 | 5 | For writing and debugging [cpp11](https://cpp11.r-lib.org) in VSCode, you may 6 | go to [vscode-cpp11-demo](https://github.com/renkun-ken/vscode-cpp11-demo). 7 | ## Preview 8 | 9 | * Code editing 10 | 11 | ![Code editing](https://user-images.githubusercontent.com/4662568/71535253-8f2d2380-293f-11ea-920e-8a58a944fb50.gif) 12 | 13 | * Debugging 14 | 15 | ![Debugging](https://user-images.githubusercontent.com/4662568/71535254-8f2d2380-293f-11ea-85cc-0828234d3c26.gif) 16 | 17 | ## Configuration 18 | 19 | Before configuration, install VSCode [C/C++ extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools). 20 | 21 | ### Code editing 22 | 23 | For C/C++ source code editing, only `.vscode/c_cpp_properties.json` is needed. When the proper paths of included headers are 24 | provided, the full-featured C/C++ editing features including auto-completion, hover, definition, type inference, etc. will work. 25 | Source code editing does not require that the code is in a package. 26 | 27 | In this repo, `c_cpp_properties.json` is supposed to work with R 4.0 under Ubuntu 16.04 or above. You may need to alter the 28 | paths according to your system and C/C++ dependencies of your package. 29 | 30 | For example, if your package depends on [RcppArmadillo](https://github.com/RcppCore/RcppArmadillo), you may run the following R 31 | code to determine the include path: 32 | 33 | ```r 34 | RcppArmadillo:::CxxFlags() 35 | ``` 36 | 37 | Then you may add the following path to `includePath`: 38 | 39 | ```text 40 | ${env:HOME}/R/x86_64-pc-linux-gnu-library/4.0/RcppArmadillo/include 41 | ``` 42 | 43 | For more code editing features, please visit [Edit C++ in Visual Studio Code](https://code.visualstudio.com/docs/cpp/cpp-ide). 44 | 45 | ### Debugging 46 | 47 | Rcpp debugging is easy to configure when the code is in an R package that uses Rcpp like how this repo is organized. 48 | 49 | Since `R` is not a binary executable but a bash script in which required environment variables are setup to start an R session, 50 | we also need to setup those environment variables for the debugger to run the R session. 51 | 52 | `.vscode/debug.R` and `.vscode/tasks.json` are the code to capture those environment variables and to run before debugging. 53 | You may need, initially, to run twice in debugging mode before environment variables are properly picked up in `.vscode/.env` 54 | 55 | `.vscode/launch.json` defines the debugger configuration which in this repo works for R 4.0 under Ubuntu 16.04 or above. 56 | 57 | For more debugging features, please visit [Debug C++ in Visual Studio Code](https://code.visualstudio.com/docs/cpp/cpp-debug). 58 | 59 | ## More information 60 | 61 | When properly configured, the Rcpp development environment is full-featured as general C/C++ development in VSCode. 62 | 63 | Please visit [C/C++ for Visual Studio Code](https://code.visualstudio.com/docs/languages/cpp) for more information. 64 | -------------------------------------------------------------------------------- /VSCodeRcppDemo.Rproj: -------------------------------------------------------------------------------- 1 | Version: 1.0 2 | 3 | RestoreWorkspace: No 4 | SaveWorkspace: No 5 | AlwaysSaveHistory: Default 6 | 7 | EnableCodeIndexing: Yes 8 | UseSpacesForTab: Yes 9 | NumSpacesForTab: 2 10 | Encoding: UTF-8 11 | 12 | RnwWeave: Sweave 13 | LaTeX: pdfLaTeX 14 | 15 | AutoAppendNewline: Yes 16 | StripTrailingWhitespace: Yes 17 | 18 | BuildType: Package 19 | PackageUseDevtools: Yes 20 | PackageInstallArgs: --no-multiarch --with-keep.source 21 | PackageRoxygenize: rd,collate,namespace,vignette 22 | -------------------------------------------------------------------------------- /man/calc_sum.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/RcppExports.R 3 | \name{calc_sum} 4 | \alias{calc_sum} 5 | \title{Simple sum} 6 | \usage{ 7 | calc_sum(x) 8 | } 9 | \arguments{ 10 | \item{x}{a numeric vector} 11 | } 12 | \description{ 13 | Simple sum 14 | } 15 | -------------------------------------------------------------------------------- /src/.clang-format: -------------------------------------------------------------------------------- 1 | --- 2 | Language: Cpp 3 | BasedOnStyle: LLVM 4 | Standard: Cpp11 5 | ReflowComments: false 6 | --- 7 | -------------------------------------------------------------------------------- /src/.gitignore: -------------------------------------------------------------------------------- 1 | *.o 2 | *.so 3 | *.dll 4 | -------------------------------------------------------------------------------- /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 | // calc_sum 14 | double calc_sum(NumericVector x); 15 | RcppExport SEXP _VSCodeRcppDemo_calc_sum(SEXP xSEXP) { 16 | BEGIN_RCPP 17 | Rcpp::RObject rcpp_result_gen; 18 | Rcpp::RNGScope rcpp_rngScope_gen; 19 | Rcpp::traits::input_parameter< NumericVector >::type x(xSEXP); 20 | rcpp_result_gen = Rcpp::wrap(calc_sum(x)); 21 | return rcpp_result_gen; 22 | END_RCPP 23 | } 24 | 25 | static const R_CallMethodDef CallEntries[] = { 26 | {"_VSCodeRcppDemo_calc_sum", (DL_FUNC) &_VSCodeRcppDemo_calc_sum, 1}, 27 | {NULL, NULL, 0} 28 | }; 29 | 30 | RcppExport void R_init_VSCodeRcppDemo(DllInfo *dll) { 31 | R_registerRoutines(dll, NULL, CallEntries, NULL, NULL); 32 | R_useDynamicSymbols(dll, FALSE); 33 | } 34 | -------------------------------------------------------------------------------- /src/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace Rcpp; 4 | 5 | //' Simple sum 6 | //' @param x a numeric vector 7 | //' @export 8 | // [[Rcpp::export]] 9 | double calc_sum(NumericVector x) { 10 | double sum = 0; 11 | for (int i = 0; i < x.size(); ++i) { 12 | std::cout << i << std::endl; 13 | sum += x[i]; 14 | } 15 | return sum; 16 | } 17 | -------------------------------------------------------------------------------- /tests/testthat.R: -------------------------------------------------------------------------------- 1 | library(testthat) 2 | library(VSCodeRcppDemo) 3 | 4 | test_check("VSCodeRcppDemo") 5 | -------------------------------------------------------------------------------- /tests/testthat/test-calc_sum.R: -------------------------------------------------------------------------------- 1 | test_that("calc_sum works", { 2 | expect_equal(calc_sum(numeric()), 0) 3 | expect_equal(calc_sum(c(1, 2, 3)), 6) 4 | }) 5 | --------------------------------------------------------------------------------