具有原始格式的Caputure表达

时间:2016-01-10 09:43:29

标签: r expression

我尝试捕获表达式但deparse()函数截止输入。有没有办法保持原始的源格式(换行符)?

f <- function(expr) {
    cat(deparse(substitute(expr)), sep = "\n")
}
f(mean(1))
#> mean(1)
f({
    head(mtcars)
    mean(rnotm(100))
})
#> {
#>     head(mtcars)
#>     mean(rnotm(100))
#> }
f(function(x, y) {
    x + y
})
#> function(x, y) {
#>     x + y
#> }
f(LDF <- list(
    data.frame(V1 = runif(1000), V2 = sample(LETTERS, 1000, replace = TRUE)),
    data.frame(V1 = runif(1000), V2 = sample(LETTERS, 1000, replace = TRUE))
))
#> LDF <- list(data.frame(V1 = runif(1000), V2 = sample(LETTERS, 
#>     1000, replace = TRUE)), data.frame(V1 = runif(1000), V2 = sample(LETTERS, 
#>     1000, replace = TRUE)))
f({
    LDF <- list(
        data.frame(V1 = runif(1000), V2 = sample(LETTERS, 1000, replace = TRUE)),
        data.frame(V1 = runif(1000), V2 = sample(LETTERS, 1000, replace = TRUE))
    )
})
#> {
#>     LDF <- list(data.frame(V1 = runif(1000), V2 = sample(LETTERS, 
#>         1000, replace = TRUE)), data.frame(V1 = runif(1000), 
#>         V2 = sample(LETTERS, 1000, replace = TRUE)))
#> }

0 个答案:

没有答案