Home > Software design >  Are pipes causing a "argument is not interpretable as logical" error?
Are pipes causing a "argument is not interpretable as logical" error?

Time:11-05

I'm encountering a bit of an error I can't explain, I've got a bs_carousel using the bsplus library. A working version of it is of this pattern:

library(bsplus)
library(reactable)

bs_carousel("info") %>% bs_set_data(interval = FALSE) %>% 
  bs_append(bs_carousel_image(image1, if(checker1 == 1){reactable(df1[,!(names(df1) %in% c("Day"))])}))%>% 
  bs_append(bs_carousel_image(image2, if(checker2 == 1){reactable(df2[,!(names(df2) %in% c("Day"))])}))%>% 
  bs_append(bs_carousel_image(image3, if(checker3 == 1){reactable(df3[,!(names(df3) %in% c("Day"))])})) 

However I need to put another condition in that shows each "bs_append" when a condition is met:

bs_carousel("info") %>% bs_set_data(interval = FALSE) %>% 
      if(dim(df1)[1] != 0){bs_append(bs_carousel_image(image1, if(checker1 == 1){reactable(df1[,!(names(df1) %in% c("Day"))])}))}%>% 
      if(dim(df2)[1] != 0){bs_append(bs_carousel_image(image2, if(checker2 == 1){reactable(df2[,!(names(df2) %in% c("Day"))])}))}%>% 
      if(dim(df3)[1] != 0){bs_append(bs_carousel_image(image3, if(checker3 == 1){reactable(df3[,!(names(df3) %in% c("Day"))])}))} 

However for some reason, I get the following error:

Error in if (.) dim(df1)[1] != 0 else { : 
  argument is not interpretable as logical

I don't think it's a data issue because when I put the entire bs_carousel in the same condition, it runs fine (or at least correctly for that condition):

if(dim(df1)[1] != 0){
bs_carousel("info") %>% bs_set_data(interval = FALSE) %>% 
  bs_append(bs_carousel_image(image1, if(checker1 == 1){reactable(df1[,!(names(df1) %in% c("Day"))])}))%>% 
  bs_append(bs_carousel_image(image2, if(checker2 == 1){reactable(df2[,!(names(df2) %in% c("Day"))])}))%>% 
  bs_append(bs_carousel_image(image3, if(checker3 == 1){reactable(df3[,!(names(df3) %in% c("Day"))])}))}

Is my syntax wrong, or am I missing something more fundamental?

Thanks for any advice.

df1:

df1 <- structure(list(Date = structure(c(19305, 19306, 19311, 
19319), class = "Date"), Name = c("Green", 
"Blue18", "Yellow1", "Green14"
), Amount = c(NA_real_, NA_real_, 
NA_real_, NA_real_), Raised = c(NA_real_, 
NA_real_, NA_real_, NA_real_), Method = c("Not listed", 
"Not listed", "Search", "Not listed"), 
    Issue_Type = c("01", "01", "02", 
    "02"), Day = c(9, 10, 15, 23)), row.names = c(NA, 
-4L), class = c("tbl_df", "tbl", "data.frame"))

df2:

df2 <- structure(list(Date = structure(c(19327, 19332, 19333, 
19339), class = "Date"), Name = c("Red", 
"Blue18", "Blue18", "Green14"
), Amount = c(NA_real_, NA_real_, 
NA_real_, NA_real_), Raised = c(NA_real_, 
NA_real_, NA_real_, NA_real_), Method = c("Not listed", 
"Not listed", "See table below", "Not listed"), 
    Issue_Type = c("03", "02", "02", 
    "01"), Day = c(1, 6, 7, 13)), row.names = c(NA, 
-4L), class = c("tbl_df", "tbl", "data.frame"))

df3:

df3 <- structure(list(Date = structure(numeric(0), class = "Date"), 
    Name = character(0), Amount = numeric(0), 
    Cash_Raised = numeric(0), Method = character(0), 
    Issue_Type = character(0), Day = numeric(0)), row.names = integer(0), class = c("tbl_df", 
"tbl", "data.frame"))

checker1 <- 1
checker2 <- 1
checker3 <- 0

image1, image2, and image3 are all basically of similar formats, and a default image can be derived from:

library(png)
library(RCurl)
image3 <- readPNG(getURLContent("https://www.eas.org.uk/assets/images/logo-light.png"))
image2 <- image3
image1 <- image3

More info given because earlier question seemed to have been closed.

CodePudding user response:

Was able to come up with a somewhat unsophisticated solution:

if(dim(df1)[1] != 0 & dim(df2)[1] != 0 & dim(df3)[1] != 0){
bs_carousel("info") %>% bs_set_data(interval = FALSE) %>% 
  bs_append(bs_carousel_image(image1, if(checker1 == 1){reactable(df1[,!(names(df1) %in% c("Day"))])}))%>% 
  bs_append(bs_carousel_image(image2, if(checker2 == 1){reactable(df2[,!(names(df2) %in% c("Day"))])}))%>% 
  bs_append(bs_carousel_image(image3, if(checker3 == 1){reactable(df3[,!(names(df3) %in% c("Day"))])}))}

if(dim(df1)[1] != 0 & dim(df2)[1] != 0 & dim(df3)[1] == 0){
bs_carousel("info") %>% bs_set_data(interval = FALSE) %>% 
  bs_append(bs_carousel_image(image1, if(checker1 == 1){reactable(df1[,!(names(df1) %in% c("Day"))])}))%>% 
  bs_append(bs_carousel_image(image2, if(checker2 == 1){reactable(df2[,!(names(df2) %in% c("Day"))])}))}

if(dim(df1)[1] != 0 & dim(df2)[1] == 0 & dim(df3)[1] == 0){
bs_carousel("info") %>% bs_set_data(interval = FALSE) %>% 
  bs_append(bs_carousel_image(image1, if(checker1 == 1){reactable(df1[,!(names(df1) %in% c("Day"))])}))}
  • Related