1

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?

chas
  • 1,565
  • 5
  • 26
  • 54
  • I don't understand how this is different from [this question](https://stackoverflow.com/questions/60797216/how-to-display-default-plot-in-www-before-actionbutton-is-clicked-r-shiny/60801334?noredirect=1#comment107593082_60801334), where you have two working answers – bretauv Mar 23 '20 at 11:32
  • in those the actionButton trigger was return using `observeEvent` while this approach is different which uses value as shown above. Probably this is going wrong somewhere here. – chas Mar 23 '20 at 11:34
  • okay but why do you want to use values if using `observeEvent` works? – bretauv Mar 23 '20 at 11:35
  • As mentioned earlier this is small snippet of the app which was written using values. the app has >20 files where it needs to be changed. So im preferring to stick with values. Do i need to use `if(input$run!=0)` for displaying default plot? did not work though. – chas Mar 23 '20 at 11:38

0 Answers0