Just met a weird situation that ifelse
changes the result.
My intention is to decide if the image is load properly, if yes, stay what it is, if not, load the image, assuming the path is correct.
but it doesn''t work. The result becomes a single value, instead of image expected.
Please find the toy sample for better picture.
library(imager)
# save boats in JPG form
save.image(boats, file = "boatsJPG.jpg")
# test -1
im <- boats
class(im)
#[1] "cimg" "imager_array" "numeric"
im = ifelse(any(class(im) %in% "cimg"), im, boats)
class(im)
#[1] "numeric"
# test -2
im <- "boatsJPG.jpg"
class(im)
#[1] "character"
im = ifelse(any(class(im) %in% "cimg"), im, load.image(im))
class(im)
#[1] "numeric"
On the other hand, the sample works
im <- boats
if(!any(class(im) %in% "cimg")) {im <- load.image(im)}
class(im)
#[1] "cimg" "imager_array" "numeric"
im <- "boatsJPG.jpg"
if(!any(class(im) %in% "cimg")) {im <- load.image(im)}
class(im)
#[1] "cimg" "imager_array" "numeric"
How come? Please advise.
question from:https://stackoverflow.com/questions/66051571/weird-that-ifelse-is-not-always-the-simple-form-of-if