简体   繁体   English

如何在 wordpress 网站上嵌入 plotly 图?

[英]How to embed a plotly graph on a wordpress website?

I am trying to embed a plotly graph I created in R on a wordpress website.我正在尝试在 wordpress 网站上嵌入我在 R 中创建的 plotly 图。 It seems a lot more difficult than it should.这似乎比它应该的要困难得多。 Perhaps I am missing something obvious.也许我错过了一些明显的东西。 Here is what I tried:这是我尝试过的:

solution 1: saved the graph as an html using htmlwidgets::saveWidget(as_widget(Basic_Spending_Graph), file = "Basic_Spending_Graph.html") .解决方案 1:使用htmlwidgets::saveWidget(as_widget(Basic_Spending_Graph), file = "Basic_Spending_Graph.html")将图形保存为 html 。 Then use that html to either embed the whole file or html source code into the website.然后使用 html 将整个文件或 html 源代码嵌入网站。 There are numerous problems with this approach.这种方法存在许多问题。 Firstly if I embed file it embeds a link to the file where you can open the page rather than fully embedding the graph within the page.首先,如果我嵌入文件,它会嵌入一个指向文件的链接,您可以在其中打开页面,而不是将图形完全嵌入页面中。 Secondly the file is 3 Mbs, which over time could put a strain on website speed which is using shared hosting.其次,该文件为 3 Mbs,随着时间的推移,可能会对使用共享主机的网站速度造成压力。

solution 2: export plotly graphs from R to chart studio which allows you to host graph on their server and generate an html embedding snippet.解决方案 2:将 plotly 图形从 R 导出到图表工作室,它允许您在其服务器上托管图形并生成 html 嵌入片段。 This seems like a great solution, but I am struggling to find an easy way to export from R to chart studio, since I already spent quite a lot of time creating the graphs in R.这似乎是一个很好的解决方案,但我正在努力寻找一种从 R 导出到图表工作室的简单方法,因为我已经花了很多时间在 R 中创建图表。 Apparently there is a way to export the charts to chart studio, but nobody seems to explain how?显然有一种方法可以将图表导出到图表工作室,但似乎没有人解释如何?

I may be missing something very obvious considering plotly was designed with web in the mind.考虑到 plotly 的设计考虑到 web,我可能会遗漏一些非常明显的东西。 Any advice would be greatly appreciated?任何建议将不胜感激? What is the best way of getting plotly charts on the webpage?在网页上获取 plotly 图表的最佳方法是什么?

Thanks!谢谢!

I can't help you with solution 1, but re: solution 2, while this is maddening to find, there is actually a pretty simple solution.我无法为您提供解决方案 1,但请回复:解决方案 2,虽然这很令人抓狂,但实际上有一个非常简单的解决方案。

First, you need to register with chart studio online and generate your api key.首先,您需要在线注册图表工作室并生成您的 api 密钥。 For whatever reason, to generate your api key you to view your "settings."无论出于何种原因,要生成您的 api 密钥,您可以查看您的“设置”。 (see here: https://chart-studio.plotly.com/settings/api ) (见这里: https://chart-studio.plotly.com/settings/api

Once you've got your api key, you can the following code from R and it will upload your plotly chart to your profile.获得 api 密钥后,您可以从 R 中输入以下代码,它将您的 plotly 图表上传到您的个人资料。

#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
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM