【问题标题】:R viewer pane is not showing D3 interactive graphsR 查看器窗格未显示 D3 交互式图表
【发布时间】:2020-12-28 02:14:31
【问题描述】:

我在最新版本的 R 和 R studio(昨天下载)中使用 bipartiteD3 包。

当我从 bipartite_D3 函数包运行示例命令和示例文件时,它没有在查看器窗格中显示交互式二分图。相反,查看器窗格中会出现一条错误消息,指出:

d3.nest 不是函数

我附上了带有代码和错误的 R studio 页面的屏幕截图。

【问题讨论】:

  • 请阅读Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers? - 总结是这不是解决志愿者的理想方式,并且可能会适得其反。请不要将此添加到您的问题中。
  • 非常抱歉。对于使用非推荐的短语/单词,我深表歉意。实际上,我下周必须在我们大学介绍我的博士论文概要。因此,我将这个问题称为“紧急”,因为我需要在 2-3 天内准备好我的演示文稿,其中将包括上面提到的交互式图表。我认为紧迫性将取决于面临问题的人的情况以及他/她需要解决问题的时间。

标签: r d3.js rstudio bipartite


【解决方案1】:

最新版本的 d3 不再包含 d3-collection 模块中包含的功能。看起来 r2D3 现在默认使用这个版本 (d3v6),但 bipartiteD3 仍然使用它。这意味着它无法访问 d3.nest(),这解释了您的错误,因为 d3.nest 不再是 d3 的方法。

BipartiteD3 可以相对容易地更新,但同时有一些选项可以纠正这个问题,例如,您可以执行以下操作之一:

  1. 安装旧版本的 r2d3 以替换当前版本

例如:

packageurl <- "https://cran.r-project.org/src/contrib/Archive/r2d3/r2d3_0.2.3.tar.gz"
install.packages(packageurl, repos=NULL, type="source")

或

  1. 在本地编辑 r 包源文件以将正确的版本传递给 r2d3(使用 d3_version = "5" 完成)

或

  1. 创建一个包含所有bipartite_D3 的新函数,同时传递一个版本参数(同样使用d3_version = "5" 完成,与#2 基本相同,但不修改bipartiteD3 源)

例如:

library(r2d3)
library(bipartite )
library(purrr) 
library(dplyr) 
library(tidyr) 
library(stringr)
library(tibble)
library(RColorBrewer)
library(bipartiteD3)


testdata <- data.frame(higher = c("bee1","bee1","bee1","bee2","bee1","bee3"), 
                       lower = c("plant1","plant2","plant1","plant2","plant3","plant4"), 
                       webID = c("meadow","meadow","meadow","meadow","meadow","meadow"), freq=c(5,9,1,2,3,7))
bipartite::frame2webs(testdata)-> SmallTestWeb

Used3v5<- 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()
  
  # only line changed here to add d3_version = "5"
  r2d3::r2d3(data = NA, d3_version="5", script = paste0(filename,".js"),
             height= MainFigSize[2], width= MainFigSize[1] ,
             dependencies ="vizjs.js")
}

Used3v5(SmallTestWeb)

【讨论】:

  • 完成了!非常感谢你!我没有把你提供的所有代码;刚刚安装了旧版本的 r2d3 (0.2.3) 并尝试运行现有的命令行。它运行良好!再次感谢您!
  • @SupratimLaha 很高兴能提供帮助 - 替换 r2d3 的版本可能是三者中最简单的选择。祝你的演讲顺利。
【解决方案2】:

你加载了所有的包吗?文档建议

library(r2d3)
library(bipartite)
library(purrr) 
library(dplyr) 
library(tidyr) 
library(stringr)
library(tibble)
library(RColorBrewer)
library(bipartiteD3)

https://cran.r-project.org/web//packages/bipartiteD3/vignettes/bipartiteD3_Intro.html

【讨论】:

  • 感谢您的回复。是的,即使按照帮助文件链接中给出的顺序,我也已经加载了所有包。另外,我的 PC 中有最新的 Java 软件。我只是被困在这里!
  • 二分法的其他部分是否有效?比如bipartite::plotweb(SmallTestWeb$meadow),可能问题就出在“demo1”上。
  • 是的,其他部分工作正常。只有当函数 bipartite_D3 运行时,它才会显示错误消息而不是图形可视化。
【解决方案3】:

我刚刚安装了旧版本的 r2d3 (0.2.3),它完美地显示了 R 查看器窗格中的交互式二分图。 这里我附上截图。 Here is the screenshot of R studio showing the commands and graph 谢谢@Andrew Reid

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-06
    • 2019-12-20
    • 1970-01-01
    • 2019-01-27
    • 1970-01-01
    • 1970-01-01
    • 2011-12-13
    • 2016-10-04
    相关资源
    最近更新 更多