簡體   English   中英

使用 R,如何使用 Rcpp 啟用 _FILE_ 宏?

[英]Using R, how to get _FILE_ macro enabled using Rcpp?

我在玩tesseract和magick...

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__編寫宏或 function ?

例如,

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;}");

后續問題是,安裝 package 時如何啟用這些功能?

您的問題的狹義答案(可能不是您想問的,請參閱我上面的評論)如下。

代碼

這使用嵌入式 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()
*/

Output

當我將其保存為文件answer.cpp ,這就是我們得到的:

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

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

請注意,我們不會將文件名傳遞給 function,而是將其取回。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM