R Func Library
# R library() and require() Functions - Loading Packages
[ R Language Examples](https://example.com/r/r-examples.html)
The R **library()** function is used to load installed packages, making their functions available for the current session.
**require()** is similar, but returns FALSE instead of throwing an error when the package does not exist.
The syntax for both functions is as follows:
library(package)
require(package)
**Parameter Description:**
* **package** - Package name (quotes are optional).
## Example
```r
# Load built-in package (no installation needed)
library(stats)
print("stats Package already loaded")
# View loaded packages
loaded <-search()
print("Currently loaded packages:")
print(loaded[1:5])
# Safe loading: require doesn't throw error on failure
if(require("stats")){
print("stats Package loaded successfully")
}
# Difference between library and require
# library("nonexistent_pkg") # This will error and stop execution
result <-require("nonexistent_pkg")# Returns FALSE, doesn't stop
print(paste("require return value for non-existent package:", result))
```
Executing the above code produces the following output:
```
"stats Package already loaded"
"Currently loaded packages:"
".GlobalEnv" "package:stats" "package:graphics"
"package:grDevices" "package:utils"
"stats Package loaded successfully"
"require return value for non-existent package: FALSE"
```
> When writing scripts, it is recommended to use **require()** for safe checking: if the package doesn't exist, give a warning instead of interrupting script execution.
[ R Language Examples](https://example.com/r/r-examples.html)
YouTip