Thursday, 29 March 2018

Beauties of R: isNumeric

So, in R apparently there is no function in the core packages to test if a string would work out as a number.

There are really unappealing workarounds, like:
https://stackoverflow.com/questions/16194212/how-to-suppress-warnings-globally-in-an-r-script
This uses global settings (options(warn=-1), bonus points for the magic number), risking failing to recover in case of erroneous situations, potentially affecting other threads unwittingly/unintentionally as part of a larger scale quest, etc.


Probably the slightly more laycoder-minded need to lay out their homemade witchery for this maybe with a regex. Or use something slightly external e.g. limma from the bioconductor repository. (And then ... there is a function called isNumeric. How ... why wouldn't you subconsciously confuse that with is.numeric? Cautious language design could come to the rescue ...)

Seriously? Is it the time when non-mathsy JS's isNaN() functionality clearly beats R?? :D No way. What am I missing?

Anyway, just for the huge fun here's my quick regex solution...

> test.values = c("5", "56", "5.67", ".67e+30")
> grepl("^([+-]?)((\\d+)|(\\d*[.]\\d+))([eE]([+-])?\\d+)?$", test.values)
[1] TRUE TRUE TRUE TRUE


Update: suppressWarnings(), for instance. I did overlook that one. However, what you get there is suppression of all warnings, not only the one you wanted to suppress. Typically, the range of warnings is not defined by the documentation of the function guarded by this function... so you never know what you got rid of.

Seems like a built in function would be a beneficial addition.


Thursday, 22 March 2018

Python console progressbar "???"

1%^M142/6456


Hence, 142 / 6456 < 0.02.

Good old Python (???) progressbar .. broke the mold, eh? Don't want to know ;)