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

如何在wordpress网站上嵌入绘图?,r,wordpress,plotly,r-plotly,R,Wordpress,Plotly,R Plotly,我正试图在wordpress网站上嵌入我在R中创建的绘图。这似乎比它应该做的要困难得多。也许我遗漏了一些显而易见的东西。以下是我尝试过的: 解决方案1:使用htmlwidgets::saveWidget(as\u widget(Basic\u expensing\u graph),file=“Basic\u expensing\u graph.html”)将图形保存为html。然后使用html将整个文件或html源代码嵌入到网站中。这种方法存在许多问题。首先,如果我嵌入文件,它会嵌入一个指向文件

我正试图在wordpress网站上嵌入我在R中创建的绘图。这似乎比它应该做的要困难得多。也许我遗漏了一些显而易见的东西。以下是我尝试过的:

解决方案1:使用
htmlwidgets::saveWidget(as\u widget(Basic\u expensing\u graph),file=“Basic\u expensing\u graph.html”)
将图形保存为html。然后使用html将整个文件或html源代码嵌入到网站中。这种方法存在许多问题。首先,如果我嵌入文件,它会嵌入一个指向文件的链接,您可以在其中打开页面,而不是在页面中完全嵌入图形。其次,该文件是3Mbs,随着时间的推移,可能会对使用共享主机的网站速度造成压力

解决方案2:将plotly图形从R导出到chart studio,这样您就可以在其服务器上托管图形并生成html嵌入片段。这似乎是一个很好的解决方案,但我正在努力找到一种从R导出到chart studio的简单方法,因为我已经花了很多时间在R中创建图表。显然有一种方法可以将图表导出到chart studio,但似乎没有人解释如何导出

考虑到plotly的设计理念是web,我可能错过了一些非常明显的东西!如有任何建议,将不胜感激。在网页上获取图表的最佳方式是什么


谢谢

解决方案1我帮不了你,但是re:solution 2,虽然这让人抓狂,但实际上有一个非常简单的解决方案

首先,您需要向chart studio online注册并生成api密钥。无论出于何种原因,要生成api密钥,您必须查看“设置”(请参见此处:)

获得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
)
#首先我们注册上传到plotly

你读过这个吗:嗨,谢谢你的回复。我读了这篇文章,但它似乎并没有涵盖如何将图形从r/r studio导出到chart studio。它只解释了如何嵌入已在chart studio中创建的图形。您是否尝试过此处介绍的github路线:或使用第10节中介绍的
htmlwidgets
离线版本: