【发布时间】:2019-03-04 16:18:05
【问题描述】:
我的 blogdown 网站(包括 plots、markdown 等)通常在 Chrome 和 Firefox 上都能正常显示。顺便说一句,它们通常在 IE 上也能正常工作,尽管我不太关心那个浏览器。为了完整起见,我将把它包括在讨论中。
当我使用 networkD3 R 包在 blogdown 中包含 Sankey Network 时,Chrome 和 IE 中的内容会“正确”呈现,但在 Firefox 中则不然。 Firefox 人为地缩小了 Sankey Network 的大小,见下文:
这是我正在使用的代码。在将 Sankey Networks 与 blogdown 一起使用时,我可以做些什么来让 Sankey Network 在 Firefox 上正确呈现?
我确实搞砸了{r, fig.width=x, fig.height=y}。增加x 和y 会增加整体图像大小,同时将整体图像保持在相同的“小”博客下拉框中,有效地减小Sankey Network 的大小,甚至比上面显示的还要大。减小 x 和 y 只会减小图像大小,同时也会使 Sankey 网络 比上面显示的要小。我想我需要解决渲染问题(Firefox 中存在,Chrome 中不存在)。
---
title: "Data Analysis"
date: "2019-01-01T18:00:00-09:00"
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(blogdown)
library(networkD3)
```
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
```{r sankey, echo=FALSE, error=FALSE, message=FALSE, warning=FALSE}
source <- c("A", "A", "B", "C", "D", "D", "E", "E")
target <- c("D", "E", "E", "D", "H", "I", "I", "H")
values <- c(1, 22, 5, 5, 5, 10, 10, 10)
nodes <- data.frame(name = unique(c(source, target)))
links <- data.frame(source = match(source, nodes$name) - 1,
target = match(target, nodes$name) - 1,
value = values)
sankeyNetwork(Links = links, Nodes = nodes, Source = "source",
Target = "target", Value = "value", NodeID = "name",
units = "unitX", fontSize = 12, nodeWidth = 20)
```
[编辑] 我已经更新了我的代码,如下所示,based on this previous SO question. 但是,它仍然呈现“小”,没有明显变化:(
---
title: "Data Analysis"
date: "2019-01-01T18:00:00-09:00"
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(blogdown)
library(networkD3)
```
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
```{r sankey, echo=FALSE, error=FALSE, message=FALSE, warning=FALSE}
source <- c("A", "A", "B", "C", "D", "D", "E", "E")
target <- c("D", "E", "E", "D", "H", "I", "I", "H")
values <- c(1, 22, 5, 5, 5, 10, 10, 10)
nodes <- data.frame(name = unique(c(source, target)))
links <- data.frame(source = match(source, nodes$name) - 1,
target = match(target, nodes$name) - 1,
value = values)
sn <- sankeyNetwork(Links = links, Nodes = nodes, Source = "source",
Target = "target", Value = "value", NodeID = "name",
units = "unitX", fontSize = 12, nodeWidth = 20)
htmlwidgets::onRender(sn, 'document.getElementsByTagName("svg")[0].setAttribute("viewBox", "")')
# also tried this to no avail
# htmlwidgets::onRender(sn, 'document.getElementById().getElementsByTagName("svg")[0].setAttribute()')
```
【问题讨论】:
-
@CJ Yetman 感谢您的提示,我试过了,但没有任何改变。您能否快速查看我的新代码块(使用更新编辑了原始帖子)并确保我正确应用了您的建议?我认为我做对了。还根据您的 GitHub 链接将此作为错误提交。
-
它适用于 macOS 10.14.3、Firefox 65.0.1
-
我刚刚更新了我所有的 R 包。 FF v65.0.2 仍然无法正常工作。我的电脑是 Win7 Pro 和 Ubuntu 18.04。无法想象它与操作系统相关(但也许)。我们尝试了。谢谢。我只是建议用户使用 Chrome。
-
这也发生在我身上
标签: r r-markdown blogdown htmlwidgets networkd3