c++ 如何使用Rcpp启用_FILE_ macro?

hc8w905p  于 2023-03-14  发布在  Mac
关注(0)|答案(1)|浏览(157)

我在玩魔方和魔法...

library(magick); #install.packages("magick", dependencies=TRUE);
library(tesseract); # install.packages("tesseract");
# https://github.com/ropensci/magick/issues/154

img.file = "iris-ocr.png";

img = magick::image_read( img.file );
img.txt = tesseract::image_ocr(img);

cat(img.txt);

注意:img.file和运行代码的笔记本在同一个位置。也就是说,没有使用setwd(),也没有完整的文件路径。但是它工作了。为了尝试,下面是PNG图像文件:

https://raw.githubusercontent.com/MonteShaffer/MasterClassDataAnalytics/main/-course-/02.020_hello-world-notebook/iris-ocr.png

所以我深入研究了magick的源代码

rdx = readRDS("C:\\Users\\Monte J. Shaffer\\Documents\\R\\win-library\\4.1\\magick\\R\\magick.rdx");

info = rdx$variables$magick_image_readpath;

# # https://stackoverflow.com/questions/61841221/how-to-view-open-and-save-a-rdb-file-in-rstudio

readRDB <- function(filename, offset, size, type = 'gzip') {
        f <- file(filename, 'rb')
        on.exit(close(f))
        seek(f, offset + 4)
        unserialize(memDecompress(readBin(f, 'raw', size - 4), type))
}

obj = readRDB("C:\\Users\\Monte J. Shaffer\\Documents\\R\\win-library\\4.1\\magick\\R\\magick.rdb", offset=info[1], size=info[2]);

其显示如下:

function (paths, density, depth, strip, defines) 
{
    .Call("_magick_magick_image_readpath", PACKAGE = "magick", 
        paths, density, depth, strip, defines)
}

源Rcpp代码显示:

// magick_image_readpath
XPtrImage magick_image_readpath(Rcpp::CharacterVector paths, Rcpp::CharacterVector density, Rcpp::IntegerVector depth, bool strip, Rcpp::CharacterVector defines);
RcppExport SEXP _magick_magick_image_readpath(SEXP pathsSEXP, SEXP densitySEXP, SEXP depthSEXP, SEXP stripSEXP, SEXP definesSEXP) {
BEGIN_RCPP
    Rcpp::RObject rcpp_result_gen;
    Rcpp::RNGScope rcpp_rngScope_gen;
    Rcpp::traits::input_parameter< Rcpp::CharacterVector >::type paths(pathsSEXP);
    Rcpp::traits::input_parameter< Rcpp::CharacterVector >::type density(densitySEXP);
    Rcpp::traits::input_parameter< Rcpp::IntegerVector >::type depth(depthSEXP);
    Rcpp::traits::input_parameter< bool >::type strip(stripSEXP);
    Rcpp::traits::input_parameter< Rcpp::CharacterVector >::type defines(definesSEXP);
    rcpp_result_gen = Rcpp::wrap(magick_image_readpath(paths, density, depth, strip, defines));
    return rcpp_result_gen;
END_RCPP
}

我熟悉C++(和PHP)的__FILE__语法:https://www.tutorialspoint.com/what-are-file-line-and-function-in-cplusplus
使用R和Rcpp,如何为__FILE__编写宏或函数?
例如,

getFILE = function() { __FILE__; }

类似于:

Rcpp::cppFunction("long long RShift(long long a, int b) { return a >> b;}");
Rcpp::cppFunction("long long LShift(long long a, int b) { return a << b;}");

接下来的问题是,如何在安装软件包时启用这些功能?

ffx8fchx

ffx8fchx1#

你的问题的狭义答案(这可能不是你想问的,见我上面的评论)如下。

代码

这使用了嵌入式R代码的技巧作为C++源代码的注解,然后sourceCpp()为我们运行。

#include <Rcpp/Lightest> // Rcpp 1.0.8 or newer

// [[Rcpp::export]]
std::string getFile() {
    const std::string filename = __FILE__;
    return filename;
}

/*** R
getFile()
*/

输出

当我把这个保存为一个文件answer.cpp时,我们得到了:

> Rcpp::sourceCpp("answer.cpp")

> getFile()
[1] "answer.cpp"
>

注意,我们并没有将文件名传递给函数,而是将它取回来。

相关问题