我正在尝试将其嵌入R
htmlwidget
到现有网页中 - 已经应用了引导程序和样式的网页.例如,请考虑以下网页(请注意应放置窗口小部件的位置):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<p>This is a test.</p>
<!-- htmlwidget should go here. -->
<p>A closing paragraph.</p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我可以datatable
像这样创建和保存小部件:
library(htmlwidgets)
library(datatable)
d1 <- datatable(mtcars, filter = "top")
saveWidget(d1, file = "widget_file.html")
Run Code Online (Sandbox Code Playgroud)
生成的widget_file.html
(即使是这个适度的小部件)包含很多代码.有没有一种简单的方法可以将其嵌入到现有的网页/模板中?
我成功使用<iframe src="widget_file.html">
但我想知道是否有更好的方法?此外,有没有办法将片段/依赖项(例如json
数据)分开,widget_file.html
以便将它们放在其他文件夹中?
注意:我创建了htmlwidget
标记,但我相信应该有一个同义htmlwidgets
标记.
如何在Rpres文件中包含绘图?如果你像在普通的Rmd文件中那样做
Basic Plot
========================================================
```{r, echo=FALSE}
library(plotly)
plot_ly(economics, x = date, y = unemploy / pop)
```
Run Code Online (Sandbox Code Playgroud)
我提出的解决方案,它使用了Markdown可以包含HTML的可能性:
Basic Plot
========================================================
```{r, results='hide', echo=FALSE}
library(plotly)
p = plot_ly(economics, x = date, y = unemploy / pop)
htmlwidgets::saveWidget(as.widget(p), file = "demo.html")
```
<iframe src="demo.html" style="position:absolute;height:100%;width:100%"></iframe>
Run Code Online (Sandbox Code Playgroud)
但我希望有一个更优雅的解决方案,不使用任何其他文件.
我有一个地图传单,我想保存在特定文件夹中的html文件中.我使用的是Windows 7.
我尝试了以下方法:
library(htmlwidgets)
saveWidget(map_leaflet, file="ressources/test.html")
library(htmlwidgets)
saveWidget(map_leaflet, file="ressources\\test.html")
library(htmlwidgets)
path_name <- file.path("ressources", "test.html", fsep="\\")
saveWidget(map_leaflet, file=path_name)
library(htmlwidgets)
path_name <- paste("ressources", "test.html", sep="/")
saveWidget(map_leaflet, file=path_name)
Run Code Online (Sandbox Code Playgroud)
作为错误消息,取决于Rstudio会话,我要么
1)setwd(dir)错误:无法更改工作目录
2)找不到路径
当我只保存这样:
library(htmlwidgets)
saveWidget(map_leaflet, file="test.html")
Run Code Online (Sandbox Code Playgroud)
它完美地运作.
预先感谢您的帮助.
当我点击节点以了解特定节点的整个故事时,我想强调整个路径,这里是一个例子 - http://bl.ocks.org/git-ashish/8959771.
请检查此链接,你会发现在javscript中突出显示路径的功能,但请注意,此功能不能达到我想要的效果,它突出显示与被点击的节点相关的链接以及与目标节点相关的链接.我想要的是突出显示与点击的节点相关的所有链接.
这是我需要的一个例子,
这是整个图,我需要的是,当我点击曼谷时,它突出显示与数据框中曼谷相同的所有节点,如突出显示与ClimateChange和EnergyShortage的链接,......然后突出显示基础设施和生态系统,和领导力和战略,......这就是我想要的.这是另一张图片,显示与曼谷相关的节点使用闪亮来分析它.
这是当我在bl.ocks和链接的问题中使用highlight_node_links时会发生什么,这是错误的,并没有显示节点和曼谷之间的关系.
以下是曼谷的数据,向您展示列如何相互关联,当您使用这些数据时,它将仅生成第二张图片.
structure(list(City = c("Bangkok", "Bangkok", "Bangkok", "Bangkok",
"Bangkok", "Bangkok", "Bangkok", "Bangkok", "Bangkok", "Bangkok",
"Bangkok", "Bangkok", "Bangkok", "Bangkok", "Bangkok", "Bangkok"
), ResiliencyChallenge = c("ClimateChange", "ClimateChange",
"ClimateChange", "ClimateChange", "ClimateChange", "InfrastructureFaliure",
"EnergyShortage", "Pollution", "Pollution", "Pollution", "TransportationSystemFailure",
"TransportationSystemFailure", "TransportationSystemFailure",
"TransportationSystemFailure", "TransportationSystemFailure",
"TransportationSystemFailure"), CRI.Dimesnsion.1 = c("Infrastructure & Ecosystems",
"Infrastructure & Ecosystems", "Infrastructure & Ecosystems",
"Infrastructure & Ecosystems", "Infrastructure & Ecosystems",
"Infrastructure & Ecosystems", "Infrastructure & Ecosystems",
"Leadership & Strategy", "Leadership & Strategy", "Infrastructure …
Run Code Online (Sandbox Code Playgroud) 我试图在R markdown()文件中ggplotly
从内部for
循环中绘制一系列交互式图形.Rmd
.我的.Rmd
文件内容:
---
title: "Untitled"
output: html_document
---
```{r}
library(ggplot2) # for plots
library(plotly) # for interactive plots
# Convert 4 variables to factor variables:
factor_vars <- c("vs", "am", "gear", "carb")
mtcars[factor_vars] <- data.frame(Map(as.factor, mtcars[factor_vars]))
for (VAR in factor_vars) {
cat(paste("Factor variable:", VAR))
# Contents of "VAR" changes inside the loop
p <- ggplot(mtcars, aes_string(x = "mpg", y = "wt", color = VAR)) + geom_point()
# Print an interactive plot
print(ggplotly(p))
} …
Run Code Online (Sandbox Code Playgroud) ++++++++++++++++
更新: 我认为我的问题的答案是你不能放入换行符.一位同事向我指出节点标签是SVG块,它不支持换行符.
++++++++++++++++
如何为使用networkD3 R软件包生成的sankey图的节点标签添加换行符?
借用将文本值放置到sankey图右侧的示例,我可以为标签添加值:
library(networkD3)
library(data.table)
set.seed(1999)
links <- data.table(
src = rep(0:4, times=c(1,1,2,3,5)),
target = sample(1:11, 12, TRUE),
value = sample(100, 12)
)[src < target, ] # no loops
nodes <- data.table(name=LETTERS[1:12])
#### Need to hover to get counts
##sankeyNetwork(Links=links, Nodes=nodes, Source='src', Target='target',
## Value='value', NodeID='name', fontSize=16)
## Add text to label
txt <- links[, .(total = sum(value)), by=c('target')]
nodes[txt$target+1L, name := paste0(name, ' (', txt$total, ')')]
## Displays the counts as part of …
Run Code Online (Sandbox Code Playgroud) 该networkD3
包(见这里和这里)允许用户创建简单的互动网络:
# Load package
library(networkD3)
# Create fake data
src <- c("A", "A", "A", "A",
"B", "B", "C", "C", "D")
target <- c("B", "C", "D", "J",
"E", "F", "G", "H", "I")
networkData <- data.frame(src, target)
# Plot
simpleNetwork(networkData)
Run Code Online (Sandbox Code Playgroud)
有没有办法指定我希望src
矢量中的所有元素都是某种颜色,同时允许target
矢量中的所有元素都是不同的颜色?这将允许我在视觉上区分src
节点和target
网络中的节点.
目前似乎不支持此功能simpleNetwork()
(但我希望有人可以帮助我使用自制程序脚本):
这里提出了一个类似但不相关的问题.
当我[WARNING] Deprecated: --self-contained. use --embed-resources --standalone
使用htmlwidgets::saveWidget(..., selfcontained = TRUE)
.
我不知道如何使用“嵌入资源”或“独立”作为函数中的参数。这只是最近开始的,想知道是否有其他人发生过此警告,或者知道为此使用的正确参数。更新了 RStudio 和软件包,但警告仍然存在。
使用 R 4.2.1、RStudio 2022.07.2+576“斑点 Wakerobin”、htmlwidgets
1.5.4。
library(plotly)
library(htmlwidgets)
fig <- plot_ly(x = 1:10, y = 1:10, type = "scatter", mode = "lines")
htmlwidgets::saveWidget(partial_bundle(fig), file = "plotly.html", selfcontained = TRUE)
utils::browseURL("plotly.html")
Run Code Online (Sandbox Code Playgroud) 试图在R的networkD3
包的帮助下制作一个相当通用的Sankey图.仅供参考 - 这是包装手册中的示例
library(networkD3)
library(jsonlite)
library(magrittr)
energy <- "https://cdn.rawgit.com/christophergandrud/networkD3/master/JSONdata/energy.json" %>%
fromJSON
sankeyNetwork(Links = energy$links,
Nodes = energy$nodes,
Source = "source",
Target = "target",
Value = "value",
NodeID = "name",
units = "TWh",
fontSize = 12,
nodeWidth = 30)
Run Code Online (Sandbox Code Playgroud)
这导致:
我相当简单的扩展包括使用以下基础数据构建图表:
links <- structure(list(source = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 4L,
5L, 4L, 5L),
.Label = c("1", "2", "3", "4", "5"),
class = "factor"),
target = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 4L,
4L),
.Label = …
Run Code Online (Sandbox Code Playgroud) 由于每个对象,我用的时候获得在Firefox中非常小的情节sankeyNetwork()
从networkd3在闪亮,但无法在Chrome或RStudio.
我没有在脚本中包含任何CSS或JS - 下面的代码为我生成了这个结果.
我错过了什么CSS属性吗?
我使用的是R 3.4.1,闪亮的1.1.0,networkD3 0.4和Firefox 52.9.0.
library(shiny)
library(magrittr)
library(shinydashboard)
library(networkD3)
labels = as.character(1:9)
ui <- tagList(
dashboardPage(
dashboardHeader(
title = "appName"
),
##### dasboardSidebar #####
dashboardSidebar(
sidebarMenu(
id = "sidebar",
menuItem("plots",
tabName = "sPlots")
)
),
##### dashboardBody #####
dashboardBody(
tabItems(
##### tab #####
tabItem(
tabName = "sPlots",
tabsetPanel(
tabPanel(
"Sankey plot",
fluidRow(
box(title = "title",
solidHeader = TRUE, collapsible = TRUE, status = "primary",
sankeyNetworkOutput("sankeyHSM1")
)
)
) …
Run Code Online (Sandbox Code Playgroud)