https://github.com/pachadotdev/pudu
Facilitate cleaning strings in C++ code before passing them to R.
https://github.com/pachadotdev/pudu
cpp11 cpp11-library r
Last synced: 4 months ago
JSON representation
Facilitate cleaning strings in C++ code before passing them to R.
- Host: GitHub
- URL: https://github.com/pachadotdev/pudu
- Owner: pachadotdev
- License: apache-2.0
- Created: 2024-10-07T02:08:46.000Z (8 months ago)
- Default Branch: main
- Last Pushed: 2025-01-11T10:47:08.000Z (4 months ago)
- Last Synced: 2025-01-11T11:37:42.849Z (4 months ago)
- Topics: cpp11, cpp11-library, r
- Language: C++
- Homepage:
- Size: 44.9 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.Rmd
- Changelog: NEWS.md
- Contributing: .github/CONTRIBUTING.md
- License: LICENSE.md
- Code of conduct: .github/CODE_OF_CONDUCT.md
- Support: .github/SUPPORT.md
Awesome Lists containing this project
README
---
output: github_document
---```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```# pudu
[](https://github.com/pachadotdev/pudu/actions/workflows/R-CMD-check.yaml)
[](https://app.codecov.io/gh/pachadotdev/pudu)
[](https://buymeacoffee.com/pacha)
[](https://CRAN.R-project.org/package=pudu)# Overview
The goal of pudu is to provide function declarations and inline
function definitions that facilitate cleaning strings in C++ code before passing
them to R. It works with `cpp11::strings` and `std::vector`
objects.The idea is the same as the
[janitor](https://cran.r-project.org/package=janitor) package, but for C++ code.Why is the name Pudu? Pudu is the smallest deer on planet Earth and this package
is tiny too. The original Pudu (unvectorized) was drawn by
[Pokanvas](https://www.deviantart.com/pokanvas/art/Baby-Pudu-944226115).# Installation
You can install the development version of pudu with:
``` r
remotes::install_github("pachadotdev/pudu")
```# Example
Here is how you can use the functions in this package in C++ code:
```cpp
#include "00_main.h"
#include "pudu.hpp"// Example 1
std::vector x = {" REGION NAME "};
tidy_std_names(x);
// Example 2
tidy_std_vars(x);
// Example 3
[[cpp11::register]] cpp11::writable::strings test_tidy_r_names(
const cpp11::strings& x) {
cpp11::writable::strings res = tidy_r_names(x);
return res;
}// Example 4
[[cpp11::register]] cpp11::writable::strings test_tidy_r_vars(
const cpp11::strings& x) {
cpp11::writable::strings res = tidy_r_vars(x);
return res;
}
```From C++, the first two examples return "region_name" and "REGION NAME",
respectively.From R, the last two examples return "region_name" and "REGION_NAME" when
calling `test_tidy_r_names(" REGION NAME ")` and
`test_tidy_r_vars(" REGION NAME ")`, respectively.Messy strings such as " DEPTO. .REF_ID_ " are converted to "depto_ref_id" and
"DEPTO. .REF_ID_", respectively.The following tests in R should give an idea of how the functions work:
```r
# German
vars <- "Gau\xc3\x9f"
expect_equal(test_tidy_r_names(vars), "gau")
expect_equal(test_tidy_r_vars(vars), "Gau\u00df")# French
vars <- "c\xc2\xb4est-\xc3\xa0-dire"
expect_equal(test_tidy_r_names(vars), "c_est_a_dire")
expect_equal(test_tidy_r_vars(vars), "c\u00b4est-\u00e0-dire")# Spanish
vars <- "\xc2\xbfC\xc3\xb3mo est\xc3\xa1s\x3f"
expect_equal(test_tidy_r_names(vars), "como_estas")
expect_equal(test_tidy_r_vars(vars), "\u00bfC\u00f3mo est\u00e1s\u003f")# Japanese
vars <- "Konnichiwa \xe3\x81\x93\xe3\x82\x93\xe3\x81\xab\xe3\x81\xa1\xe3\x81\xaf"
expect_equal(test_tidy_r_names(vars), "konnichiwa")
expect_equal(test_tidy_r_vars(vars), "Konnichiwa \u3053\u3093\u306b\u3061\u306f")
```