-1

I am quite new to R Shiny. I am trying to create a Shiny app for my digital image processing project. A quick outline, the app contains a brightness, saturation and hue slider and a checkbox to set the image to a negative format. Is there a way to display images using magick library specifically image_read() func? I tried numerous times yet to no avail, I tried printing through renderImage and renderPlot to no avail.

  • did you try `plot(yourimage)` in `renderPlot`? – Stéphane Laurent Dec 07 '22 at 11:25
  • yes I have, it works but I would like to try and use magick library for images if possible. – Brenz Gwynne HABABAG Dec 07 '22 at 12:23
  • What exactly did you try? In what ways did it fail? This question really isn't focused enough to give a clear, testable answer. It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Dec 07 '22 at 16:40
  • I tried displaying the image using an imageOutput then renderImage, inside the renderImage I created a variable for the image file location then used the image_read(filepath) function from magick library. – Brenz Gwynne HABABAG Dec 08 '22 at 06:13

1 Answers1

1
library(magick)
library(shiny)

ui <- fluidPage(
  plotOutput("p1")
)

server <- function(input, output, session) {
  
  #get image and its metadata
  my_img <- reactive({
    frink <- image_read("https://jeroen.github.io/images/frink.png")
    w <- image_info(frink)$width
    h <- image_info(frink)$height
    list(
      raster = as.raster(frink),
      w = w,
      h = h
    )
  })

  # a function factory to conveniently get out an images dimension
  img_dim_f <- function(parm) {
    function() {
      p <- 0
      i <- my_img()
      if (isTruthy(i)) {
        if (isTruthy(i[[parm]])) {
          p <- i[[parm]]
        }
      }
      p
    }
  }

  output$p1 <- renderPlot(
    expr = {
      i <- req(my_img())
      r <- req(i$raster)
      plot(r)
    },
    width = img_dim_f("w"),
    height = img_dim_f("h")
  )
}

shinyApp(ui, server)
Nir Graham
  • 2,567
  • 2
  • 6
  • 10
  • Helpful, thanks, being new to this. I added a file selection step, and then in the img_dim_f function, I needed to put req(my_img()), or subsequent images came out at the same size. – Scrope Dec 19 '22 at 14:21