Package 'RcppIconvExample'

Title: Rcpp Example of Using Iconv Offered by R
Description: Character conversion via the 'iconv' library is used by R itself, and can be accessed from compiled code relying on one standard header exported by R. This package illustrates this usage by building on and extending an earlier example in a blog post at <https://fishandwhistle.net/post/2021/using-rs-cross-platform-iconv-wrapper-from-cpp11/>. Rcpp is used only for its convenience of seamlessly building the package, and converting between character variable at the R and C++ levels.
Authors: Dirk Eddelbuettel
Maintainer: Dirk Eddelbuettel <[email protected]>
License: GPL (>= 2)
Version: 0.0.1
Built: 2024-10-05 03:40:31 UTC
Source: https://github.com/eddelbuettel/rcppiconvexample

Help Index


Rcpp Example of Using Iconv Offered by R

Description

Character conversion via the 'iconv' library is used by R itself, and can be accessed from compiled code relying on one standard header exported by R. This package illustrates this usage by building on and extending an earlier example in a blog post at <https://fishandwhistle.net/post/2021/using-rs-cross-platform-iconv-wrapper-from-cpp11/>. Rcpp is used only for its convenience of seamlessly building the package, and converting between character variable at the R and C++ levels.

Package Content

Index of help topics:

RcppIconvExample-package
                        Rcpp Example of Using Iconv Offered by R
read_file               Read an Enconded File, Optionally Converting to
                        Another Encoding

Maintainer

Dirk Eddelbuettel <[email protected]>

Author(s)

Dirk Eddelbuettel


Read an Enconded File, Optionally Converting to Another Encoding

Description

This function relies on the 'iconv' facility available with R. Having 'iconv' is optional but likely for most builds of R; see capabilities("iconv") to verify. Also note that 'iconv', while portable, does not guarantee identical results across implementations and operating systems.

Usage

read_file(filename, encoding = "")

Arguments

filename

[string] A filename

encoding

[string, optional] An encoding. If present, file content is converted to the given encoding; if missing (as indicated by the default empty string) no conversion is made.

Value

A string

See Also

https://fishandwhistle.net/post/2021/using-rs-cross-platform-iconv-wrapper-from-cpp11/

Examples

## example file from package 'uchardet' encoding as windows-1252
win1252file <- system.file("rawdata", "windows-1252.txt", package="RcppIconvExample")
win1252txt <- read_file(win1252file, "windows-1252")
utf8file <- system.file("rawdata", "utf8.txt", package="RcppIconvExample")
utf8txt <- read_file(utf8file, "UTF-8")
stopifnot(substr(win1252txt, 1, 62) == substr(utf8txt, 1, 62))
cat(win1252txt)