简体   繁体   English

R: get(as.character(FUN), mode = “function”, envir = envir) 中的错误:

[英]R: Error in get(as.character(FUN), mode = “function”, envir = envir) :

I am working with R. I am trying to replicate the answer provided from this stackoverflow post over here: How can I plot 3D function in r?我正在与 R 一起工作。我试图在这里复制此 stackoverflow 帖子中提供的答案: 如何在 r 中绘制 3D 函数?

Using the "lattice" library in R, I am trying to create a 3D surface plot of "input_1", "input_2", "input_3" - and color the surface according to values of "final_value".使用 R 中的“lattice”库,我试图创建“input_1”、“input_2”、“input_3”的 3D 曲面图 - 并根据“final_value”的值为曲面着色。

I created a function for this problem:我为这个问题创建了一个函数:

my_function_b <- function(input_1, input_2, input_3, input_4) {
    
    final_value = sin(input_1) + cos(input_2) + input_3 + input_4
    
}

Then, I assigned each "input" from this function a series of values:然后,我为这个函数的每个“输入”分配了一系列值:

input_1 <- seq(-10, 10, length= 30)
input_2 <- input_1
input_3 <- input_1
input_4 <- input_1

Next, I try to use the "outer" function:接下来,我尝试使用“外部”功能:

z <- outer(input_1, input_2, input_3, my_function_b)

But this returns the following error:但这会返回以下错误:

Error in get(as.character(FUN), mode = "function", envir = envir) : 
  object 'input_3' of mode 'function' was not found

Can someone please show me what I am doing wrong?有人可以告诉我我做错了什么吗?

Thanks谢谢

Additional References:其他参考资料:

outer takes only two arguments. outer只接受两个参数。 We may need pmap我们可能需要pmap

library(purrr)
pmap_dbl(list(input_1, input_2, input_3, input_4), my_function_b)

or Map/mapplyMap/mapply

mapply(my_function_b, input_1, input_2, input_3, input_4)

If we need all combinations, create the combinations with expand.grid and apply over the rows如果我们需要所有组合,请使用expand.grid创建组合并应用于行

tmp <- expand.grid(input_1 = input_1, input_2 = input_2, 
     input_3 = input_3, input_4 = input_4)
out <- apply(tmp, 1, 
       FUN = function(x) do.call(my_function_b, as.list(x)))

Or may speed up with dapply from collapse或者可能会通过dapplycollapse加速

library(collapse)

out1 <- dapply(tmp, MARGIN = 1, FUN = function(x) 
            my_function_b(x[1], x[2], x[3], x[4]))

Perhaps, we create the combinations on two vectors, and then add ?也许,我们在两个向量上创建组合,然后添加 ?

my_function_b <- function(input_1, input_2) sin(input_1) + cos(input_2)
tmp1 <- outer(input_1, input_2, my_function_b)
z <- tmp1 + input_3[col(tmp1)] + input_4[col(tmp1)]
library(lattice)
wireframe(z, drape=TRUE, col.regions=rainbow(100))

-output -输出

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 get(as.character(FUN), mode = “function”, envir = envir) 中的错误: - Error in get(as.character(FUN), mode = “function”, envir = envir) : 获取错误(as.character(FUN),模式=“功能”,环境=环境) - Error in get(as.character(FUN), mode = “function”, envir = envir) get(as.character(FUN), mode = &quot;function&quot;, envir = envir) 中的错误:模式 &#39;function&#39; 的对象 &#39;guide_legend&#39; - Error in get(as.character(FUN), mode = "function", envir = envir) : object 'guide_legend' of mode 'function' 从函数内部使用jags.parallel(R语言get(name,envir = envir)中的错误:未找到对象&#39;y&#39;) - Using jags.parallel from within a function (R language Error in get(name, envir = envir) : object 'y' not found) R; 使用操纵函数并获取:(函数(x)中的错误:未使用的参数(envir = <environment> ) - R; Using manipulate function and get : Error in (function (x) : unused argument (envir = <environment>) 在R中编写函数,收到错误“eval中的错误(expr,envir,enclos):...” - wrote function in R, receiving error “Error in eval(expr, envir, enclos) : …” ls中的错误(envir = envir,all.names = private):R中的&#39;envir&#39;参数无效 - Error in ls(envir = envir, all.names = private) : invalid 'envir' argument in R XLConnect&#39;envir&#39;错误 - XLConnect 'envir' error rollapply回归“envir”错误 - rollapply regression “envir” error R 不会编织到 html 或 pdf 与 Z9778840A0100CB30C98Z2876A4 块。 eval 中的错误(x,envir = envir):未找到 object '连接' - R wont knit to html or pdf with SQL chunks. Error in eval(x, envir = envir) : object 'connection' not found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM