【问题标题】:R plot method for custom S4 class自定义 S4 类的 R 绘图方法
【发布时间】:2021-02-17 10:52:53
【问题描述】:

我正在开发一个 R 包,并想为我的自定义 S4 类创建一个绘图函数。它应该像这样工作:

setClass("Person", representation(name = "character"))
me <- new("Person", name = "Dan")

plot(me) # should do something, say:

plot.new()
text(x = 0.5, y = 0.5, me@name)

【问题讨论】:

    标签: r package class-method s4


    【解决方案1】:

    我想通了。 definition 参数必须是接受参数 x 的函数。

    setMethod("plot", "Person", function(x) {
        plot.new()
        text(x = 0.5, y = 0.5, x@name)
    })
    plot(me)
    

    【讨论】:

    • 是的,当使用现有的泛型时,您必须为签名中的参数保持相同的名称。您可以随意命名其他参数。
    猜你喜欢
    • 2021-02-17
    • 2013-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多