0

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
}
2
  • More details would help. What are you trying to do? Why not just put click() in the mydist function? There's at least one way to do what you're asking for, but the use case seems odd, at least with the way it's described so far. More details would help provide answers tailored to your situation. Nov 15, 2011 at 1:49
  • In a comment below, @MarkKennedy wrote: I wanted a command list type of functionality, here. click would be read in somehow like readline() and then the .r does something. etc etc Nov 15, 2011 at 17:59

4 Answers 4

6

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.

0
4

Here's the 'proper' way to do it. This is an example from my .Rprofile file.

invisible(makeActiveBinding("newq", function(...){quartz();par(cex=.75); cat("OK\n")}, .GlobalEnv))  

The function makeActiveBinding does some sort of magic so the complete function gets called w/o need for any parentheses.

1
  • This is the more compact way to do it. Feb 25, 2014 at 6:08
1

Here's one way to do what you've asked for. However, there may be a better way to do what you're trying to do.

click <- structure(1, class="click")
print.click <- function(x, ...) {
  cat("prompt user to click on img...\n")
}

Then

> click
prompt user to click on img...
7
  • 1
    I wanted a command list type of functionality, here. click would be read in somehow like readline() and then the .r does something. etc etc
    – CyberShot
    Nov 15, 2011 at 2:26
  • Aaron's approach is a bit more compact than mine (and would override mine if they were in the same session), but they both allow you to attach any set of plotting or other commands to the execution of the code.
    – IRTFM
    Nov 15, 2011 at 17:25
  • Thanks, @DWin. Yours has better commentary, though. Feel free to add this way of defining click to yours; if you want to do that I'll probably delete this answer; it's mostly superfluous already. Nov 15, 2011 at 17:58
  • Thanks for permission, but I liked it so much I already did. I'll upvote, too.
    – IRTFM
    Nov 15, 2011 at 18:08
  • @DWin, I meant the structure line; otherwise they're the same, yes? I don't actually know what you mean by "encapsulate the class definition in the object." Nov 15, 2011 at 21:12
1

I could be totally confused as to what you want to do, but I think something like below may answer your query. It can be expanded upon by adding other 'else-if' options and other custom functions to be run as a result of whatever the user types.

customfunction <- function() {
print(1:10)
}

mydist <- function(mapfile) {

   img <- read.pnm(mapfile)
   plot(img)

ANSWER <- readline("")

# customfunction() will be run (printing 1:10) as soon as 
# the user types "click" and hits enter

if (ANSWER=="click") customfunction()

}
1
  • 1
    +1 From what the OP has said, this sounds like what I'd do; one could even print a little menu for the user to choose what they want to do. Nov 15, 2011 at 15:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.