说我有两个 htmlwidget

# Load energy projection data
# Load energy projection data
library(networkD3)
URL <- paste0(
        "https://cdn.rawgit.com/christophergandrud/networkD3/",
        "master/JSONdata/energy.json")
Energy <- jsonlite::fromJSON(URL)
# Plot
sankeyNetwork(Links = Energy$links, Nodes = Energy$nodes, Source = "source",
             Target = "target", Value = "value", NodeID = "name",
             units = "TWh", fontSize = 12, nodeWidth = 30)


library(leaflet)
data(quakes)

# Show first 20 rows from the `quakes` dataset
leaflet(data = quakes[1:20,]) %>% addTiles() %>%
  addMarkers(~long, ~lat, popup = ~as.character(mag))

我想将它们并排放置在 html 页面中。我怎样才能做到这一点?我可以使用 iframe 吗?其他?

最佳答案

有很多方法可以回答这个问题。通常,大小和定位会因 htmlwidget 的作者而异,因此您可能需要进行一些试验。如果您不打算使用带有网格助手的 CSS 框架,最简单的方法是将每个 htmlwidget 包装在 tags$div() 中并使用 CSS 。您可能还对来自 RStudio http://github.com/rstudio/flexdashboard 的非常好的基于 flexbox 的仪表板包感兴趣。

# Load energy projection data
# Load energy projection data
library(networkD3)
URL <- paste0(
  "https://cdn.rawgit.com/christophergandrud/networkD3/",
  "master/JSONdata/energy.json")
Energy <- jsonlite::fromJSON(URL)
# Plot
sn <- sankeyNetwork(Links = Energy$links, Nodes = Energy$nodes, Source = "source",
              Target = "target", Value = "value", NodeID = "name",
              units = "TWh", fontSize = 12, nodeWidth = 30,
              width = "100%")
library(leaflet)
data(quakes)

# Show first 20 rows from the `quakes` dataset
leaf <- leaflet(data = quakes[1:20,]) %>% addTiles() %>%
  addMarkers(~long, ~lat, popup = ~as.character(mag))


library(htmltools)
browsable(
  tagList(list(
    tags$div(
      style = 'width:50%;display:block;float:left;',
      sn
    ),
    tags$div(
      style = 'width:50%;display:block;float:left;',
      leaf
    )
  ))
)

关于htmlwidgets 并排在 html 中?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35950710/

10-12 20:12