2016-07-23 07:29:20 +02:00
|
|
|
<!doctype html>
|
|
|
|
|
|
|
|
<title>CodeMirror: R mode</title>
|
|
|
|
<meta charset="utf-8"/>
|
|
|
|
<link rel=stylesheet href="../../doc/docs.css">
|
|
|
|
|
|
|
|
<link rel="stylesheet" href="../../lib/codemirror.css">
|
|
|
|
<script src="../../lib/codemirror.js"></script>
|
|
|
|
<script src="r.js"></script>
|
|
|
|
<style>
|
|
|
|
.CodeMirror { border-top: 1px solid silver; border-bottom: 1px solid silver; }
|
|
|
|
.cm-s-default span.cm-semi { color: blue; font-weight: bold; }
|
|
|
|
.cm-s-default span.cm-dollar { color: orange; font-weight: bold; }
|
|
|
|
.cm-s-default span.cm-arrow { color: brown; }
|
|
|
|
.cm-s-default span.cm-arg-is { color: brown; }
|
|
|
|
</style>
|
|
|
|
<div id=nav>
|
2019-10-15 10:40:42 +02:00
|
|
|
<a href="https://codemirror.net"><h1>CodeMirror</h1><img id=logo src="../../doc/logo.png"></a>
|
2016-07-23 07:29:20 +02:00
|
|
|
|
|
|
|
<ul>
|
|
|
|
<li><a href="../../index.html">Home</a>
|
|
|
|
<li><a href="../../doc/manual.html">Manual</a>
|
|
|
|
<li><a href="https://github.com/codemirror/codemirror">Code</a>
|
|
|
|
</ul>
|
|
|
|
<ul>
|
|
|
|
<li><a href="../index.html">Language modes</a>
|
|
|
|
<li><a class=active href="#">R</a>
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<article>
|
|
|
|
<h2>R mode</h2>
|
|
|
|
<form><textarea id="code" name="code">
|
2019-10-15 10:40:42 +02:00
|
|
|
X <- list(height = 5.4, weight = 54)
|
|
|
|
cat("Printing objects: "); print(X)
|
|
|
|
print("Accessing individual elements:")
|
|
|
|
cat(sprintf("Your height is %s and your weight is %s\n", X$height, X$weight))
|
2016-07-23 07:29:20 +02:00
|
|
|
|
2019-10-15 10:40:42 +02:00
|
|
|
# Functions:
|
2016-07-23 07:29:20 +02:00
|
|
|
square <- function(x) {
|
2019-10-15 10:40:42 +02:00
|
|
|
return(x * x)
|
2016-07-23 07:29:20 +02:00
|
|
|
}
|
2019-10-15 10:40:42 +02:00
|
|
|
cat(sprintf("The square of 3 is %s\n", square(3)))
|
2016-07-23 07:29:20 +02:00
|
|
|
|
2019-10-15 10:40:42 +02:00
|
|
|
# In R, the last expression in a function is, by default, what is
|
|
|
|
# returned. The idiomatic way to write the function is:
|
|
|
|
square <- function(x) {
|
|
|
|
x * x
|
2016-07-23 07:29:20 +02:00
|
|
|
}
|
2019-10-15 10:40:42 +02:00
|
|
|
# or, for functions with short content:
|
|
|
|
square <- function(x) x * x
|
2016-07-23 07:29:20 +02:00
|
|
|
|
2019-10-15 10:40:42 +02:00
|
|
|
# Function arguments with default values:
|
|
|
|
cube <- function(x = 5) x * x * x
|
|
|
|
cat(sprintf("Calling cube with 2 : %s\n", cube(2)) # will give 2^3
|
|
|
|
cat(sprintf("Calling cube : %s\n", cube()) # will default to 5^3.
|
2016-07-23 07:29:20 +02:00
|
|
|
|
2019-10-15 10:40:42 +02:00
|
|
|
powers <- function(x) list(x2 = x*x, x3 = x*x*x, x4 = x*x*x*x)
|
2016-07-23 07:29:20 +02:00
|
|
|
|
2019-10-15 10:40:42 +02:00
|
|
|
cat("Powers of 3: "); print(powers(3))
|
2016-07-23 07:29:20 +02:00
|
|
|
|
2019-10-15 10:40:42 +02:00
|
|
|
# Data frames
|
|
|
|
df <- data.frame(letters = letters[1:5], '#letter' = 1:5)
|
|
|
|
print(df$letters)
|
|
|
|
print(df$`#letter`)
|
2016-07-23 07:29:20 +02:00
|
|
|
|
2019-10-15 10:40:42 +02:00
|
|
|
# Operators:
|
|
|
|
m1 <- matrix(1:6, 2, 3)
|
|
|
|
m2 <- m1 %*% t(m1)
|
|
|
|
cat("Matrix product: "); print(m2)
|
|
|
|
|
|
|
|
# Assignments:
|
|
|
|
a <- 1
|
|
|
|
b <<- 2
|
|
|
|
c = 3
|
|
|
|
4 -> d
|
|
|
|
5 ->> e
|
2016-07-23 07:29:20 +02:00
|
|
|
</textarea></form>
|
|
|
|
<script>
|
|
|
|
var editor = CodeMirror.fromTextArea(document.getElementById("code"), {});
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<p><strong>MIME types defined:</strong> <code>text/x-rsrc</code>.</p>
|
|
|
|
|
|
|
|
<p>Development of the CodeMirror R mode was kindly sponsored
|
|
|
|
by <a href="https://twitter.com/ubalo">Ubalo</a>.</p>
|
|
|
|
|
|
|
|
</article>
|