R编程功能没有()

Cyb*_*hot 0 interactive r function

所以,我有以下非常简单的map.r文件.

我正在尝试让用户在交互模式下键入"click",然后使用该功能.

由于它是一个函数,用户必须键入"click()"我如何才能使它们只需要单词(没有括号),然后让该函数对img做一些事情.

所以用户输入:

mydist( "image.pnm")

点击

//然后单击函数会执行它应该执行的操作

mydist <- function(mapfile) {

    img <- read.pnm(mapfile)

    plot(img)
}

click <- function() {

    //Prompt user to click on img
}
Run Code Online (Sandbox Code Playgroud)

42-*_*42- 6

如果你给它一个自己的类和一个回应该信息的打印方法,你就可以实现你的目标.

  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!
Run Code Online (Sandbox Code Playgroud)

即使你想像Aaron所展示的那样将类定义"封装"在对象本身中,你也不会受限于打印消息.你可以这样做:

print.click <- function(x, ...) {plot(1:10, 10:1, type="l")
   cat("prompt user to click on img...\n")
Run Code Online (Sandbox Code Playgroud)

如果您想要呼叫locator,您可以扩展与用户的交互性.