Home > Software design >  API Endpoints with R Plumber and Plotly Exception Error
API Endpoints with R Plumber and Plotly Exception Error

Time:03-31

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
}

Editing to show how this is solved with Pandoc

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.

CodePudding user response:

Update based on comments

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 Screenshot swagger

But if you instead go to http://127.0.0.1:8000/plotly in your browser, you should see the plotly widget:

Screenshot browser

Sorry about the small image size.

  • Related