R:将多个html小部件保存在一起 [英] R: saving multiple html widgets together

查看:74
本文介绍了R:将多个html小部件保存在一起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用R编程语言.我对学习如何保存多个"html窗口小部件"感兴趣.一起.我已经能够手动创建不同类型的html小部件:

  #widget 1图书馆(htmlwidgets)图书馆(传单)库(RColorBrewer)#创建地图数据map_data<-data.frame(拉提"= c(43.6426、43.6424、43.6544、43.6452、43.6629),"Longi"表示= c(-79.3871,-79.3860,-79.3807,-79.3806,-79.3957),工作"= c(经济学家",经济学家",老师",老师",律师"),"First_Name"= c("John","James","Jack","Jason","Jim"),姓氏"= c(史密斯",查尔斯",亨利",大卫",罗伯特"),车辆"= c(汽车",厢式货车",汽车",无",汽车"))王国-c(经济学家",律师",老师")my_palette<-brewer.pal(3,"Paired")factpal<-colorFactor(my_palette,等级=王国)组<-唯一(map_data $ Job)#完成地图地图<-传单(map_data)%>%addTiles(group ="OpenStreetMap")%>%addCircleMarkers(〜Longi,〜Lati,popup =〜Job,半径= 10,重量= 2,不透明度= 1,颜色=〜factpal(作业),fill = TRUE,fillOpacity = 1,组=〜作业)widget_1 =地图%>%addLayersControl(overlayGroups =组,选项= layersControlOptions(折叠=假))%&%addTiles()%&%;%addMarkers(lng =〜Longi,lat =〜Lati,popup =〜paste("Job",Job,< br>","First_Name:",First_Name,< br>",姓氏:",姓氏,"br",车辆",车辆,"br"))) 

小工具2:

  #####小部件2图书馆(密谋)库(ggplot2)p_plot<-data.frame(频率= c(rnorm(31,1),rnorm(31)),is_consumed = factor(round(runif(62))))p2<-p_plot%&%;%ggplot(aes(频率,填充= is_consumed))+geom_density(alpha = 0.5)widget_2 = ggplotly(p2) 

小工具3:

  ##### widget_3今天<-Sys.Date()tm <-seq(0,600,by = 10)x<-今天-tmy<-rnorm(长度(x))widget_3<-plot_ly(x =〜x,y =〜y,mode ='lines',text = paste(tm,'days from today')) 

小工具4:

  #### widget_4图书馆(igraph)图书馆(dplyr)图书馆(visNetwork)Data_I_Have<-data.frame("Node_A"= c("John","John","John","Peter","Peter","Peter","Tim","Kevin","Adam","Adam").亚当",泽维尔"),"Node_B";= c(克劳德",彼得",蒂姆",蒂姆",克劳德",亨利",凯文",克劳德",蒂姆",亨利",克劳德"))graph_file<-data.frame(Data_I_Have $ Node_A,Data_I_Have $ Node_B)colnames(graph_file)<-c("Data_I_Have $ Node_A","Data_I_Have $ Node_B")图<-graph.data.frame(graph_file,向= F)图<-简化(图)节点<-data.frame(id = V(graph)$ name,title = V(graph)$ name)节点<-节点[order(nodes $ id,减少= F),]边缘<-get.data.frame(graph,what ="edges")[1:2]widget_4 = visNetwork(节点,边)%>%visIgraphLayout(layout ="layout_with_fr")%&%visOptions(highlightNearest = TRUE,nodesIdSelection = TRUE) 

从这里,我发现了另一个stackoverflow帖子,其中询问了类似的问题:

但是,我正在使用无法访问Internet或USB端口的计算机.该计算机具有带有限库的R的预装副本(它具有在我的问题中使用的所有库,"manipulateWidget"除外).我正在寻找将多个html小部件保存在一起的最简单方法(例如,在base R中是否可能)?

谢谢

解决方案

如果格式不是太重要,则可以使用tagList合并小部件并直接保存它们:

  htmltools :: save_html(tagList(widget_1,widget_2,widget_3,widget_4),file ="C://Users//Me//Desktop//widgets.html") 

(不用说,您将需要编辑文件路径!)

如果要控制小部件的布局,可以将每个小部件包装在div中,然后设置其样式:

  doc<-htmltools :: tagList(div(widget_1,style ="float:left; width:50%;"),div(widget_2,style ="float:left; width:50%;"),div(widget_3,style ="float:left; width:50%;"),div(widget_4,style ="float:left; width:50%;"))htmltools :: save_html(html = doc,file ="C://Users//Me//Desktop//widgets.html") 

I am using the R programming language. I am interested in learning how to save several "html widgets" together. I have been able to manually create different types of html widgets:

#widget 1
library(htmlwidgets)
library(leaflet)
library(RColorBrewer)

# create map data
map_data <- data.frame(
  "Lati" = c(43.6426, 43.6424, 43.6544, 43.6452, 43.6629), "Longi" = c(-79.3871, -79.3860, -79.3807, -79.3806, -79.3957),
  "Job" = c("Economist", "Economist", "Teacher", "Teacher", "Lawyer"),
  "First_Name" = c("John", "James", "Jack", "Jason", "Jim"),
  "Last_Name" = c("Smith", "Charles", "Henry", "David", "Robert"),
  "vehicle" = c("car", "van", "car", "none", "car")
)

kingdom <- c("Economist", "Lawyer", "Teacher")
my_palette <- brewer.pal(3, "Paired")
factpal <- colorFactor(my_palette, levels = kingdom)

groups <- unique(map_data$Job)

# finalize map
map <- leaflet(map_data) %>%
  addTiles(group = "OpenStreetMap") %>% 
  addCircleMarkers(~Longi, ~Lati, popup = ~Job,
                   radius = 10, weight = 2, opacity = 1, color = ~factpal(Job),
                   fill = TRUE, fillOpacity = 1, group = ~Job
  )

widget_1 = map %>%
  addLayersControl(overlayGroups = groups, options = layersControlOptions(collapsed = FALSE)) %>%
  addTiles() %>%
  addMarkers(lng = ~Longi, 
             lat = ~Lati, 
             popup = ~paste("Job", Job, "<br>", 
                            "First_Name:", First_Name, "<br>", 
                            "Last_Name:", Last_Name, "<br>", "vehicle:", vehicle, "<br>"))

widget 2:

##### widget 2

library(plotly)
library(ggplot2)

p_plot <- data.frame(frequency = c(rnorm(31, 1), rnorm(31)),
                     is_consumed = factor(round(runif(62))))
p2 <- p_plot %>%
  ggplot(aes(frequency, fill = is_consumed)) +
  geom_density(alpha = 0.5)     

widget_2 = ggplotly(p2)

widget 3:

#####widget_3

today <- Sys.Date()
tm <- seq(0, 600, by = 10)
x <- today - tm
y <- rnorm(length(x))
widget_3 <- plot_ly(x = ~x, y = ~y, mode = 'lines', text = paste(tm, "days from today"))

widget 4:

####widget_4

library(igraph)
library(dplyr)
library(visNetwork)


Data_I_Have <- data.frame(
   
    "Node_A" = c("John", "John", "John", "Peter", "Peter", "Peter", "Tim", "Kevin", "Adam", "Adam", "Xavier"),
    "Node_B" = c("Claude", "Peter", "Tim", "Tim", "Claude", "Henry", "Kevin", "Claude", "Tim", "Henry", "Claude")
)


graph_file <- data.frame(Data_I_Have$Node_A, Data_I_Have$Node_B)


colnames(graph_file) <- c("Data_I_Have$Node_A", "Data_I_Have$Node_B")

graph <- graph.data.frame(graph_file, directed=F)
graph <- simplify(graph)


nodes <- data.frame(id = V(graph)$name, title = V(graph)$name)
nodes <- nodes[order(nodes$id, decreasing = F),]
edges <- get.data.frame(graph, what="edges")[1:2]

widget_4 = visNetwork(nodes, edges) %>%   visIgraphLayout(layout = "layout_with_fr") %>%
    visOptions(highlightNearest = TRUE, nodesIdSelection = TRUE)

From here, I found another stackoverflow post where a similar question was asked: Using R and plot.ly, how to save multiples htmlwidgets to my html?

In this post, it explains how to save several html widgets together - the person who answered the question wrote a function to do so:

library(htmltools)
save_tags <- function (tags, file, selfcontained = F, libdir = "./lib") 
{
  if (is.null(libdir)) {
    libdir <- paste(tools::file_path_sans_ext(basename(file)), 
                    "_files", sep = "")
  }
  htmltools::save_html(tags, file = file, libdir = libdir)
  if (selfcontained) {
    if (!htmlwidgets:::pandoc_available()) {
      stop("Saving a widget with selfcontained = TRUE requires pandoc. For details see:\n", 
           "https://github.com/rstudio/rmarkdown/blob/master/PANDOC.md")
    }
    htmlwidgets:::pandoc_self_contained_html(file, file)
    unlink(libdir, recursive = TRUE)
  }
  return(htmltools::tags$iframe(src= file, height = "400px", width = "100%", style="border:0;"))
}

I tried using this function to save the 4 widgets together:

save_tags(widget_1, widget_2, widget_3, widget_4)

But doing so, I got the following error:

 Error in dirname(file) : a character vector argument expected 

Is there a straightforward and simple way for saving multiple html widgets together?

Thanks

NOTE: I know that you can use the combineWidgets() function in R:

library(manipulateWidget)
combineWidgets(widget_1, widget_2, widget_3, widget_4)

However, I am working with a computer that has no internet access or USB ports. This computer has a pre-installed copy of R with limited libraries (it has all the libraries used throughout my question except "manipulateWidget"). I am looking for the simplest way to save multiple html widgets together (e.g. is this possible in base R)?

Thanks

解决方案

If format doesn't matter too much, you can merge the widgets using tagList and save them directly:

htmltools::save_html(tagList(widget_1, widget_2, widget_3, widget_4), file = "C://Users//Me//Desktop//widgets.html")

(It goes without saying that you will need to edit the filepath!)

If you want to control the layout of the widgets, you can wrap each in a div, and then style those:

doc <- htmltools::tagList(
  div(widget_1, style = "float:left;width:50%;"),
  div(widget_2,style = "float:left;width:50%;"),
  div(widget_3, style = "float:left;width:50%;"),
  div(widget_4, style = "float:left;width:50%;")
)

htmltools::save_html(html = doc, file = "C://Users//Me//Desktop//widgets.html")

这篇关于R:将多个html小部件保存在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆