Using objects from draw package in a Shiny App

I have some pictures created with a function using the draw-package. Now I want to use them in a Shiny App. For now I succeeded in saving them as .png and loading them into my Shiny App. The problem is, that it takes too long.
So my question now is, if there is any way that I can convert those graphics directly to ggplots or other grobs which allow me to use them in the Shiny App.

I think it depends what you mean by 'pictures'. If you mean plots drawn from data, that can be rendered with ggplot, and just happened to be saved as .png images right now, then yes you can definitely use those with ggplot in Shiny.

It's a little bit complicated since I was not the one writing the draw-function.
But it is a function using the draw-package like this:

drawCard <- function(){
    draw::drawPage(units = "inches",width = 7, height = 12)
}

Later there are some more elements added through a data frame.
My problem ist that I don't really understand the structure of those objects created with the draw-package and if there is any way to get them as a ggplot or add them to a ggplot. I tried that with

ggdraw() + drawCard()

But I get the error "Can't add drawCard() to a ggplot object."

I hope my question is clear now.

I looked it up and draw is a wrapper around base graphics functions, as such they are not compatible with ggplot. However, shiny supports output from both ggplot and base plotting so it should still integrate just fine.
Example:

library(draw)
library(shiny)

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

server <- function(input, output, session) {
  output$myplot <- renderPlot({
    drawSettings(pageWidth = 5, pageHeight = 5, units = "inches")
    drawPage()
    drawBox(x = 2.5, y = 2.5, width = 1, height = 1)
    drawCircle(x = 2.5, y = 2.5, radius = 0.5)
    drawLine(x = c(1, 4),
             y = c(1 ,1))
    drawText(x = 2.5, y = 2.5, text = "TEXT")
  })
}

shinyApp(ui, server)
1 Like

Ok, thank you! Somehow the graph I have is not shown correct in shiny. But if there's no way to get it as a ggplot I will look for a solution like this.

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.