如何查看函數(shù)的源代碼?我想查看一個函數(shù)的源代碼,看看它是如何工作的。我知道我可以通過在提示符下鍵入其名稱來打印函數(shù):> tfunction (x) UseMethod("t")<bytecode: 0x2332948><environment: namespace:base>在這種情況下,是什么UseMethod("t")意思?我如何找到實際使用的源代碼,例如:t(1:10)?有沒有當(dāng)我看到之間的差異UseMethod,當(dāng)我看到standardGeneric和showMethods,與with?> withstandardGeneric for "with" defined from package "base"function (data, expr, ...) standardGeneric("with")<bytecode: 0x102fb3fc0><environment: 0x102fab988>Methods may be defined for arguments: dataUse showMethods("with") for currently available ones.在其他情況下,我可以看到正在調(diào)用R函數(shù),但我找不到這些函數(shù)的源代碼。> ts.unionfunction (..., dframe = FALSE) .cbind.ts(list(...), .makeNamesTs(...), dframe = dframe, union = TRUE)<bytecode: 0x36fbf88><environment: namespace:stats>> .cbindtsError: object '.cbindts' not found> .makeNamesTsError: object '.makeNamesTs' not found我如何找到.cbindts和.makeNamesTs?一樣的功能?在其他情況下,有一些R代碼,但大多數(shù)工作似乎在其他地方完成。> matrixfunction (data = NA, nrow = 1, ncol = 1, byrow = FALSE, dimnames = NULL) { if (is.object(data) || !is.atomic(data)) data <- as.vector(data) .Internal(matrix(data, nrow, ncol, byrow, dimnames, missing(nrow), missing(ncol)))}<bytecode: 0x134bd10><environment: namespace:base>> .Internalfunction (call) .Primitive(".Internal")> .Primitivefunction (name) .Primitive(".Primitive")我如何找出該.Primitive功能的作用?同樣,一些函數(shù)調(diào)用.C,.Call,.Fortran,.External,或.Internal。如何找到這些的源代碼
3 回答

神不在的星期二
TA貢獻1963條經(jīng)驗 獲得超6個贊
使用debug()函數(shù)進行調(diào)試時會顯示它。假設(shè)您想在t()轉(zhuǎn)置函數(shù)中查看底層代碼。只需輸入't',就不會顯示太多。
>t function (x) UseMethod("t")<bytecode: 0x000000003085c010><environment: namespace:base>
但是,使用'debug(functionName)',它揭示了底層代碼,沒有內(nèi)部。
> debug(t)> t(co2)debugging in: t(co2)debug: UseMethod("t")Browse[2]> debugging in: t.ts(co2)debug: { cl <- oldClass(x) other <- !(cl %in% c("ts", "mts")) class(x) <- if (any(other)) cl[other] attr(x, "tsp") <- NULL t(x)}Browse[3]> debug: cl <- oldClass(x)Browse[3]> debug: other <- !(cl %in% c("ts", "mts"))Browse[3]> debug: class(x) <- if (any(other)) cl[other]Browse[3]> debug: attr(x, "tsp") <- NULLBrowse[3]> debug: t(x)
編輯: debugonce()完成相同,而不必使用undebug()
- 3 回答
- 0 關(guān)注
- 1514 瀏覽
添加回答
舉報
0/150
提交
取消