Where can I find a log of R session warnings? - r

One of my R scripts produced a message that there are some warnings during processing. However, since this is not an interactive session, I can't use warnings() to access the warnings. What is the standard location, if any, of the most recent R session's warnings log file, so that I could review them? Thank you!

From ?warnings:
It is undocumented where last.warning is stored nor that it is
visible, and this is subject to change.
However, you can use the function warnings in your script and specify a file, where the last warnings should be saved.
warnings(file = "C:/Rwarnings.txt")

Related

libstableR library unwanted output

I'm using libstableR library to calculate some estimators and function stable_fit_mle outputs some messages while running, namely tons of Minimizer warning: No error and occasionally also: FINAL ESTIMATED PARAMETER ARE NOT VALID: No error.
I managed to suppress those messages in my final document (compiled with R Sweave) with invisible(capture.output(...)) but those still clutter my console during compilation. Is there any way to stop those messages from appearing in the first place?
My main concern is to understand why stable_fit_mle is outputing these warnings (in oppose to similrar functions from libstableR package) and how to prevent this behaviour.

R: Why does fs::dir_copy fail some of the time?

During a process, I need to copy a directory using fs::dir_copy(currentPath, newPath). Some of the time, I get the following error message:
Error in link_copy(links, path(new_path[[i]], path_rel(links, path[[i]])), :
all(is_link(path)) is not TRUE
This only happens some of the time, not always. What's more, if I replace my directory with a manual copy of itself (i.e. manually copy directory, delete original, rename the copy), then my code will work.
Could someone please explain why this could be happening? Is there a way I can sidestep that error once and for all?
This does not answer your question but may help you to further analyse what is going on. One of the great things of R is that you can easily inspect source code. Especially in a situation where unusual things appear to happen, looking at the code may be useful.
In your case, we can inspect the source code of fs::dir_copy and trace back to which code generates the error message. If you type fs::dir_copy (no parenthesis) into the console, R will print the R code of the function (if the function is not primitive). Doing that here will show you that the fs:dir_copy function calls the fs::link_copyfunction. Makes sense, as the error message comes from this function.
We can now print this function with fs::link_copy
This function generates the error message in this line:
stopifnot(all(is_link(path)))
From the error message we know that all(is_link(path)) returns FALSE. Next step is to have a look at the fs::is_link function. Here we see that the error may come from the call to the setNames function, which depends on the fs::file_info function: res <- file_info(path)
Here we see that setNames is called with a condition depending on what the file_infofunction returned:
setNames(!is.na(res$type) & res$type == "symlink", res$path)
This looks unusual as setNames takes an object and a character vector as formals. But then, I am not the developer of these functions ;-)
Perhaps there are issues with the file type on your machine and fs::file_info(path) returns something unexpected under some conditions.

Delayed warnings in R

I had an odd experience regarding warnings when I used R, version 3.5.0. The warnings come out after I already finished data manipulation. A while later - about 10 minutes later, the warnings for one data set appeared when I was manipulating another data set. At first, I thought I made some mistakes I was not aware of, and re-did everything. However, this kept happening.
So, I again re-did everything, and specifically typed warnings() after I finished the part that the warnings pointed to, and I got nothing, meaning there was no warnings. As expected, a while later, when I am manipulating some other data, the exactly same warnings come out!
Did anybody else encounter this also?
Thanks a lot!
Under ?options you see:
warn:
sets the handling of warning messages. If warn is negative all warnings are ignored. If warn is zero (the default) warnings are stored until the top–level function returns. If 10 or fewer warnings were signalled they will be printed otherwise a message saying how many were signalled. An object called last.warning is created and can be printed through the function warnings. If warn is one, warnings are printed as they occur. If warn is two or larger all warnings are turned into errors.
So basically by default the warnings don't print until the top level function returns. If R thinks that a function is not done (a plotting function maybe) then it will wait to issue the warnings. Try entering options(warn=1) and re-running the code to see where the issue is.

Create my own warning message outside a function?

This should be simple based on posts like this but somehow I cannot get it to work. What is wrong with this example?
x<-1
y<-0
if(x>y){warning("careful, one is greater than zero!")}
It works with stop():
if(x>y){stop("careful, one is greater than zero!")}
So either I'm making a simple syntax mistake or warning is not supposed to be used outside of functions?
Your code works fine with me. I'm using R 3.3.2.
I think a possibile solution to your problem is to check if warning messages are enabled in your session.
If you read ?options, you'll notice that between the values returned by the function there is the warn value.
From the reference:
warn:
sets the handling of warning messages. If warn is negative all warnings are ignored. If warn is zero (the default) warnings are stored until the top–level function returns. If 10 or fewer warnings were signalled they will be printed otherwise a message saying how many were signalled. An object called last.warning is created and can be printed through the function warnings. If warn is one, warnings are printed as they occur. If warn is two or larger all warnings are turned into errors.
So, if you have a negative value for warn, you won't see warning messages.
You can enable warning messages in the following way:
options(warn=1)
Try changing this and re-run your code.

Can I check if roxygenize failed?

Is it possible to detect if there was a problem when running roxygenize (package roxygen2)?
I want to automate the process documenting, checking and building a package, and would like to stop when documenting goes wrong.
The roxygenize help says the return value is NULL, and I searched stackoverflow without success. Currently, I need to look at the output and search if there was a line starting with "Error".
Any hint appreciated!
When roxygenize finds an error, for example, you included stop("Raise an error") in your code, then roxygenize will return an error.
The other scenario (which is what you are getting at), is that roxygenise is able to finish, but some aspects of the documentation process are incorrect. In this case, these errors are stored as warnings. So one solution is to change warnings to errors.
For example, suppose you had a file containing the line:
#' #XXX
This would cause:
roxygenise("pkg/")
to raise a warning
Warning: XXX is an unknown key in block AllGenerics.R:5
If we changed warnings to errors:
##All warnings are now errors
options(warn=2)
Then
roxygenise("pkg/")
would raise the error:
Error: (converted from warning) XXX is an unknown key in block AllGenerics.R:5
You can then use the standard tryCatch technique for dealing with errors.

Resources