AdeelK93 / collapsibleTree

Create Interactive Collapsible Tree Diagrams in R using D3.js
https://adeelk93.github.io/collapsibleTree/
158 stars 41 forks source link

Strange things in tooltip with sub function (tooltip leaves are identicals) #35

Closed philibe closed 6 years ago

philibe commented 6 years ago

Is it a normal R feature or a collapsibleTree bug ?

This works, eachs values are corrects in all leaves:

df_dep$tooltip <- paste0(
  ifelse(!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  ),
    ""
  )
)

This doesn't work, the same value is dispatched in all leaves:

estvide <- function(x) { any(is.null(x) || is.na(x) ||is.nan(x) || (x=="")) }
df_dep$tooltip <- paste0(
  ifelse(!estvide (df_dep$my_var) ,
    paste0("<br>my_var:", df_dep$my_var),
    ""
  )
)
AdeelK93 commented 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:

That 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.

philibe commented 6 years ago

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.

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.

AdeelK93 commented 6 years ago

Is foo a vectorized function in your example? Would help if you posted a code example I could run

philibe commented 6 years ago

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.)

philibe commented 6 years ago

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)