In R Shiny, I was trying to produce multiple plots from one model (including one set of simulations) but it only returns one plot. I tried the code from the answer in another post on stack overflow and it works however when i added the second plot in the model, only the second plot can show but not the first one. Anybody can advise on this please? code as below from the answer in the post mentioned above:
library(shiny)
ui <- shinyUI(fluidPage(
br(),
actionButton("numb", "generate a random numbers"),
br(),
br(),
verbatimTextOutput("text"),
plotOutput("plot"),
plotOutput("plot2"),
tableOutput("table")
))
server <- shinyServer(function(input, output) {
model <- eventReactive(input$numb, {
# draw a random number and print it
random <- sample(1:100, 1)
print(paste0("The number is: ", random))
# generate data for a table and plot
data <- rnorm(10, mean = 100)
table <- matrix(data, ncol = 2)
# create a plot
Plot <- plot(1:length(data), data, pch = 16, xlab ="This is the first plot", ylab =
"")
# create a second plot
Plot2 <- plot(1:length(data), data, pch=16, xlab="This is the second plot", ylab =
"")
# return all object as a list
list(random = random, Plot = Plot, Plot2=Plot2, table = table)
})
output$text <- renderText({
# print the random number after accessing "model" with brackets.
# It doesn't re-run the function.
youget <- paste0("After using model()$random you get: ", model()$random,
". Compare it with a value in the console")
print(youget)
youget
})
output$plot <- renderPlot({
# render saved plot
model()$Plot
})
output$plot2 <-renderPlot({
# render second plot
model()$Plot2
})
output$table <- renderTable({
model()$table
})
})
shinyApp(ui = ui, server = server)