如何在 WordPress 网站上嵌入绘图?

问题描述 投票:0回答:2

我正在尝试将我在 R 中创建的绘图嵌入到 WordPress 网站上。这似乎比应有的困难得多。也许我错过了一些明显的东西。这是我尝试过的:

解决方案 1:使用

htmlwidgets::saveWidget(as_widget(Basic_Spending_Graph), file = "Basic_Spending_Graph.html")
将图表保存为 html。然后使用该 html 将整个文件或 html 源代码嵌入到网站中。这种方法存在很多问题。首先,如果我嵌入文件,它会嵌入一个指向该文件的链接,您可以在其中打开页面,而不是将图形完全嵌入页面中。其次,该文件为 3 Mbs,随着时间的推移,这可能会对使用共享托管的网站速度造成压力。

解决方案 2:将绘图从 R 导出到图表工作室,这允许您在其服务器上托管图表并生成 html 嵌入片段。这似乎是一个很好的解决方案,但我正在努力寻找一种从 R 导出到图表工作室的简单方法,因为我已经花了很多时间在 R 中创建图表。显然有一种方法可以将图表导出到图表工作室,但似乎没有人解释怎么做?

考虑到plotly是根据网络设计的,我可能会遗漏一些非常明显的东西!任何建议将不胜感激。在网页上获取图表的最佳方式是什么?

谢谢!

r wordpress plotly r-plotly
2个回答
2
投票

我无法帮助您解决方案 1,但回复:解决方案 2,虽然这很难找到,但实际上有一个非常简单的解决方案。

首先,您需要在线注册 Chart Studio 并生成您的 api 密钥。无论出于何种原因,要生成 api 密钥,您需要查看您的“设置”。 (参见此处:https://chart-studio.plotly.com/settings/api

获得 api 密钥后,您可以从 R 运行以下代码,它将把您的绘图上传到您的个人资料。

#first we register to upload to plotly
key <- readLines("path/to/your/api/key")
Sys.setenv("plotly_username"="<your username>")
Sys.setenv("plotly_api_key"=key)

#now we post to plolty
plotly_POST(
  x = last_plot(),
  file = "<whatever you want to name your plot>",
  fileopt = "overwrite",
  sharing = c("public"),
  world_readable=TRUE
)

#note that evidently, plotly_POST is depreciated, though it worked for me as of 11/2020
#use instead the call below, with the same arguments
api_create(
  x = last_plot(),
  file = "<whatever you want to name your plot>",
  fileopt = "overwrite",
  sharing = c("public"),
  world_readable=TRUE
)

0
投票

inseri core允许这样的用例。 Plotly Chart Block 博客 允许您尝试具体示例(随意编辑 JSON)。

请参阅相应的文档:Plotly Chart如何将 Plotly 与 JSON 结合使用

© www.soinside.com 2019 - 2024. All rights reserved.