Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I display a message to user at the end of a function's return output?

Tags:

r

I am creating a function that imports a .txt file and returns a data.frame. At times I want to display a message to the user to go along with the data. My problem is that by including the message in the body of the function, it is displayed before the data, and in a large data.frame with many rows, the user likely will never see it.

For example, given:

foo=function(cars){
  message('Pay attention to me!')
  return(cars)
}

If the user enters:

foo(cars)

The function will display my message, but only before the returned object (in this case the data cars from R's base package. How can I display my message() contents at the bottom of the returned data.frame?

I have tried returning the message too, but to no avail:

foo=function(cars){
  return(cars,message('help!'))
}

I notice that the warning() and stop() functions both display their text at the end of the data.frame, but I am wanting to display a message that is neither a warning nor an error, but just an FYI.

To clarify, ideally, the message would be displayed when the function is called (regardless of if the output is assigned to a variable name or not) but it would not be displayed every time the resulting object is used. foo(cars) should display the message, as should obj=foo(cars). But just obj should not.

like image 836
CephBirk Avatar asked Sep 28 '22 22:09

CephBirk


2 Answers

This isn't a direct answer, but I think that if it's that important for the user to see the message perhaps a warning is appropriate after all. Alternatively, you can pause the display to give the user a chance to read the message, either for a fixed amount of time:

foo=function(cars){
  message("pay attention to me!")
  Sys.sleep(2)
  return(cars)
}

or until the user confirms:

foo=function(cars){
  x <- readline("pay attention to me!\n(press enter to continue)")
  return(cars)
}
like image 163
Ista Avatar answered Oct 06 '22 20:10

Ista


Since Andrie's answer to a similar question of mine did not quite apply to data frames (it returned an empty frame), I've made a minor adjustment to print.bar() in that answer that seems to work well with data frames (so far at least).

foo <- function(x) {
    ## put all your function code here - 'x' will be the return value
    class(x) <- c("bar", class(x))
    x
}

print.bar <- function(x, message = TRUE, ...) {
    ## the only difference is the absence of this line
    NextMethod(x)
    if(message) message("I am a message, hear me ROAR!!")
}

tail(foo(mtcars))
#                 mpg cyl  disp  hp drat    wt qsec vs am gear carb
# Porsche 914-2  26.0   4 120.3  91 4.43 2.140 16.7  0  1    5    2
# Lotus Europa   30.4   4  95.1 113 3.77 1.513 16.9  1  1    5    2
# Ford Pantera L 15.8   8 351.0 264 4.22 3.170 14.5  0  1    5    4
# Ferrari Dino   19.7   6 145.0 175 3.62 2.770 15.5  0  1    5    6
# Maserati Bora  15.0   8 301.0 335 3.54 3.570 14.6  0  1    5    8
# Volvo 142E     21.4   4 121.0 109 4.11 2.780 18.6  1  1    4    2
# I am a message, hear me ROAR!!

As of this edit, I am having difficulty implementing the request in the comments below (no message printing under the object after assignment has been made and then object called). One possible workaround for this is to use suppressMessages() when assigning

obj <- suppressMessages(foo(mtcars))

So when calling in the console, foo() will print the message. When assigning obj, the message will not print when subsequently printing obj

like image 43
Rich Scriven Avatar answered Oct 06 '22 18:10

Rich Scriven