Suppress warning message in R console of shiny - r

We developed a shiny application. It was showing some warning messages, we never bothered it, because the application is working fine. But, we can't distribute the application with warning message appearing in the console. Now my question is, how to suppress the warning message in R console, when shiny application is running.

Insert this in your ui script.
tags$style(type="text/css",
".shiny-output-error { visibility: hidden; }",
".shiny-output-error:before { visibility: hidden; }"
)

You actually have two suppress functions via R that work differently for you:
suppressMessages() evaluates its expression in a context that ignores all ‘simple’ diagnostic messages." See ?suppressMessages.
suppressWarnings() evaluates its expression in a context that ignores all warnings. See ?suppressWarnings.
Examples:
f <- function(a) { a ; warning("This is a warning") ; message("This is a message not a warning")}
> f(1)
This is a message not a warning
Warning message:
In f(1) : This is a warning
> suppressWarnings(f(1))
This is a message not a warning
> suppressMessages(f(1))
Warning message:
In f(1) : This is a warning

Wrapping suppressWarnings around your code should work. See ?suppressWarnings. You need to do something like:
atest <- function(n) {warning("a warning"); return(n+1)}
atest(1)
#[1] 2
#Warning message:
#In atest(2) : a warning
suppressWarnings(atest(1))
#[1] 2
But I guess that the better solution is to actually deal with the warning and not just ignore it.

Related

R: What causes "In eval(ei, envir) : NAs introduced by coercion"?

I sometimes get the following warning in my R code output:
Warning message:
In eval(ei, envir) : NAs introduced by coercion
This happens only when running my R code with R --slave --file=/path/to/sourcecode.R --args arg1 arg2 arg3 arg4 or Rscript /path/to/sourcecode.R arg1 arg2 arg3 arg4 but not when running the code interactively (not even if I run it in an interactive R session on a cluster node).
I cannot provide example code to reproduce the problem because this is a large project with a lot of code spread across several files, and I'm not sure what specific code or circumstances are triggering it.
Googling the error, I found references to an error message referencing "eval(ei, envir)" that happens when you call source from inside of a function . The message I get is different - the post in the link is an error about not being able to find a variable, mine is a warning about NAs being introduced by coercion. So it's probably not the same problem, but I suspect it is somehow still related to using source(), because my code also uses source().
If I knew what "ei" was and why using source() apparently involves calling eval(ei,envir), that might help me figure out what exactly (an environment variable? which one?) has something in it that would trigger an "NAs introduced by coercion" message... does anyone have any ideas what might be going on here?
I have figured out what was causing this. Since Google searches did not find any exact matches for this message, I'm making a Q&A-style post in hopes it will help the next person to Google this particular error.
The warning happens when a variable or other object created in your main code contains values that get coerced to NA in code that you have loaded via source(). Here is some example code to cause it:
test1.R:
print("Starting test1.R")
x <- c('3','bicycle','5')
source('test2.R')
print('Finished test1.R.')
test2.R:
print("Starting test2.R")
y <- as.numeric(x)
print("Finished test2.R")
The output of Rscript test1.R is:
[1] "Starting test1.R"
[1] "Starting test2.R"
[1] "Finished test2.R"
Warning message:
In eval(ei, envir) : NAs introduced by coercion
[1] "Finished test1.R."
Note that even though the line that triggers the error happens in the middle of the code loaded with source(), you don't see the warning in your output until after the code loaded by source() has finished.
You also don't have to be running in batch mode for it to happen. Here I use source() in interactive mode to cause the same message:
> x <- c('3','bicycle','5')
> source('test2.R')
[1] "Starting test2.R"
[1] "Finished test2.R"
Warning message:
In eval(ei, envir) : NAs introduced by coercion
So why was this only happening in batch mode in my original problem? It turned out to be a red herring... when I ran my code in interactive mode, I created a vector to simulate command-line arguments, like this: args <- c('/path/to/infile','27',NA,'0.1'). But when my code was running in batch mode, it created the vector "args" like this: args <- commandArgs(trailingOnly=T). And an NA passed as a command line argument is not a real NA, it is the string 'NA'. So as.numeric('NA') coerces 'NA' to NA. But the argument was only 'NA' instead of NA when running in batch mode.
Those two quirks combined were enough to cause a troubleshooting nightmare that cost me a whole evening. Hopefully this Q&A-style post will save the next person to Google this error message some time!

Is there a feature to remove error message

Following code in R shiny application (Server side) gives the result as shown below. But can we make this error message disable? Like we do no need any message to be shown. Can we achieve this?
options(shiny.sanitize.errors = TRUE)
Result in Console (In Red)
Error : An error has occurred. Check your logs or contact the app author for clarification
Expected result :
or
"No table" (The user can type anything he needs?)
As mentioned by #nopassport1, the best option is to prevent the error in the first place.
Alternatively, on a case-by-case basis you can remove the error warning by wrapping code in a try(x,silent = TRUE) function.
> try(1+"a")
Error in 1 + "a" : non-numeric argument to binary operator
> try(1+"a",silent = TRUE)
>

Additionnal warning message returns an error instead of a warning

I have two functions, f_ that throws an error and f that throws a warning before calling f_.
f_ <- function() stop()
f <- function() {
warning()
f_()
}
Since I have a warning before the error, R produces "additionnal warning messages", but the message in this warning is not my f warning but the error produced in f_ called a 2nd time :
> f()
Error in f_() :
In addition: Warning message:
In f() :
Error in f_() :
It seems to works as expected if the error is produced in the same function or by a built_in function.
f <- function() {
warning()
stop()
}
> f()
Error in f() :
In addition: Warning message:
In f() :
Can someone helps me to understand what is happening there ?
Thanks for any help.
I'm running R version 3.3.2 on x86_64-w64-mingw32 using RStudio.
I think this is caused by the Rstudio error inspector. When encountering an error Rstudio displays the possibility for traceback and debugging. I believe that is the source of the confusion (my own included). The "second" error is simply a feature in Rstudio which assists in debugging as seen below. Note the two buttons on the right allowing you to "show traceback" and "rerun with debug".
In Rstudio
As you can see below, if you run R in a terminal,
this "additional" error is not there.
In a terminal
In your global options in Rstudio, under General tab, you can turn off the use of the debug error handler. You can also do this under Debug -> On Error.
Rstudio will then not display the "additional" message.
Edit:
Upon investigating a bit further, there is something odd going on though. Below, I tried to make the error and warning message a bit more informative with the following observations:
Calling f() many times in a row, it is not entirely clear to me when the error inspector appears and when it does not.
When the error inspector does appear, the warning message is not displayed. When the error inspector does not appear, the warning message is displayed.
I do not know anything about Rstudio's internals, but it is quite definitely the error inspector causing these minor issues.

testthat: handling both warning and value

What's the best way to handle calls that generate a warning but then also return a value?
e.g.
> require(testthat)
> expect_warning(log(-1))
> expect_equal(log(-1), NaN)
Warning message:
In log(-1) : NaNs produced
I want to write the test such that the call to log(-1) should both (a) generate a warning and (b) return the value NaN. The way above works, but seeing the "Warning message:" at the bottom might confuse people. Should I suppress warnings temporarily?
require(testthat)
expect_warning(val <- log(-1))
expect_true(is.nan(val))

Error in running the gWidgets2Qt demo

I've got this error while running the demo of the gWidgets2Qt package:
> demo(gWidgets2Qt)
demo(gWidgets2Qt)
---- ~~~~~~~~~~~
Type <Return> to start :
> ## run examples
> require(gWidgets2)
> options(guiToolkit="Qt")
> ## run examples
> source(system.file("examples", "run_examples.R", package="gWidgets2"))
Error in envRefSetField(x, what, refObjectClass(x), selfEnv, value) :
‘.visible’ is not a field in class “GWindow”
please check also this question I've just asked for session info and a similar error I've got with the cranvas package which I think might be related to the above. Thanks a lot.
EDIT:
following the tips from #jverzani I tried a simple code which worked. Then I did some tests:
I get this when detaching the package
detach("package:gWidgets2Qt", unload=TRUE)
There were 50 or more warnings (use warnings() to see the first 50)
> warnings()
Warning messages:
1: In FUN(X[[2L]], ...) :
Created a package name, ‘2013-05-23 07:40:37’, when none found
Tried to re-load and run the demo but still didn't work
I restarted ubuntu and tried again
library(gWidgets2Qt)
demo(gWidgets2Qt)
it run correctly, I only get error with the ex-graphics.R example
which fails to run with this error at the first attempt:
Error in qsceneDevice(width, height, pointsize, family, the_scene) :
unused argument (the_scene)
In addition: Warning message:
In .removePreviousCoerce(class1, class2, where, prevIs) :
methods currently exist for coercing from “AlternativeSingleEnum” to “character”; they will be replaced.
Error in qinvoke(<environment>, "initScene", ...) :
Implementation failed for method 'R::gWidgets2Qt::QtDevice::initScene'
and this one at the next attmpts:
Error in qsceneDevice(width, height, pointsize, family, the_scene) :
unused argument (the_scene)
Error in qinvoke(<environment>, "initScene", ...) :
Implementation failed for method 'R::gWidgets2Qt::QtDevice::initScene'
But all the other examples work. However, as soon as I load cranvas, with
> library(cranvas)
Attaching package: ‘cranvas’
The following object is masked from ‘package:gWidgets2’:
visible, visible<-
demo(gWidgets2Qt) fails again and detaching cranvas
> detach("package:cranvas", unload=TRUE)
There were 50 or more warnings (use warnings() to see the first 50)
> warnings()
Warning messages:
1: In FUN(X[[2L]], ...) :
Created a package name, ‘2013-05-23 08:37:43’, when none found
demo(gWidgets2Qt) runs again. Has been this incompatibility already seen? Is this depending on invisible being masked from gWidgets2?
I'm not sure why this isn't working. I just installed the whole thing (qtbase, qtutils, gWidgets2, gWidget2Qt) on a linux setup and the demo starts. The ones involving graphs don't really work, but the basic demo does. To see if everything is working, try with something simple:
w <- gwindow("something simple")
b <- gbutton("click me", container=w)
addHandlerChanged(b, handler=function(h,...) {
gmessage("Hello world", parent=w)
})
If that doesn't work then there are installation issues

Resources