使用闪亮时输出sankey图不正确

时间:2014-03-18 14:28:24

标签: r shiny rcharts sankey-diagram

当我在常规R会话中创建一个sankey图时,输出看起来没问题。工具提示在连接之间显示一个箭头:

require(rCharts)
require(rjson)

links <- matrix(unlist(
  rjson::fromJSON(
    file = "http://bost.ocks.org/mike/sankey/energy.json"
  )$links
),ncol = 3, byrow = TRUE)
nodes <- unlist(
  rjson::fromJSON(
    file = "http://bost.ocks.org/mike/sankey/energy.json"
  )$nodes
)

links <- data.frame(links)
colnames(links) <- c("source", "target", "value")
links$source <- sapply(links$source, FUN = function(x) {return(as.character(nodes[x+1]))}) #x+1 since js starts at 0
links$target <- sapply(links$target, FUN = function(x) {return(nodes[x+1])}) #x+1 since js starts at 0
sankeyPlot <- rCharts$new()
sankeyPlot$setLib('http://timelyportfolio.github.io/rCharts_d3_sankey')
sankeyPlot$set(
  data = links,
  nodeWidth = 15,
  nodePadding = 10,
  layout = 32,
  width = 960,
  height = 500,
  units = "TWh",
  title = "Sankey Diagram"
)
sankeyPlot

enter image description here

当我在shiny中创建它时,工具提示中的箭头将被异常字符替换。在图表下方还会打印一个不寻常的字符。我需要下载d3_sankey库以使闪亮的应用程序版本正常工作,因此如果要重现它,则必须更改setLib语句中的路径。如何解决这个问题?

require(shiny)

runApp(list(
  ui = pageWithSidebar(
    headerPanel('Test'),
    sidebarPanel(  'Test'  ),
    mainPanel(
      chartOutput("Plot", 'C:/R-3.0.1/library/rCharts/libraries/sankey')  
    )
  ),
  server = function(input, output, session){

    output$Plot <-  renderChart2({
      sankeyPlot2 <- rCharts$new()
      sankeyPlot2$setLib('C:/R-3.0.1/library/rCharts/libraries/sankey')
      sankeyPlot2$set(
    data = links,
    nodeWidth = 15,
    nodePadding = 10,
    layout = 32,
    width = 960,
    height = 500,
    units = "TWh",
    title = "Sankey Diagram"
      )
      return(sankeyPlot2)
     })
  }
))

enter image description here

> sessionInfo()
R version 3.0.2 (2013-09-25)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=Dutch_Belgium.1252  LC_CTYPE=Dutch_Belgium.1252   
[3] LC_MONETARY=Dutch_Belgium.1252 LC_NUMERIC=C                  
[5] LC_TIME=Dutch_Belgium.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] shiny_0.8.0.99 rjson_0.2.13   rCharts_0.4.2 

loaded via a namespace (and not attached):
 [1] bitops_1.0-6    caTools_1.16    digest_0.6.4    grid_3.0.2     
 [5] httpuv_1.2.1    lattice_0.20-23 plyr_1.8        Rcpp_0.10.6    
 [9] RCurl_1.95-4.1  RJSONIO_1.0-3   tools_3.0.2     whisker_0.3-2  
[13] xtable_1.7-1    yaml_2.1.10    

1 个答案:

答案 0 :(得分:1)

问题与多个文件中的字符编码有关。这就是我在Windows 7机器上解决问题的方法。

  1. 鼠标悬停工具提示问题
  2. 箭头字符用于构建源和目标之间的“链接”。它出现在这些文件中:

    example_build_network_sankey.html
    layouts\chart.html
    layouts\chart_static_title.html
    layouts\chart.html
    

    将箭头替换为ASCii字符 - &gt; 所以代码看起来像:

    .text(function (d) { return d.source.name + " -> " + d.target.name + "\n" + format(d.value); });
    
    1. 图表下方的字符
    2. 发现于:

      \libraries\highlighters\prettify\css\sunburst.css
      \layouts\chart.html
      \libraries\widgets\d3_sankey\layouts\chart.html
      

      我使用UltraEdit中的Search and Replace in Files工具将这个特殊字符替换为空格。这个很棘手,因为我在UE编辑器中看不到该字符。如果我突出显示空白区域,则会显示为反引号。该字符也可以在jquery-1.8.2.min.js中找到。