How to render default plot in R shiny

372 views Asked by At

I have the below sample code from an application to genarate a plot and render to UI.

library(shiny)

ui <- fluidPage(
selectInput("choice", "Choose", choices = names(mtcars)),
actionButton("run", "Run"),
plotOutput("some_ui")
)

server <- function(input, output, session) {

output$some_ui <- renderPlot({
if(input$run==0) return()
withProgress(message = paste("Drawing heatmap, please wait."),{
heatmap_render(x,y)    ##custom function to generate plot###
})
})
}

This is not a working example as it includes a custom function to generate plot. This approach works.

However, i would need to display a default plot when the application is launched and before te action button is clicked. I tried a couple of approaches.

ui <- fluidPage(
selectInput("choice", "Choose", choices = names(mtcars)),
actionButton("run", "Run"),
plotOutput("some_ui")
)

server <- function(input, output, session) {

output$some_ui <-  renderUI({
if(input$run == 0)return()
  list(src = "www/heatmap.png")
})

output$some_ui <- renderPlot({
if(input$run == 0) return()
withProgress(message = paste("Drawing heatmap, please wait."),{
heatmap_render(x,y)    ##custom function to generate plot###
})
})
}

This did not render the default plot but works normal when the action button is clicked.

Apporoach 2: Changed plotOutput to uiOutput.

ui <- fluidPage(
selectInput("choice", "Choose", choices = names(mtcars)),
actionButton("run", "Run"),
uiOutput("some_ui")
)

server <- function(input, output, session) {
output$some_ui <-  renderUI({
if(input$run==0)return()
  list(src = "/www/heatmap.png")
})

output$some_ui <- renderPlot({
if(input$run == 0) return()
withProgress(message = paste("Drawing heatmap, please wait."),{
heatmap_render(x,y)    ##custom function to generate plot###
})
})
}

This gives the error Error in pngfun: invalid quartz() device size when actionButton is triggered. And defualt image ("www/heatap.png")is not shown.

Also using renderImage in approach 2 gives the same error Error in pngfun: invalid quartz() device size when actionButton is triggered.And defualt image ("www/heatap.png")is not shown.

 output$some_ui <-  renderImage({
 if(input$run==0)return()
  list(src = "www/heatmap.png", contentType = 'image/png')
  }, deleteFile = FALSE)

Any help to render default plot when the application is launched?

0

There are 0 answers