R编程功能无() [英] R programming function without ()
问题描述
我试图让用户在交互模式下输入click,然后获得函数。
因为它是一个函数,所以用户必须输入click(),我怎样才能使它们只有这个单词(没有括号),然后让该函数对img执行一些操作。
所以用户键入:
mydist(image.pnm)
点击
< //然后函数click做它应该做的事情
$ b $ pre $ m $ c $ mydist < - function(mapfile){
img < - read.pnm(mapfile)
plot(img)
}
单击< - function(){
//提示用户点击img
}
如果你给它一个自己的类和一个能够回应这个消息的打印方法,你就可以实现你的目标。
print.click< - function(x,...){
#
#可以在这里做些什么
#the< something>可能是一个情节或计算
情节(1:10,10:1,type =l)
cat(您的点击消息在这里\\\
或许\\\
向下划线!)
隐形(x)
}
点击< - 点击
class(点击)< - 点击
点击
#您的点击消息在这里
#也许
#向下划线!
即使您想像Aaron所说的那样将对象本身的类定义封装不限于打印消息。你可以这样做:
print.click< - function(x,...){plot(1: (提示用户点击img ... \\\
)
如果您想调用 locator
,您可以扩展与用户的交互。
So, I Have the following very simple map.r file.
I'm trying to have the user type "click" in interactive mode and then have the function .
Since it's a function, the user has to type "click()" how can I make it so that they only have to the word (w/o parentheses), and then have that function do something with the img.
So the user types:
mydist("image.pnm")
click
//And then the function click does what it's supposed to
mydist <- function(mapfile) {
img <- read.pnm(mapfile)
plot(img)
}
click <- function() {
//Prompt user to click on img
}
If you give it a class of its own and a print method which echoes that message you can achieve your goal.
print.click <- function(x, ...){
#
# could do something here
# the <something> could be a plot or calculation
plot(1:10, 10:1, type="l")
cat("Your click message here\n perhaps \n Downward line plotted!")
invisible(x)
}
click <- "click"
class(click) <- "click"
click
# Your click message here
# perhaps
# Downward line plotted!
Even if you wanted to "encapsulate" the class definition in the object itself as Aaron demonstrated, you would not be limited to printing a message. You could do something like this:
print.click <- function(x, ...) {plot(1:10, 10:1, type="l")
cat("prompt user to click on img...\n")
If you wanted to call locator
, you could extend the interactivity with the user.
这篇关于R编程功能无()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!