- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个关于在 R 中使用 S3 类做某事的“正确”方法的问题。我想做的是有一个方法来更改类,然后在新类上调用相同的方法。像这样的东西:
my_func <- function(.x, ...) {
UseMethod("my_func")
}
my_func.character <- function(.x, ...) {
return(paste(".x is", .x, "of class", class(.x)))
}
my_func.numeric <- function(.x, ...) {
.x <- as.character(.x)
res <- my_func(.x) # this should call my_func.character
return(res)
}
character
对彼此而言
> my_func("hello")
[1] ".x is hello of class character"
> my_func(1)
[1] ".x is 1 of class character"
res <- my_func(.x)
)。
NextMethod()
必须以某种方式成为答案,但我已经阅读了一堆文档(例如
this 和
this),但他们都在谈论这件事,它正在跳到类列表中的下一个类,比如来自
data.frame
高达
matrix
当你有
class(df)
并获取
c("data.frame", "matrix")
例如。
NextMethod()
不是正确使用的东西,但是还有其他东西,还是我应该像拥有它一样离开它?
最佳答案
1) 如果想法是 my_func.numeric 做了额外的处理,但又想使用 my_func.character 而不重复,那么在 func.numeric
设置 .Class
然后调用NextMethod
像这样:
my_func.numeric <- function(.x, ...) {
.Class <- "character"
.x <- as.character(.x)
NextMethod()
}
NextMethod
返回一个结果,并且可以选择在它之后进行其他处理。更多信息可以通过发布
?NextMethod
找到。在 R。
my.func.character <- my.func.numeric <- function(.x, ...) {
.x <- as.character(.x)
paste(".x is", .x, "of class", class(.x))
}
my_func_impl <- function(.x, ...) paste(".x is", .x, "of class", class(.x))
my_func.character <- function(.x, ...) {
# some procesing unique to this method
my_func_impl(.x, ...)
}
my_func.numeric <- function(.x, ...) {
# some procesing unique to this method
my_func_impl(.x, ...)
}
关于R S3 类 - 更改类并传回相同的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60641142/
昨天开始学习ansible,所以我相信我可能会在这里冒XY问题的风险,但仍然...... 主要的yml: - hosts: localhost vars_files: [ "users.y
我在 Storyboard中创建了一些segue。它有一些标识符。我在方法 prepareForSegue:sender: 中配置 destinationViewController 并分配一些对象作
我想用 Promises 替换代码中的一些回调。 我有一个使用 setAnimationFrame 在循环中运行的模型。我想在我的模型中启动一些东西,并在完成时通知我。目前我是这样做的。 doSome
我需要动态获取用户选择的任何选项的值,并在单击按钮时将该值传递回 Facebook Pixel。 请参阅脚本区域中的所选选项值。 Wh
这个问题在这里已经有了答案: dismissModalViewController AND pass data back (4 个回答) 9年前关闭。 我有一个带有表格的popoverview,我想在
我想用 Python 显示一个 2D map ,然后在 Python 代码中用 coursor 的坐标做一些事情。但是,我无法获得 Python 部分的坐标。这是我的代码: from PyQt5.Qt
我正在使用 NXP lpc1769 演示板开发一个小项目。我正在使用 CodeRed 的 LPCExpresso 5 对其进行编程,我想知道是否可以将应用程序 hex/bin 文件从开发板传输回 PC
我是 Android 的新手,一般来说是事件驱动代码。我没有在 Activity 中嵌入大量匿名事件监听器类来处理 onClick 事件等,而是定义了单独的类以保持代码整洁。然后我使用它们,例如像这样
我已经成功地使用了 prepareForSegue,不过我已经成功地传递了变量。我现在正在尝试通过 segue 的反向传递 NSNumber 但 prepareForSegue 没有被调用。要返回到我
我有 ViewController 和 TableViewController。在 ViewController 中,我有 2 个按钮和 2 个文本字段,当单击按钮 1 时,它将导航到带有静态数据的
我如何将信息从 Jinja 模板页面传回 Flask? 假设我打印了一些项目列表。用户选择项目,我可以通过 Javascript 捕捉。 将所选项目作为参数传递给将生成该项目自己的页面的函数的最佳做法
我怎样才能将数据从 php 的然后行传回 ajax? PHP $query = 'SELECT * FROM picture order by rand() LIMIT 10'; $result =
在我的 MVC4 项目 View 中,正在传递 IEnumerable模型来查看。当我将表单提交回 Controller 时, Controller 方法的参数为 IEnumerable data 。
在我的 MVC4 项目 View 中,正在传递 IEnumerable要查看的模型。当我将表单提交回我的 Controller 时, Controller 方法的参数为 IEnumerable dat
我试图将订单 ID 从订单页面传递到 Paypal,然后返回到感谢页面,这样我就可以将订单标记为已付款,但我遇到了问题。目前我将这些变量传递给 Paypal,如下所示: $vars = array(
所以通常我会为此使用委托(delegate)模式,但这是一个棘手的情况。 View Controller A 呈现 -> View Controller B 呈现 -> View Controller
我是 Android 和 Java 的初学者。到目前为止还不错,但我偶然发现了一个我无法解决的问题。 我正在尝试在我的应用程序类中创建一个方法,该方法将使用传递给它的值对列表进行 http 调用。这是
在我的应用程序中,我使用 uitable 从我的列表中选择类别。我的任务是,当用户单击或选择一个单元格时,他应该能够在下一个 View (详细 View )中查看所选单元格的详细信息。当他在详细 Vi
我正在构建一个应用程序,当用户从 select 标签中选择一个值时,系统会提示用户使用另一个带有相应数据的 select 标签。我通过从第一个字段中获取数据并使用 Ajax 将其传回我的 Contro
我有一个 C# 应用程序需要获取从 C++ DLL 传回的可变长度数据。通常,我按如下方式解决此问题:对于每个实体类型(例如字符串或位图),我都有一个适当类型的持久 C++ 变量。然后,C# 应用程序
我是一名优秀的程序员,十分优秀!