Home > Back-end >  Shiny: perform operations prior to output
Shiny: perform operations prior to output

Time:10-05

I am trying to figure out how to take the input values, perform various operations/calculations, and then outputting the results of those operations as both a plot and a text.

Here's an example to illustrate what I'm trying to get at:

ui <- fluidPage(
  
  sidebarLayout(
    
    sidebarPanel(
      sliderInput("mu", "Mean",
                  min = 0,
                  max = 100,
                  value = 1),
      sliderInput("sigma", "Standard deviation", 
                  min = 0,
                  max = 100,
                  value = 1),
      sliderInput("n", "Number of observations", 
                  min = 3,
                  max = 1000,
                  value = 1000)
    ),
    
    mainPanel(
      plotOutput("output_plot"),
      textOutput("output_text")
    )
  )
)

Using the input parameters, I want to generate a vector x of random, normally distributed numbers and plot a histogram. This works fine:

server <- function(input, output) {
  
  output$output_plot <- renderPlot({
    x <- rnorm(input$n, input$mu, input$sigma)
    plot(hist(x))
  })
}

shinyApp(ui, server)

In addition to this, I want to print out the values of the mean and standard deviation of x as a text below the plot. If I only wanted the text, this would work:

server <- function(input, output) {

  output$output_text <- renderText({
    x <- rnorm(input$n, input$mu, input$sigma)
    paste("Observed mean = ", round(mean(x), 3), ". Observed standard deviation = ", round(sd(x), 3), ".", sep = "")
  })
}

shinyApp(ui, server)

My question is, how can I generate x before outputting the plot and the text? I want the result to look like this:

Example

I don't want to generate one x for the plot and another for the text. I want the text to describe the mean and SD of the same x that is shown on the plot.

The following does not work because reactive values can't be accessed outside of a reactive consumer:

server <- function(input, output) {
  
  x <- rnorm(input$n, input$mu, input$sigma)
  
  output$output_plot <- renderPlot({
    plot(hist(x))
  })
  
  output$output_text <- renderText({
    paste("Observed mean = ", round(mean(x), 3), ". Observed standard deviation = ", round(sd(x), 3), ".", sep = "")
  })
}

shinyApp(ui, server)

CodePudding user response:

There you go. You just need to pass x to a reactive value. Then reactive values are called with parentheses.


library(shiny)

ui <- fluidPage(
  
  sidebarLayout(
    
    sidebarPanel(
      sliderInput("mu", "Mean",
                  min = 0,
                  max = 100,
                  value = 1),
      sliderInput("sigma", "Standard deviation", 
                  min = 0,
                  max = 100,
                  value = 1),
      sliderInput("n", "Number of observations", 
                  min = 3,
                  max = 1000,
                  value = 1000)
    ),
    
    mainPanel(
      plotOutput("output_plot"),
      textOutput("output_text")
    )
  )
)

server <- function(input, output) {
  
  x <- reactive({
    rnorm(input$n, input$mu, input$sigma)
  })
  
  output$output_plot <- renderPlot({
    plot(hist(x()))
  })
  
  output$output_text <- renderText({
    paste("Observed mean = ", round(mean(x()), 3), ". Observed standard deviation = ", round(sd(x()), 3), ".", sep = "")
  })
  
}

shinyApp(ui, server)
  • Related