Closed philibe closed 6 years ago
Without being able to run the code I can't say for sure, but the issue might be with your use of any
. I believe you should get the right result if you either:
||
with ,
in your any
any
and use the |
OR operator rather than ||
logical ORThat last one is a common issue I see with R code. You can read more about the difference here but in short ||
only looks at the first element of the vector, and if you ask me, should never be used.
IMHO it's maybe a R feature, or bug R feature, but it's not a problem of any
or |
or ||
(I've tried too) : I've tried a function from a package I have a the same bug.
It seems that with every non built-in function there is this bug.
df_dep$tooltip <- [3]paste0(
[2] ifelse( [1]!any(is.null(df_dep$my_var ) || is.na(df_dep$my_var ) ||is.nan(df_dep$my_var ) || (df_dep$my_var =="")) ,
paste0("<br>my_var :", df_dep$my_var ),
""
)
)
foo<- function(x) { all functionnality you want}
df_dep$tooltip <- [3] paste0(
[2] ifelse([1]!foo(df_dep$my_var) ,
paste0("<br>my_var:", df_dep$my_var),
""
)
)
But tooltip
structure is a sort of callback. It's maybe the cause of this strange thing. Maybe all calls out of tooltip
are not computed in this sort of callback.
Nota Bene: [1]
,[2]
,[3]
are not real code, but labels for my explanation.
Is foo
a vectorized function in your example? Would help if you posted a code example I could run
My real example was the function estvide()
.
I've found the package I tested as an alternative at my function : gtools. Found in this message in Stackoverflow.
I have the same problem with gtools::invalid()
than my estvide()
.
edit
library("gtools")
.....
df_dep$tooltip <- paste0(
ifelse(!gtools::invalid(df_dep$my_var) ,
paste0("<br>my_var:", df_dep$my_var),
""
)
And I bet it's the same problem with functions far way of empty tests.
(I cannot for the moment make a self-ruling code.)
I think I've found the problem : ifelse
doesn't work in that case oO
Sorry :)
My self-ruling code:
library(shiny)
library("collapsibleTree")
app<-
shinyApp(
ui = basicPage(
collapsibleTreeOutput("tree")
),
server = function(input, output) {
org <- data.frame(
Manager = c(
NA, "Ana", "Ana", "Bill", "Bill", "Bill", "Claudette", "Claudette", "Danny",
"Fred", "Fred", "Grace", "Larry", "Larry", "Nicholas", "Nicholas"
),
Employee = c(
"Ana", "Bill", "Larry", "Claudette", "Danny", "Erika", "Fred", "Grace",
"Henri", "Ida", "Joaquin", "Kate", "Mindy", "Nicholas", "Odette", "Peter"
),
Title = c(
"President", "VP Operations", "VP Finance", "Director", "Director", "Scientist",
"Manager", "Manager", "Jr Scientist", "Operator", "Operator", "Associate",
"Analyst", "Director", "Accountant", "Accountant"
),
stringsAsFactors = FALSE
)
estvide <- function(x) { any(is.null(x) || is.na(x) ||is.nan(x) || (x=="")) }
ifelsephil <- function(x,y,z) {if (x) y else z }
output$tree <- renderCollapsibleTree({
org$tooltip <- #!any(is.null(org$Employee ) , is.na(org$Employee ) ,is.nan(org$Employee ), (org$Employee ==""))
paste0(
# #1) if...else works
# if(
# #!any(is.null(org$Employee ) || is.na(org$Employee ) ||is.nan(org$Employee ) || (org$Employee ==""))
# !estvide(org$Employee)
# )
# paste0("<br>my_var :", org$Employee )
# else
# ""
#2)ifelse() bultin function doesn't work oO ! Ana is everywhere
#ifelse(!estvide(org$Employee), paste0("<br>my_var :", org$Employee ),"")
#3)ifelsephil() works
ifelsephil(!estvide(org$Employee), paste0("<br>my_var :", org$Employee ),"")
)
ctree<-
collapsibleTreeNetwork(
org,
tooltipHtml = "tooltip",
collapsed = FALSE
)
return(ctree)
})
}
)
shiny::runApp(app)
Is it a normal R feature or a collapsibleTree bug ?
This works, eachs values are corrects in all leaves:
This doesn't work, the same value is dispatched in all leaves: