Skip to content

Commit 0e4c156

Browse files
flying-sheepmarijnh
authored andcommitted
[r mode] Various improvements
Closes #4297
1 parent 147389d commit 0e4c156

File tree

2 files changed

+45
-35
lines changed

2 files changed

+45
-35
lines changed

mode/r/index.html

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -31,47 +31,50 @@
3131
<article>
3232
<h2>R mode</h2>
3333
<form><textarea id="code" name="code">
34-
# Code from http://www.mayin.org/ajayshah/KB/R/
34+
X <- list(height = 5.4, weight = 54)
35+
cat("Printing objects: "); print(X)
36+
print("Accessing individual elements:")
37+
cat(sprintf("Your height is %s and your weight is %s\n", X$height, X$weight))
3538

36-
# FIRST LEARN ABOUT LISTS --
37-
X = list(height=5.4, weight=54)
38-
print("Use default printing --")
39-
print(X)
40-
print("Accessing individual elements --")
41-
cat("Your height is ", X$height, " and your weight is ", X$weight, "\n")
42-
43-
# FUNCTIONS --
39+
# Functions:
4440
square <- function(x) {
45-
return(x*x)
41+
return(x * x)
4642
}
47-
cat("The square of 3 is ", square(3), "\n")
43+
cat(sprintf("The square of 3 is %s\n", square(3)))
4844

49-
# default value of the arg is set to 5.
50-
cube <- function(x=5) {
51-
return(x*x*x);
45+
# In R, the last expression in a function is, by default, what is
46+
# returned. The idiomatic way to write the function is:
47+
square <- function(x) {
48+
x * x
5249
}
53-
cat("Calling cube with 2 : ", cube(2), "\n") # will give 2^3
54-
cat("Calling cube : ", cube(), "\n") # will default to 5^3.
50+
# or, for functions with short content:
51+
square <- function(x) x * x
5552

56-
# LEARN ABOUT FUNCTIONS THAT RETURN MULTIPLE OBJECTS --
57-
powers <- function(x) {
58-
parcel = list(x2=x*x, x3=x*x*x, x4=x*x*x*x);
59-
return(parcel);
60-
}
53+
# Function arguments with default values:
54+
cube <- function(x = 5) x * x * x
55+
cat(sprintf("Calling cube with 2 : %s\n", cube(2)) # will give 2^3
56+
cat(sprintf("Calling cube : %s\n", cube()) # will default to 5^3.
6157

62-
X = powers(3);
63-
print("Showing powers of 3 --"); print(X);
58+
powers <- function(x) list(x2 = x*x, x3 = x*x*x, x4 = x*x*x*x)
6459

65-
# WRITING THIS COMPACTLY (4 lines instead of 7)
60+
cat("Powers of 3: "); print(powers(3))
6661

67-
powerful <- function(x) {
68-
return(list(x2=x*x, x3=x*x*x, x4=x*x*x*x));
69-
}
70-
print("Showing powers of 3 --"); print(powerful(3));
62+
# Data frames
63+
df <- data.frame(letters = letters[1:5], '#letter' = 1:5)
64+
print(df$letters)
65+
print(df$`#letter`)
7166

72-
# In R, the last expression in a function is, by default, what is
73-
# returned. So you could equally just say:
74-
powerful <- function(x) {list(x2=x*x, x3=x*x*x, x4=x*x*x*x)}
67+
# Operators:
68+
m1 <- matrix(1:6, 2, 3)
69+
m2 <- m1 %*% t(m1)
70+
cat("Matrix product: "); print(m2)
71+
72+
# Assignments:
73+
a <- 1
74+
b <<- 2
75+
c = 3
76+
4 -> d
77+
5 ->> e
7578
</textarea></form>
7679
<script>
7780
var editor = CodeMirror.fromTextArea(document.getElementById("code"), {});

mode/r/r.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ CodeMirror.defineMode("r", function(config) {
4444
} else if (ch == "'" || ch == '"') {
4545
state.tokenize = tokenString(ch);
4646
return "string";
47+
} else if (ch == "`") {
48+
stream.match(/[^`]+`/);
49+
return "variable-3";
4750
} else if (ch == "." && stream.match(/.[.\d]+/)) {
4851
return "keyword";
4952
} else if (/[\w\.]/.test(ch) && ch != "_") {
@@ -62,13 +65,17 @@ CodeMirror.defineMode("r", function(config) {
6265
return "variable";
6366
} else if (ch == "%") {
6467
if (stream.skipTo("%")) stream.next();
65-
return "variable-2";
66-
} else if (ch == "<" && stream.eat("-")) {
67-
return "arrow";
68+
return "operator variable-2";
69+
} else if (
70+
(ch == "<" && stream.eat("-")) ||
71+
(ch == "<" && stream.match("<-")) ||
72+
(ch == "-" && stream.match(/>>?/))
73+
) {
74+
return "operator arrow";
6875
} else if (ch == "=" && state.ctx.argList) {
6976
return "arg-is";
7077
} else if (opChars.test(ch)) {
71-
if (ch == "$") return "dollar";
78+
if (ch == "$") return "operator dollar";
7279
stream.eatWhile(opChars);
7380
return "operator";
7481
} else if (/[\(\){}\[\];]/.test(ch)) {

0 commit comments

Comments
 (0)