R 函数不返回数值 [英] R function does not return a numeric value

查看:75
本文介绍了R 函数不返回数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个函数如下:

I have created a function as bellow:

#!/usr/bin/Rscript
targetRunner <- function(temp) {
  cost <- ((temp - 32) * (5 / 9)) + 273.15
  return(cost)
}

args <- commandArgs(trailingOnly=TRUE)
if (length(args) > 0) {
  targetRunner(as.numeric(args[1]))
} else {
  print("Supply one argument as the numeric value: usage ./target-runner temp")
}

我可以从 Linux 的终端完美地运行这个功能.但是,当我将它作为 tragetRunner 引入 iRace 包并使用 checkIraceScenario(scenario = scene) 来检查我是否已为该包正确准备了所有内容时,我收到以下错误:

I can flawlessly run this function from the terminal of Linux. However, when I introduce it to the iRace package as the tragetRunner and use checkIraceScenario(scenario = scenario) to check that I have properly prepared everything for this package, I receive the following error:

...
== irace == The output was:
[1] NA
...
# targetRunner returned:
NULL

根据 iRace 包的用户指南,仅当此函数的结果不是数值时才会出现此错误.请告诉我为什么这个函数不返回数值?

According to the User Guide of the iRace package, this error occurs only when the outcome of this function is not a numeric value. Would you please let me know why this function does not return a numeric value?

推荐答案

这个问题是针对 iRace 包的.请相应地标记它.

This question is specific to iRace package. Please Tag it accordingly.

我不熟悉 iRace,但从提供的示例 [1] 看来,上面的脚本必须打印一个简单的数值.在您的情况下,R 打印的文本比您想要的多.尝试在 if 语句中使用 cat(targetRunner(...)).

I am not familiar with iRace but from the examples provided [1] it looks like the script above must print a simple numeric value. In your case, R printing more text than you want. Try to use cat(targetRunner(...)) in your if statement.

您还应该仔细阅读 iRace 文档,因为您将获得 4 个参数作为脚本的输入,而不仅仅是一个,请确保第一个参数是您想要用于温度转换的参数.

You should also read carefully the iRace documentation because you are getting 4 arguments as input to the script, not just one, make sure the first argument is the one you want to use for temperature conversion.

[1] https://github.com/cran/irace/blob/master/inst/examples/Spear/target-runner

这篇关于R 函数不返回数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆