阶乘函数

Facfactorial Function

我对 Shiny 很陌生,但我正在尝试在 R 中实现递归阶乘函数

这是我要实现的代码:

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)

是否可以在 Shiny 中放置这样的东西?

谢谢

像这样?

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)