Suppose the contents of my plumber.r file are as follows below. I can successfully test each endpoint by visiting the path using chrome and viewing the expected output except for the \plotly endpoint.
When visiting that in my browser (i.e., http://localhost:8000/plotly) I get "An exception error occurred". I am not using R Studio, I am launching this from an RGui session using
pr("plumber.R") %>% pr_run(port=8000)
Any advice on where my error is?
###plumber.R
library(plotly)
#* Plot a histogram
#* @serializer png
#* @get /plot
function() {
rand <- rnorm(100)
hist(rand)
}
#* Return the sum of two numbers
#* @param a The first number to add
#* @param b The second number to add
#* @get /sum
function(a, b) {
as.numeric(a) + as.numeric(b)
}
#* Echo back the input
#* @get /dat
function() {
data.frame(id = c(1,2,3,4), v2 = c(5,6,7,8))
}
#* @apiTitle HTML widgets API#* @apiTitle HTML widgets API
#* Return interactive plot using plotly
#* @serializer htmlwidget
#* @get /plotly
function() {
fig <- plot_ly(data = iris, x = ~Sepal.Length, y = ~Petal.Length)
fig
}
I am doing this with Vanilla RGui 4.1.3 and not with the RStudio IDE. So, I manually installed Pandoc 2.17.1.1. Following the advice of @jpiversen below, I did the following.
I first manually use setwd() to the location where Pandoc is locally installed. I needed this because the htmlwidgets:::find_pandoc()
does not have an argument for the path, it scans the directory. After setting the working directory, then run
htmlwidgets:::find_pandoc()
htmlwidgets:::.pandoc$version
htmlwidgets:::.pandoc$dir
This solved the issue and I can now view the plotly endpoint.
The error message:
<simpleError in htmlwidgets::saveWidget(val, tmpfile, selfcontained = TRUE, ...): Saving a widget with selfcontained = TRUE requires pandoc. For details see: github.com/rstudio/rmarkdown/blob/master/PANDOC.md>
seems to correspond to the function plumber::serializer_htmlwidget()
where its calling htmlwidget::saveWidget()
.
In the source code for htmlwidget::saveWidget()
we can see that the excact error message is triggered by !pandoc_available()
.
htmlwidget::pandoc_available()
differs from rmarkdown::pandoc_available()
, and somehow it is not finding your pandoc. Try running:
htmlwidgets:::find_pandoc()
htmlwidgets:::.pandoc$version
htmlwidgets:::.pandoc$dir
This seems to be running fine - it's probably just that you cannot see it in Swagger.
If you run this from swagger, you get the HTML-code needed for the plot:
But if you instead go to http://127.0.0.1:8000/plotly
in your browser, you should see the plotly widget:
Sorry about the small image size.