on master help sign in
ADDED test file for R
e7a3f34Akshat Gadhwal
tests/syntax-tests/highlighted/R/test.rnew file+170
@@ -0,0 +1,170 @@
1+# take input from the user
2+num = as.integer(readline(prompt="Enter a number: "))
3+factorial = 1
4+# check is the number is negative, positive or zero
5+if(num < 0) {
6+print("Sorry, factorial does not exist for negative numbers")
7+} else if(num == 0) {
8+print("The factorial of 0 is 1")
9+} else {
10+for(i in 1:num) {
11+factorial = factorial * i
12+}
13+print(paste("The factorial of", num ,"is",factorial))
14+}
15+
16+x <- 0
17+if (x < 0) {
18+print("Negative number")
19+} else if (x > 0) {
20+print("Positive number")
21+} else
22+print("Zero")
23+
24+x <- 1:5
25+for (val in x) {
26+if (val == 3){
27+next
28+}
29+print(val)
30+}
31+
32+x <- 1
33+repeat {
34+print(x)
35+x = x+1
36+if (x == 6){
37+break
38+}
39+}
40+
41+`%divisible%` <- function(x,y)
42+{
43+if (x%%y ==0) return (TRUE)
44+else return (FALSE)
45+}
46+
47+switch("length", "color" = "red", "shape" = "square", "length" = 5)
48+[1] 5
49+
50+recursive.factorial <- function(x) {
51+if (x == 0) return (1)
52+else return (x * recursive.factorial(x-1))
53+}
54+
55+pow <- function(x, y) {
56+# function to print x raised to the power y
57+result <- x^y
58+print(paste(x,"raised to the power", y, "is", result))
59+}
60+
61+A <- read.table("x.data", sep=",",
62+ col.names=c("year", "my1", "my2"))
63+nrow(A) # Count the rows in A
64+
65+summary(A$year) 
66+
67+A$newcol <- A$my1 + A$my2 # Makes a new
68+newvar <- A$my1 - A$my2 # Makes a 
69+A$my1 <- NULL # Removes 
70+str(A)
71+summary(A)
72+library(Hmisc) 
73+contents(A)
74+describe(A)
75+
76+set.seed(102) # This yields a good illustration.
77+x <- sample(1:3, 15, replace=TRUE)
78+education <- factor(x, labels=c("None", "School", "College"))
79+x <- sample(1:2, 15, replace=TRUE)
80+gender <- factor(x, labels=c("Male", "Female"))
81+age <- runif(15, min=20,max=60)
82+
83+D <- data.frame(age, gender, education)
84+rm(x,age,gender,education)
85+print(D)
86+
87+# Table about education
88+table(D$education)
89+
90+# Table about education and gender --
91+table(D$gender, D$education)
92+# Joint distribution of education and gender --
93+table(D$gender, D$education)/nrow(D)
94+
95+# Add in the marginal distributions also
96+addmargins(table(D$gender, D$education))
97+addmargins(table(D$gender, D$education))/nrow(D)
98+
99+# Generate a good LaTeX table out of it --
100+library(xtable)
101+xtable(addmargins(table(D$gender, D$education))/nrow(D),
102+ digits=c(0,2,2,2,2)) 
103+
104+by(D$age, D$gender, mean)
105+by(D$age, D$gender, sd)
106+by(D$age, D$gender, summary)
107+
108+a <- matrix(by(D$age, list(D$gender, D$education), mean), nrow=2)
109+rownames(a) <- levels(D$gender)
110+colnames(a) <- levels(D$education)
111+print(a)
112+print(xtable(a))
113+
114+dat <- read.csv(file = "files/dataset-2013-01.csv", header = TRUE)
115+interim_object <- data.frame(rep(1:100, 10),
116+ rep(101:200, 10),
117+ rep(201:300, 10))
118+object.size(interim_object) 
119+rm("interim_object") 
120+ls() 
121+rm(list = ls())
122+
123+vector1 <- c(5,9,3)
124+vector2 <- c(10,11,12,13,14,15)
125+array1 <- array(c(vector1,vector2),dim = c(3,3,2))
126+vector3 <- c(9,1,0)
127+vector4 <- c(6,0,11,3,14,1,2,6,9)
128+array2 <- array(c(vector1,vector2),dim = c(3,3,2))
129+matrix1 <- array1[,,2]
130+matrix2 <- array2[,,2]
131+result <- matrix1+matrix2
132+print(result)
133+
134+column.names <- c("COL1","COL2","COL3")
135+row.names <- c("ROW1","ROW2","ROW3")
136+matrix.names <- c("Matrix1","Matrix2")
137+result <- array(c(vector1,vector2),dim = c(3,3,2),dimnames = list(row.names,
138+ column.names, matrix.names))
139+print(result[3,,2])
140+print(result[1,3,1])
141+print(result[,,2])
142+
143+# Load the package required to read JSON files.
144+library("rjson")
145+result <- fromJSON(file = "input.json")
146+print(result)
147+
148+x <- c(151, 174, 138, 186, 128, 136, 179, 163, 152, 131)
149+y <- c(63, 81, 56, 91, 47, 57, 76, 72, 62, 48)
150+relation <- lm(y~x)
151+print(relation)
152+
153+relation <- lm(y~x)
154+png(file = "linearregression.png")
155+plot(y,x,col = "blue",main = "Height & Weight Regression",
156+abline(lm(x~y)),cex = 1.3,pch = 16,xlab = "Weight in Kg",ylab = "Height in cm")
157+dev.off()
158+
159+data <- c("East","West","East","North","North","East","West","West","West","East","North")
160+print(data)
161+print(is.factor(data))
162+factor_data <- factor(data)
163+print(factor_data)
164+print(is.factor(factor_data))
165+
166+v <- c(7,12,28,3,41)
167+
168+# Give the chart file a name.
169+png(file = "line_chart_label_colored.jpg")
170+plot(v,type = "o", col = "red", xlab = "Month", ylab = "Rain fall", main = "Rain fall chart")
tests/syntax-tests/source/R/test.rnew file+170
@@ -0,0 +1,170 @@
1+# take input from the user
2+num = as.integer(readline(prompt="Enter a number: "))
3+factorial = 1
4+# check is the number is negative, positive or zero
5+if(num < 0) {
6+print("Sorry, factorial does not exist for negative numbers")
7+} else if(num == 0) {
8+print("The factorial of 0 is 1")
9+} else {
10+for(i in 1:num) {
11+factorial = factorial * i
12+}
13+print(paste("The factorial of", num ,"is",factorial))
14+}
15+
16+x <- 0
17+if (x < 0) {
18+print("Negative number")
19+} else if (x > 0) {
20+print("Positive number")
21+} else
22+print("Zero")
23+
24+x <- 1:5
25+for (val in x) {
26+if (val == 3){
27+next
28+}
29+print(val)
30+}
31+
32+x <- 1
33+repeat {
34+print(x)
35+x = x+1
36+if (x == 6){
37+break
38+}
39+}
40+
41+`%divisible%` <- function(x,y)
42+{
43+if (x%%y ==0) return (TRUE)
44+else return (FALSE)
45+}
46+
47+switch("length", "color" = "red", "shape" = "square", "length" = 5)
48+[1] 5
49+
50+recursive.factorial <- function(x) {
51+if (x == 0) return (1)
52+else return (x * recursive.factorial(x-1))
53+}
54+
55+pow <- function(x, y) {
56+# function to print x raised to the power y
57+result <- x^y
58+print(paste(x,"raised to the power", y, "is", result))
59+}
60+
61+A <- read.table("x.data", sep=",",
62+ col.names=c("year", "my1", "my2"))
63+nrow(A) # Count the rows in A
64+
65+summary(A$year)
66+
67+A$newcol <- A$my1 + A$my2 # Makes a new
68+newvar <- A$my1 - A$my2 # Makes a
69+A$my1 <- NULL # Removes
70+str(A)
71+summary(A)
72+library(Hmisc)
73+contents(A)
74+describe(A)
75+
76+set.seed(102) # This yields a good illustration.
77+x <- sample(1:3, 15, replace=TRUE)
78+education <- factor(x, labels=c("None", "School", "College"))
79+x <- sample(1:2, 15, replace=TRUE)
80+gender <- factor(x, labels=c("Male", "Female"))
81+age <- runif(15, min=20,max=60)
82+
83+D <- data.frame(age, gender, education)
84+rm(x,age,gender,education)
85+print(D)
86+
87+# Table about education
88+table(D$education)
89+
90+# Table about education and gender --
91+table(D$gender, D$education)
92+# Joint distribution of education and gender --
93+table(D$gender, D$education)/nrow(D)
94+
95+# Add in the marginal distributions also
96+addmargins(table(D$gender, D$education))
97+addmargins(table(D$gender, D$education))/nrow(D)
98+
99+# Generate a good LaTeX table out of it --
100+library(xtable)
101+xtable(addmargins(table(D$gender, D$education))/nrow(D),
102+ digits=c(0,2,2,2,2))
103+
104+by(D$age, D$gender, mean)
105+by(D$age, D$gender, sd)
106+by(D$age, D$gender, summary)
107+
108+a <- matrix(by(D$age, list(D$gender, D$education), mean), nrow=2)
109+rownames(a) <- levels(D$gender)
110+colnames(a) <- levels(D$education)
111+print(a)
112+print(xtable(a))
113+
114+dat <- read.csv(file = "files/dataset-2013-01.csv", header = TRUE)
115+interim_object <- data.frame(rep(1:100, 10),
116+ rep(101:200, 10),
117+ rep(201:300, 10))
118+object.size(interim_object)
119+rm("interim_object")
120+ls()
121+rm(list = ls())
122+
123+vector1 <- c(5,9,3)
124+vector2 <- c(10,11,12,13,14,15)
125+array1 <- array(c(vector1,vector2),dim = c(3,3,2))
126+vector3 <- c(9,1,0)
127+vector4 <- c(6,0,11,3,14,1,2,6,9)
128+array2 <- array(c(vector1,vector2),dim = c(3,3,2))
129+matrix1 <- array1[,,2]
130+matrix2 <- array2[,,2]
131+result <- matrix1+matrix2
132+print(result)
133+
134+column.names <- c("COL1","COL2","COL3")
135+row.names <- c("ROW1","ROW2","ROW3")
136+matrix.names <- c("Matrix1","Matrix2")
137+result <- array(c(vector1,vector2),dim = c(3,3,2),dimnames = list(row.names,
138+ column.names, matrix.names))
139+print(result[3,,2])
140+print(result[1,3,1])
141+print(result[,,2])
142+
143+# Load the package required to read JSON files.
144+library("rjson")
145+result <- fromJSON(file = "input.json")
146+print(result)
147+
148+x <- c(151, 174, 138, 186, 128, 136, 179, 163, 152, 131)
149+y <- c(63, 81, 56, 91, 47, 57, 76, 72, 62, 48)
150+relation <- lm(y~x)
151+print(relation)
152+
153+relation <- lm(y~x)
154+png(file = "linearregression.png")
155+plot(y,x,col = "blue",main = "Height & Weight Regression",
156+abline(lm(x~y)),cex = 1.3,pch = 16,xlab = "Weight in Kg",ylab = "Height in cm")
157+dev.off()
158+
159+data <- c("East","West","East","North","North","East","West","West","West","East","North")
160+print(data)
161+print(is.factor(data))
162+factor_data <- factor(data)
163+print(factor_data)
164+print(is.factor(factor_data))
165+
166+v <- c(7,12,28,3,41)
167+
168+# Give the chart file a name.
169+png(file = "line_chart_label_colored.jpg")
170+plot(v,type = "o", col = "red", xlab = "Month", ylab = "Rain fall", main = "Rain fall chart")