How to render default plot in R shiny

我怕爱的太早我们不能终老 提交于 2020-03-25 16:06:55

问题


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?

来源:https://stackoverflow.com/questions/60811581/how-to-render-default-plot-in-r-shiny

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!