如何在 R 中查看 NMF 算法的源代码?

How to view the source code of a NMF algorithm in R?

我是 R 的新手,我必须编写一个新的 NMF 算法,以便在调用函数 nmf() 时用作参数 method

作为开始,我想查看内置算法的源代码,如 brunet、KL、lee,但我找不到它。有人可以帮我吗?

如果您使用的是 Rstudio,在 R 脚本中您可以按住 Ctrl 并单击函数名称,它会跳转到函数源。

您可以下载源代码 here,然后检查您感兴趣的函数的代码。此外,尝试在控制台输入函数的名称。如果它是用 R 编写的,您可能会看到打印出的整个函数。例如

f <- function(x) {return(x^2)}
f

function(x) {return(x^2)}

如果该函数是用 C/C++ 编写的,它会给您它已编译的信息,您可以在下载的源代码中查找该函数,例如

head
function (x, ...) 
UseMethod("head")
<bytecode: 0x10a0d9158>
<environment: namespace:utils>
library(NMF)
showMethods("nmf")

Function: nmf (package NMF)
x="data.frame", rank="ANY", method="ANY"
x="formula", rank="ANY", method="ANY"
x="matrix", rank="data.frame", method="ANY"
x="matrix", rank="matrix", method="ANY"
x="matrix", rank="missing", method="ANY"
x="matrix", rank="NMF", method="ANY"
x="matrix", rank="NULL", method="ANY"
x="matrix", rank="numeric", method="character"
x="matrix", rank="numeric", method="function"
x="matrix", rank="numeric", method="list"
x="matrix", rank="numeric", method="missing"
x="matrix", rank="numeric", method="NMFStrategy"
x="matrix", rank="numeric", method="NULL"

下一个 select 您想要查看其源代码的方法

selectMethod("nmf", c("matrix", "matrix", "ANY"))
selectMethod("nmf", c("data.frame", "ANY", "ANY"))