{"id":19644881,"url":"https://github.com/uscbiostats/classy","last_synced_at":"2025-04-28T14:32:28.463Z","repository":{"id":93429770,"uuid":"153340039","full_name":"USCbiostats/classy","owner":"USCbiostats","description":"Simple example of C++ Classes and External Pointers with Rcpp","archived":false,"fork":false,"pushed_at":"2021-04-28T17:28:32.000Z","size":19,"stargazers_count":9,"open_issues_count":0,"forks_count":1,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-04-05T09:11:23.623Z","etag":null,"topics":["cpp","cpp11","hpc","r-package","r-programming","rcpp","rstats"],"latest_commit_sha":null,"homepage":null,"language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/USCbiostats.png","metadata":{"files":{"readme":"README.Rmd","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-10-16T19:09:35.000Z","updated_at":"2021-04-28T17:28:34.000Z","dependencies_parsed_at":"2023-03-10T08:31:49.695Z","dependency_job_id":null,"html_url":"https://github.com/USCbiostats/classy","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/USCbiostats%2Fclassy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/USCbiostats%2Fclassy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/USCbiostats%2Fclassy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/USCbiostats%2Fclassy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/USCbiostats","download_url":"https://codeload.github.com/USCbiostats/classy/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251330473,"owners_count":21572289,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cpp","cpp11","hpc","r-package","r-programming","rcpp","rstats"],"created_at":"2024-11-11T14:30:34.640Z","updated_at":"2025-04-28T14:32:28.422Z","avatar_url":"https://github.com/USCbiostats.png","language":"C++","readme":"---\noutput: github_document\n---\n\n\u003c!-- README.md is generated from README.Rmd. Please edit that file --\u003e\n\n```{r setup, include = FALSE}\nknitr::opts_chunk$set(\n  collapse = TRUE,\n  comment = \"#\u003e\",\n  fig.path = \"man/figures/README-\",\n  out.width = \"100%\"\n)\n```\n# classy\n\n[![Travis build status](https://travis-ci.org/USCbiostats/classy.svg?branch=master)](https://travis-ci.org/USCbiostats/classy) [![AppVeyor build status](https://ci.appveyor.com/api/projects/status/github/USCbiostats/classy?branch=master\u0026svg=true)](https://ci.appveyor.com/project/USCbiostats/classy) [![lifecycle](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://www.tidyverse.org/lifecycle/#experimental) [![CRAN status](https://www.r-pkg.org/badges/version/classy)](https://cran.r-project.org/package=classy)\n\nThis R package illustrates how to work with classes and `Rcpp::XPtr` objects in R and Rcpp.\n\nThe R package includes documentation using [roxygen2](https://cran.r-project.org/package=roxygen2), so the `Rd` files in the `man` folder were generated automatically by it.\n\n## Two types of examples\n\nBy default, all the `Rcpp::` objects that are mapped to R objects provide pointers instead of *deep copies*, which translates in efficient memory management and speed. The problem is that whenever you start using objects other than `NumericMatrix`, `NumericVector`, `IntegerMatrix`, etc. Rcpp generates copies of these objects.\n\nThe `classy` R package provides two different approaches when working with C++ classes. The first approach, `classy1`, more simple but inefficient, uses `std::vector\u003c double \u003e` member objects, which makes `Rcpp` duplicate the object that you are passing when creating the class. This should be OK whenever you don't care about memory usage, otherwise, if you are dealing with large memory objects (i.e. something that occupies a couple of gigs in your computer).\n\nYou can take a look at the source of the files:\n\n-   Header [classy1.h](src/classy1.h)\n-   Implementation [classy1.cpp](src/classy1.cpp)\n-   R wrappers (with Rcpp::XPtr) [R-wrappers1.cpp](src/R-wrappers1.cpp)\n\nThe second method provided, `classy3` (there will be an intermediate), uses `NumericVector` wrapped around smart pointers, in particular, [`std::shared_ptr`](https://en.cppreference.com/w/cpp/memory/shared_ptr) instead, but it is also implemented using [class templates](https://en.wikipedia.org/wiki/Template_(C%2B%2B)#Class_templates). This is a more appropiate approach when dealing with large objects since data is never duplicated.\n\nYou can take a look at the source of the files:\n\n-   Header [classy3.h](src/classy3.h)\n-   Implementation [classy3.cpp](src/classy3.cpp)\n-   R wrappers (with Rcpp::XPtr) [R-wrappers3.cpp](src/R-wrappers3.cpp)\n\n## Installation\n\n``` r\ndevtools::install_github(\"USCbiostats/classy\")\n```\n\n## Example 1: Using `classy1` (i.e. duplicating memory)\n\n```{r example1}\n\n# loading the package\nlibrary(classy)\n\nset.seed(1)\nx \u003c- runif(10)\nobj \u003c- new_classy1(x)\n\n# Getting the sum\nclassy_sum1(obj)\n\n# Counting\nclassy_count1(obj)\n\n# Conditional count\nclassy_count_if_less1(obj, 0.5)\n\n# The object\nclassy_get1(obj)\n```\n\n## Example 2: Using `classy3` (i.e. using smart pointers)\n\n```{r example3}\n\n# loading the package\nlibrary(classy)\n\nobj3 \u003c- new_classy3(x)\n\n# Getting the sum\nclassy_sum3(obj3)\n\n# Counting\nclassy_count3(obj3)\n\n# Conditional count\nclassy_count_if_less3(obj3, 0.5)\n\n# The object\nclassy_get3(obj3)\n```\n\n## Checking out the addresses\n\nThe final challenge, make sure that `obj3` (`classy3`) is actually pointing to\nthe same object than `x`.\n\n```{r}\nget_address(x)\nclassy_get_address1(obj)\nclassy_get_address3(obj3)\n```\n\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fuscbiostats%2Fclassy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fuscbiostats%2Fclassy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fuscbiostats%2Fclassy/lists"}