函数 `grDevices:::C_col2rgb` 的源代码?

Source code of the function `grDevices:::C_col2rgb`?

如何找到函数 grDevices:::C_col2rgb 的 C 源代码?

在对一些 RGL 函数进行基准测试(使用 R pkg profvis)后,我被引导到这个函数,即 rgl:::rgl.quads 和其中调用的函数。包装 C_col2rgb 的相应 R 函数是来自 grDevices 的 col2rgb。我有兴趣查看 C_col2rgb 的来源,看看我是否可以制作更快的版本。

而且,一般来说,当您遇到在 R 代码中使用 C 函数时,是否有快速查找其源代码的方法?

非常感谢!

通常当你想查看一个R函数的源代码时,你只需在控制台中输入它的名字并回车即可。但是,当该函数用另一种语言(例如 C)编写并暴露给 R 时,您最终会看到(类似于)

.Call(C_col2rgb, col, alpha)

其中R调用编译后的代码。要查看此类功能的源代码,您实际上必须查看包源代码。您正在谈论的功能在 grDevices 包中,它是通常称为“base R”的一部分(不是(必须)与 R 包 base 混淆) - 包附带所有 R 安装。

https://github.com/wch/r-source that I like to consult if I need to look at R's source code. The code for the grDevices package is there at https://github.com/wch/r-source/tree/trunk/src/library/grDevices 的 GitHub 上有一个 R 源代码镜像。

正如我在评论中提到的,您可以在 r-source/src/library/grDevices/src/colors.c 找到 C_col2rgb() 的代码。但是,它看起来只是叫做 col2rgb()。真的一样吗?

。如果你查阅 Writing R Extensions, Section 1.5.4,你会看到

A NAMESPACE file can contain one or more useDynLib directives which allows shared objects that need to be loaded.... Using argument .fixes allows an automatic prefix to be added to the registered symbols, which can be useful when working with an existing package. For example, package KernSmooth has

    useDynLib(KernSmooth, .registration = TRUE, .fixes = "F_")

which makes the R variables corresponding to the Fortran symbols F_bkde and so on, and so avoid clashes with R code in the namespace.

我们可以在NAMESPACE file for grDevices

中看到
useDynLib(grDevices, .registration = TRUE, .fixes = "C_")

因此,此包中可用的 C 函数将 所有 都带有 C_ 前缀,即使它们不在 C 源代码中也是如此。这使您可以同时调用 R 和 C 函数 col2rgb 而不会导致任何问题。