PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, July 11, 2022

[FIXED] How can I test if a function returns a message

 July 11, 2022     message, r, rselenium, selenium     No comments   

Issue

BACKGROUND: In R the package "testit" (here) has both the functions has_warning and has_error but I'm looking for a function that returns a logical TRUE/FALSE if has_message.

WHY: to identify when a webElem$submitElement() from the RSelenium package returns an RSelenium message since the selenium messages are not classified as warnings or errors in R.

Is there a way to test if a function returns a message at all in R?

Ideally something like below:

#Ideally a function like this made up one:
has_message(message("Hello ","World!"))
[1] TRUE

has_message(print("Hello World!"))
[1] FALSE

Solution

You can use tryCatch:

has_message <- function(expr) {
  tryCatch(
    invisible(capture.output(expr)),
    message = function(i) TRUE
  ) == TRUE
}

has_message(message("Hello World!"))
# TRUE
has_message(print("Hello World!"))
# FALSE
has_message(1)
# FALSE

Evaluate expression within tryCatch with invisible(capture.output()) to suppress print or other output. We need final == TRUE to return FALSE when no message was present, otherwise for the last to examples there would be no output.



Answered By - pogibas
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing