0

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)
Yiqiao
  • 1
  • 2

1 Answers1

0

I moved the plots to the renderPlot server functions and modified the plot call:

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)

    # return all object as a list
    list(random = random, 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
    mod_list=model()
    data=mod_list$table
    # create a plot
    plot(data[,1], data[,2], pch = 16, xlab ="This is the first plot", ylab ="")

  })

  output$plot2 <-renderPlot({
    # render second plot
    mod_list=model()
    data=mod_list$table
    # create a second plot
    plot(data[,1], data[,2], pch=16, xlab="This is the second plot", ylab ="")

  })

  output$table <- renderTable({

    model()$table
  })
})

Your x and y arguments for plot function are confusing. If you want two lines per plot, try using qplot and the melt function to reshape your dataframe to long format. If you just want a plot with 10 random values, don'y use the matrix function

  • Thanks so much!!! it works very well. Shiny is new to me and this really solves the problem. I was using the code as an example where the key thing is to render multiple plots from the same data produced. This is amazing. Thanks so much. – Yiqiao Nov 27 '18 at 10:55