简体   繁体   中英

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. 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") . Then use that html to either embed the whole file or html source code into the website. 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.

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. 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. 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. Any advice would be greatly appreciated? What is the best way of getting plotly charts on the webpage?

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.

First, you need to register with chart studio online and generate your api key. For whatever reason, to generate your api key you to view your "settings." (see here: 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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