How to display default plot in www/ before actionButton is clicked R shiny

淺唱寂寞╮ 提交于 2020-12-15 18:55:50

问题


Here is a sample code to generate a plot upon clicking the actionButton.

shinyApp(
shinyUI(fluidPage(
    inputPanel( 
        numericInput("n", "n", 10),
        actionButton("update", "Update")
    ),
    plotOutput("plot")
)),

shinyServer(function(input, output) {
    values <- reactiveValues()
    values$data <- c()

    obs <- observe({
        input$update
        isolate({ values$data <- c(values$data, runif(as.numeric(input$n), -10, 10)) })
    }, suspended=TRUE)

    obs2 <- observe({
        if (input$update > 0) obs$resume()
    })

    output$plot <- renderPlot({
        dat <- values$data
        hist(dat)
    })
}) 

)

I would like to display a default plot which is in www/test.png to appear when the application is launched. And then change the plot after clicking the actionButton as per the user input.


回答1:


First, I create a simple plot, export it as an image (manually, not in code) and name it Rplot.png (save it where you want):

plot(mtcars$mpg)

Then, in the shiny app, we have to distinguish two situations :

  • when the app starts, no button is clicked yet, we render the image with renderImage

  • when we click on the button, we replace renderImage with renderPlot and render an interactive plot

This means that we must use uiOutput in ui part so that we can choose the output to be an image or a plot according to the situation.

Here's an example (I didn't adapt your code but it should not be too difficult):

library(shiny)

# determine your path to image here (you should use the package "here" to do so)

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

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

  ### "Static" part: no click on actionButton yet
  output$some_ui <- renderUI({
    imageOutput("image_plot")
  })

  output$image_plot <- renderImage({
    list(src = "Rplot.png",
         contentType = 'image/png')
  }, deleteFile = FALSE) # Do not forget this option



  ### Click on actionButton
  observeEvent(input$run, {
    output$some_ui <- renderUI({
      plotOutput("dynamic_plot")
    })

    output$dynamic_plot <- renderPlot({
      plot(mtcars[[input$choice]])
    })
  })

}

shinyApp(ui, server)



回答2:


The key is to use renderUI, so you can either show an image or a R plot. This should do what you desire:

shinyApp(
  shinyUI(fluidPage(
    inputPanel( 
      numericInput("n", "n", 10),
      actionButton("update", "Update")
    ),
    uiOutput("out")
  )),

  shinyServer(function(session, input, output) {
    values <- reactiveValues()

    # check if plot has been already rendered
    check <- reactiveVal(FALSE)
    values$data <- c()

    observeEvent(input$update, {
      # set check to TRUE
      check(TRUE)
      input$update
      values$data <- c(values$data, runif(as.numeric(input$n), -10, 10))
      dat <- values$data
      output$plot <- renderPlot({
        hist(dat)
      })
    })

    # initial picture. 
    output$picture <- renderImage({
      list(src = "temp.png")
    }, deleteFile = FALSE)


    output$out <- renderUI({
      # in the  beginning, check is FALSE and the picture is shown
      if (!check()) {
        imageOutput("picture")
      } else {
        # as soon as the button has been pressed the first time,
        # the plot is shown
        plotOutput("plot")
      }


    })
  }) 

)



回答3:


I know, that this has been solved a while, but I needed a solution, without uiOutput. Plus I find this much simpler.

library(shiny)

if (interactive()) {
  shinyApp(
    ui = fluidPage(
      actionButton("btn", "Click me"),
      br(),
      plotOutput('some_plot', width = '100%')
    ),
    server = function(input, output) {
      output$some_plot <- renderPlot({
        if (!input$btn) {
          # default plot
          plot(1, 1, col = 'red')
        } else{
          # updated plot
          plot(1000, 1000, col = 'green')
        }
      })
    }
  )
}


来源:https://stackoverflow.com/questions/60797216/how-to-display-default-plot-in-www-before-actionbutton-is-clicked-r-shiny

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