Mateus Figueiredo
Mateus Figueiredo

Reputation: 81

Successive calculations in Shiny

I want to make a shiny app that can make successive calculations based on user input. Something like this:

a <- input$inputa
b <- a+2
c <- b-3
d <- c*4
e <- d/5

So the user would choose input a, and the shiny app would do the rest and show values a, b, c, d, e.

I managed to do it if the app always makes the entire calculations based on a. But if I try to create value b and call it, it breaks.

The following code works and shows the final result as it should, but I'm sure it can be improved upon, removing repetitions:

# UI
ui <- fluidPage(
  
  # Application title
  titlePanel("Doing algebra"),
  
  # Sidebar with numeric input
  sidebarLayout(
    sidebarPanel(
      numericInput("inputa",
                   "Input a:",
                   min = 0,
                   max = 100,
                   value = 20,
                   step=1)
      ),
    
    # Show results of successive calculations
    mainPanel(
      verbatimTextOutput("output1"),
      h4("+2"),
      verbatimTextOutput("output2"),
      h4("-3"),
      verbatimTextOutput("output3"),
      h4("*4"),
      verbatimTextOutput("output4"),
      h4("/5"),
      verbatimTextOutput("output5")
    )
  )
)

# server
server <- function(input, output) {  
  output$output1 <- renderText({     input$inputa })
  output$output2 <- renderText({     input$inputa+2 })
  output$output3 <- renderText({ (   input$inputa+2)-3 })
  output$output4 <- renderText({ ((  input$inputa+2)-3)*4 })
  output$output5 <- renderText({ ((( input$inputa+2)-3)*4)/5 })
}

shinyApp(ui, server)

The last bit, (((input$inputa+2)-3)*4)/5, looks terrible and is terrible. Can I make a shiny app that creates a value in one equation and uses that value in the next equation?

Thanks!

Upvotes: 1

Views: 875

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 389135

You can store the data in a reactive expression.

ui <- fluidPage(
  
  # Application title
  titlePanel("Doing algebra"),
  
  # Sidebar with numeric input
  sidebarLayout(
    sidebarPanel(
      numericInput("inputa",
                   "Input a:",
                   min = 0,
                   max = 100,
                   value = 20,
                   step=1)
    ),
    
    # Show results of successive calculations
    mainPanel(
      verbatimTextOutput("output1"),
      h4("+2"),
      verbatimTextOutput("output2"),
      h4("-3"),
      verbatimTextOutput("output3"),
      h4("*4"),
      verbatimTextOutput("output4"),
      h4("/5"),
      verbatimTextOutput("output5")
    )
  )
)

# server
server <- function(input, output) { 
  rv <- reactive({
    tibble::tibble(a = input$inputa, b = a + 2, c = b-3, d = c*4, e = d/5)
    })
  
  output$output1 <- renderText({rv()$a})
  output$output2 <- renderText({rv()$b})
  output$output3 <- renderText({rv()$c})
  output$output4 <- renderText({rv()$d})
  output$output5 <- renderText({rv()$e})
}

shinyApp(ui, server)

Upvotes: 2

Related Questions