Search code examples
rrecursionshinyfactorialshiny-server

Facfactorial Function


I am quite new to Shiny, but I am trying to implement a recursive factorial function into the R

Here is the code I am trying to implement:

recursive.factorial <- function(x) {
    # if the value of x is 0 or 1, then 1 is returned
    if (x == 0 || x == 1) {
    return (1)
  }
  else {
    return (x * recursive.factorial(x - 1)) # recursive function to calculate the factorial
  }
}
recursive.factorial(5)

Is it even possible to put something like this in Shiny?

Thanks


Solution

  • Like this?

    library(shiny)
    
    recursive.factorial <- function(x) {
      # if the value of x is 0 or 1, then 1 is returned
      if (x == 0 || x == 1) {
    return (1)
    }
      else {
    return (x * recursive.factorial(x - 1)) # recursive function to calculate the factorial
    } 
    }
    
    ui <- fluidPage(
      sidebarLayout(
      sidebarPanel(
      numericInput("obs", "Observations:", 10, min = 1, max = 100)
    ),
    mainPanel(textOutput("value"))
     )
    )
    
    server <- function(input, output) {
      output$value <- renderText({
      print(recursive.factorial(input$obs))
    
      })
    }
    
    shinyApp(ui = ui, server = server)