rstudio / plumber

Turn your R code into a web API.
https://www.rplumber.io
Other
1.39k stars 256 forks source link

Example with htmlwidget #205

Closed mgahan closed 6 years ago

mgahan commented 6 years ago

Hello there,

I just want to say this is an awesome package! Very useful. I was curious if you have an example of how to build a function to call an htmlwidget? There is some documentation on it but I am unsure how to get it to work.

#' @get /hello
#' @htmlwidget
function(){
  library(dygraphs)
  lungDeaths <- cbind(mdeaths, fdeaths)
  out <- dygraph(lungDeaths)
  htmlwidgets::saveWidget(out, "out.html")
  return(out)
}
mgahan commented 6 years ago

Closing since there is an answer already in this repo (sorry). https://github.com/trestletech/plumber/issues/79

#' Plot out data from the iris dataset
#' @get /hello
#* @serializer htmlwidget
function(){
  library(dygraphs)
  lungDeaths <- cbind(mdeaths, fdeaths)
  out <- dygraph(lungDeaths)
  print(out)
}
trestletech commented 6 years ago

I noticed that the documentation here was wrong. I just fixed it in https://github.com/trestletech/apis-plumber/commit/b2da0597e2e7edfbe96e8534cbc4f5b89cb74377 (should be updated momentarily)

It might be nice to have a section in the docs the shows an example with the print() trick as that trips a lot of people up. FYI here's a blog post on that topic, for anyone curious. https://yihui.name/en/2017/06/top-level-r-expressions/