R Recursion
Define a base case and a small supported input range.
How it works
A recursive function calls itself on a smaller problem. The base case must be reachable and return the correct result. Recursion consumes call-stack space, so this teaching function deliberately accepts only integers from zero through twenty. A vectorised formula or loop would be simpler for large sums.
Run this R example
These examples run locally with R, not PHP, Python or JavaScript. The reviewed baseline is R 4.5.2 on Windows x64/UCRT, using only packages shipped with R. This is a tested version, not a newest-release claim. Install your own R from the official R project; an IDE is optional. Save the complete source as recursion.R in a separate learning folder and run Rscript --vanilla recursion.R. Keep code and fictional fixtures separate from a real project.
Bhylu has no R browser runner. There is no Try in playground link because the existing Python and JavaScript editors do not execute R. No package installation, account, network dataset, Apache, MySQL or PHP setting is needed to run these base-R sources.
The complete download uses a local scope, restores its temporary options and treats unexpected warnings as errors. Examples that handle an expected warning do so narrowly. Random examples change only their R process’s RNG state. File examples own their temporary output; figure examples leave their named PNG in the current learning folder and refuse to overwrite it. Use a new empty folder to rerun a figure.
Download the complete recursion.R · Download all source files with hash checks
For the optional collection downloader, save download-examples.mjs in an empty folder and run node download-examples.mjs. Local preview before publishing uses node download-examples.mjs http://127.0.0.1:8000/examples/r/. It refuses an existing bhylu-r-examples folder and does not install or execute R.
Example
local({
previous_options <- options(warn = 2, digits = 8, stringsAsFactors = FALSE)
on.exit(options(previous_options), add = TRUE)
sum_to <- function(n) {
if (!is.numeric(n) || length(n) != 1L || is.na(n) ||
!is.finite(n) || n < 0 || n > 20 || n != floor(n)) {
stop("Use one integer from 0 to 20")
}
if (n == 0) return(0)
n + sum_to(n - 1)
}
cat("zero:", sum_to(0), "four:", sum_to(4), "\n")
invisible(NULL)
})
Focus on the lesson operations
This excerpt omits the complete script’s option/resource setup. Use the full download to execute it.
sum_to <- function(n) {
if (!is.numeric(n) || length(n) != 1L || is.na(n) ||
!is.finite(n) || n < 0 || n > 20 || n != floor(n)) {
stop("Use one integer from 0 to 20")
}
if (n == 0) return(0)
n + sum_to(n - 1)
}
cat("zero:", sum_to(0), "four:", sum_to(4), "\n")
Expected result
zero: 0 four: 10
The recursive argument decreases by one until zero is reached. Validation also rejects missing, infinite, fractional and over-budget values. This is a demonstration of the recurrence, not a claim that every recursive function will scale safely in R.
For API behaviour, consult the official Introduction to R and your installed help pages. These small examples do not establish real-world statistical conclusions or production performance.