简体   繁体   中英

R Shiny eventReactive actionBotton interaction

I have an actionBotton to randomly pick one column from the first 5 columns from the dataset mtcars and plot it.

Now the actionBotton could do its job but the chart is not plotted at the first place when the app is launched.

Is there any way to have it plotted when the Shiny app is launched.

library(shiny)

server <- function(input, output) {
  X = eventReactive(input$plot,{
    mtcars
  })
  output$plot = renderPlot({
    i = sample(1:5,1)
    plot(X()[,i],ylab=names(mtcars)[i])
  })
}

ui <- fluidPage(
  actionButton("plot","randomly plot"),
  plotOutput("plot")

)

shinyApp(ui = ui, server = server)

You can add the condition is the button hasn't been clicked. Note that button works as a counter so if it hasn't been clicked the value will be 0.

library(shiny)

server <- function(input, output) {
  X = eventReactive(input$plot,{
    mtcars
  })
  output$plot = renderPlot({
    i = sample(1:5,1)
    if(input$plot == 0){
      return(plot(mtcars[,i],ylab=names(mtcars)[i]))
    }
    plot(X()[,i],ylab=names(mtcars)[i])
  })
}

ui <- fluidPage(
  actionButton("plot","randomly plot"),
  plotOutput("plot")

)

shinyApp(ui = ui, server = server)

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM