Hammar
Hammar

Reputation: 91

Use Shiny to serve images reactively

I need to somehow serve R images reactively to a system that only lets me use a subset of HTML, so no iframes. Basically, it will have to be something like `img src="https://shinyserver.com/app/image123.jpeg" /'.

I am thinking about using https://github.com/Appsilon/shiny.router, but not sure if it will work.

Does anyone have suggestions?

Upvotes: 3

Views: 111

Answers (1)

SeGa
SeGa

Reputation: 9809

I am not sure if that really addresses your problem, but since you mentioned shiny.router, I think the plumber package might also be useful.

This is a small example, that lets you create an image tag using an R function as API.

plumber.R

library(shiny)
library(plumber)

#' Get an image tag with source from its name
#' @param imgsrc Source of the image
#' @get /
img2tag <- function(imgsrc = "") {
  url = paste0('https://shinyserver.com/app/', imgsrc)
  as.character(tags$img(src=url))
}

Then you have to start the plumber API by entering:

pr <- plumber::plumb("plumber.R")
pr$run(port = 7818)

And then you can access it in the browser via:

http://localhost:7818/?imgsrc=image123.jpeg

which gives you the JSON-aquivalent of the string:

0   "<img src=\"https://shinyserver.com/app/image123.jpeg\"/>"

or through the console with:

curl http://localhost:7818/?imgsrc=image123.jpeg

Upvotes: 2

Related Questions