jcdterry / bipartiteD3

Repository for R package bipartiteD3
GNU General Public License v2.0
0 stars 0 forks source link

bug with bipartite_D3 function #3

Closed iremendoza closed 2 years ago

iremendoza commented 2 years ago

I am trying to reproduce the example from the vignette and there is a bug with the bipartite_D3 function.

bipartite_D3(SmallTestWeb)

The error that appears is: "Error: d3.nest is not a function in (bipartiteD3Script.js#-2:3999) TypeError: d3.nest is not a function"

The command for sourcing the URL is not working too.

I have installed the last version of RStudio (2021.09.0 Build 351), R (v4.1.1), and Java 8 updated on July 2021.

I have seen that the last update of the package is old, from 2019. Indeed, I used this function with the same computer as now in 2020 and it worked out, so I guess that there must be an issue with the new releases of associated software or packages.

I appreciate any recommendation or correction of the bug. Best, Irene

jcdterry commented 2 years ago

Thanks for this. We solved the problem over email, but will repeat here for visibility. The issue is due to an update to D3 that removed d3.nest after version 6. I will get it updated on CRAN as soon as I can, but the solution is to force r2d3() to use version 5 of d3 [penultimate line]:

bipartite_D3<- function(data,
                       filename='bipartiteD3Script',
                       PrimaryLab = 'Primary',
                       SecondaryLab='Secondary',
                       SiteNames=NULL,
                       colouroption=c('monochrome', 'brewer', 'manual')[1],
                       HighlightLab = 'Unlinked',
                       HighlightCol = '#3366CC',
                       monoChromeCol = 'rgb(56,43,61)',
                       ColourBy = c(1, 2)[2],
                       BrewerPalette = 'Accent',
                       NamedColourVector,
                       MainFigSize = NULL,
                       SortPrimary=NULL,
                       SortSecondary=NULL,
                       mp = c(1,1),
                       MinWidth=10,
                       Pad=1,
                       IndivFigSize= c(200, 400),
                       BarSize = 35,
                       Orientation = c('vertical', 'horizontal')[1],
                       EdgeMode = c('straight','smooth' )[2],
                       BoxLabPos = NULL,
                       IncludePerc = TRUE,
                       PercentageDecimals =0,
                       PercPos = NULL,
                       CSS_Output_Supress = FALSE,
                       PRINT=FALSE){
  df<-NULL

  if(tibble::is_tibble(data)){
    data<- as.data.frame(data)
  }

  if(is.data.frame(data)){
    df<- data
  }else{
    if(is.list(data)){
      df<-List2DF(data, PrimaryLab,SecondaryLab,SiteNames)
    }else{
      dimensions<-length(dim(data))
      if(dimensions ==3){
        df<-Array2DF(data, PrimaryLab,SecondaryLab,SiteNames)
      }
      if(dimensions==2){
        df<-Matrix2DF(data,PrimaryLab,SecondaryLab, SiteNames)
      }
    }
  }
  if(is.null(df)){#
    stop('invalid data input. Valid forms are data frame, bipartite style matrix, list or array')
  }

  if((mp[2]*mp[1])> (ncol(df)-2)){
    warning('Making too many facets. Are you sure mp is set ok?')
  }
  if((mp[2]*mp[1])< (ncol(df)-2)){
    warning('Making too few facets. Guessing you want 1 row')
    mp[2]<-ncol(df)-2
  }

  if(is.null( MainFigSize)){
    MainFigSize<-c(mp[2]*700, mp[1]*700) # Having a reasonable guess at a default overall figure size
  }

  BP_JS_Writer(df = df,filename = filename,
               colouroption = colouroption,
               HighlightLab = HighlightLab,
               HighlightCol = HighlightCol,
               monoChromeCol = monoChromeCol,
               ColourBy = ColourBy,
               BrewerPalette = BrewerPalette,
               NamedColourVector = NamedColourVector,
               MainFigSize = MainFigSize ,
               SortPrimary = SortPrimary,
               SortSecondary = SortSecondary,
               mp = mp,
               MinWidth = MinWidth,
               Pad = Pad,
               IndivFigSize = IndivFigSize,
               BarSize = BarSize ,
               Orientation = Orientation,
               EdgeMode = EdgeMode,
               AxisLabels= c(PrimaryLab, SecondaryLab),
               FigureLabel = SiteNames,
               BoxLabPos = BoxLabPos,
               IncludePerc = IncludePerc ,
               PercentageDecimals = PercentageDecimals,
               PercPos = PercPos,
               CSS_Output_Supress = CSS_Output_Supress ,
               PRINT =  PRINT)

  LoadVisJS()

  r2d3::r2d3(data = NA, script = paste0(filename,".js"),
         height= MainFigSize[2], width= MainFigSize[1] , d3_version = '5',
       dependencies ="vizjs.js")
}
jcdterry commented 2 years ago

v.0.3.0 submitted to CRAN