Bug 15147 - Sys.which() misbehaves if NA values are input
Sys.which() misbehaves if NA values are input
Status: CLOSED FIXED
Product: R
Classification: Unclassified
Component: Misc
R 2.15.2
x86_64/x64/amd64 (64-bit) Linux
: P5 minor
Assigned To: R-core
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2012-12-19 15:17 UTC by Markus Göker
Modified: 2012-12-26 02:08 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Markus Göker 2012-12-19 15:17:01 UTC
According to the documentation, the return value of Sys.which() is a vector with the same length than the argument called 'names'. This does not work if NA values are input. Example:

x <- c(NA, "cat", NA, "ls", "echo")
Sys.which(x)

The expected output on UNIX would be approximately (some paths might differ):

c(NA, "/bin/cat", NA, "/bin/ls", "/bin/echo")

...with x used as its names. But the result is (on Ubuntu):

structure(c("", "/bin/cat", "", "/bin/ls", "/bin/echo", "", "", 
"", ""), .Names = c(NA, "cat", NA, "ls", "echo", NA, NA, NA, 
NA))

i.e. the NA values occur twice. The fix is probably easy: the loop should go through the vector indices, not the names. The according code is below, but note that some parts of the function (I suppose not those changed) are probably platform-specific.

Sys.which2 <- function (names)
{
    res <- character(length(names))
    names(res) <- names
    which <- "/usr/bin/which"
    if (!nzchar(which)) {
        warning("'which' was not found on this platform")
        return(res)
    }
    for (i in seq_along(names)) {
        ans <- suppressWarnings(system(paste(which, names[i]), intern = TRUE,
            ignore.stderr = TRUE))
        if (grepl("solaris", R.version$os)) {
            tmp <- strsplit(ans[1], " ", fixed = TRUE)[[1]]
            if (identical(tmp[1:3], c("no", names[i], "in")))
                ans <- ""
        }
        res[i] <- if (length(ans))
            ans[1]
        else ""
        if (!file.exists(res[i]))
            res[i] <- ""
    }
    res
}
Comment 1 Brian Ripley 2012-12-25 15:57:04 UTC
According the documentation, NA values are not valid.

Changed in R-patched to give an explicit error, and in R-devel to return NA.
Comment 2 Markus Göker 2012-12-26 02:08:02 UTC
(In reply to comment #1)
> According the documentation, NA values are not valid.
> 
> Changed in R-patched to give an explicit error, and in R-devel to return NA.

Thanks for the change, but I wonder about your claim regarding the documentation. As it seems to me, the current help message of Sys.which() does not even mention NA values.