簡體   English   中英

如何在 wordpress 網站上嵌入 plotly 圖?

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

我正在嘗試在 wordpress 網站上嵌入我在 R 中創建的 plotly 圖。 這似乎比它應該的要困難得多。 也許我錯過了一些明顯的東西。 這是我嘗試過的:

解決方案 1:使用htmlwidgets::saveWidget(as_widget(Basic_Spending_Graph), file = "Basic_Spending_Graph.html")將圖形保存為 html 。 然后使用 html 將整個文件或 html 源代碼嵌入網站。 這種方法存在許多問題。 首先,如果我嵌入文件,它會嵌入一個指向文件的鏈接,您可以在其中打開頁面,而不是將圖形完全嵌入頁面中。 其次,該文件為 3 Mbs,隨着時間的推移,可能會對使用共享主機的網站速度造成壓力。

解決方案 2:將 plotly 圖形從 R 導出到圖表工作室,它允許您在其服務器上托管圖形並生成 html 嵌入片段。 這似乎是一個很好的解決方案,但我正在努力尋找一種從 R 導出到圖表工作室的簡單方法,因為我已經花了很多時間在 R 中創建圖表。 顯然有一種方法可以將圖表導出到圖表工作室,但似乎沒有人解釋如何?

考慮到 plotly 的設計考慮到 web,我可能會遺漏一些非常明顯的東西。 任何建議將不勝感激? 在網頁上獲取 plotly 圖表的最佳方法是什么?

謝謝!

我無法為您提供解決方案 1,但請回復:解決方案 2,雖然這很令人抓狂,但實際上有一個非常簡單的解決方案。

首先,您需要在線注冊圖表工作室並生成您的 api 密鑰。 無論出於何種原因,要生成您的 api 密鑰,您可以查看您的“設置”。 (見這里: https://chart-studio.plotly.com/settings/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
)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM