如何在R中无形输出部分输出

时间:2019-05-13 04:59:15

标签: r function

在下面的R函数中,我希望始终可见输出对象output。但是我想知道是否有一种方法可以无形但可提取输出对象cl,也就是函数调用吗?

如果不可能,如何将cl明显地添加到output上,以便在执行cl之后与output一起提取foo <- function(one = T){ cl <- match.call() output <- if(one) data.frame(d = 6:8, long = c(F, F,F)) else list(Study1 = data.frame(d = 6:8, long = c(F, F,F)), Study2 = data.frame(d = 9:11, long = c(T, T, F)) ) return(output) } # Example of use: foo()

var html = '';
$('#pro').html('');
$.each(pro, function( index, value ) {
    if (value.name + " - Price: " + value.unit_cost==event.target.textContent) {
        html += value.description;
        html += value.unit_cost;
        html += '</br>';
        html += '<img src="'+value.image_url+'">';
    }
});
$('#pro').html(html);

1 个答案:

答案 0 :(得分:0)

您可以return list的功能输出

foo <- function(one = T){
     cl <- match.call()  
     output <- if(one) data.frame(d = 6:8, long = c(F, F,F)) else
     list(Study1 = data.frame(d = 6:8, long = c(F, F,F)), 
         Study2 = data.frame(d = 9:11, long = c(T, T, F)) )
    return(list(output = output, cl = cl))
}

,然后使用eval评估函数

a = foo(TRUE)

eval(a$cl)
#$output
#  d  long
#1 6 FALSE
#2 7 FALSE
#3 8 FALSE

#$cl
#foo(one = TRUE)