gpt4 book ai didi

r - 如何在 wordpress 网站上嵌入图表?

转载 作者:行者123 更新时间:2023-12-04 04:11:13 26 4
gpt4 key购买 nike

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

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

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

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

谢谢!

最佳答案

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

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

获得 API key 后,您可以从 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
)

关于r - 如何在 wordpress 网站上嵌入图表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61691263/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com