Skip to content

Commit 5d528ac

Browse files
committed
feat (language): 支持 R 语言
1 parent 621278a commit 5d528ac

7 files changed

Lines changed: 405 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ CodeForge 是一款轻量级、高性能的桌面代码执行器,专为开发
3939
<img src="public/icons/php.svg" width="60" alt="PHP">
4040
<img src="public/icons/python.svg" width="60" alt="Python 2">
4141
<img src="public/icons/python.svg" width="60" alt="Python 3">
42+
<img src="public/icons/r.svg" width="60" alt="R">
4243
<img src="public/icons/ruby.svg" width="60" alt="Ruby">
4344
<img src="public/icons/rust.svg" width="60" alt="Rust">
4445
<img src="public/icons/shell.svg" width="60" alt="Shell">

public/icons/r.svg

Lines changed: 14 additions & 0 deletions
Loading

src-tauri/src/examples/r.r

Lines changed: 323 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,323 @@
1+
# R 示例代码 - CodeForge 代码执行环境
2+
3+
cat("欢迎使用 CodeForge!\n")
4+
cat("Welcome to CodeForge!\n\n")
5+
6+
cat("=========================================\n")
7+
cat(" CodeForge R \n")
8+
cat("=========================================\n\n")
9+
10+
# 基本输出示例
11+
cat("R 运行成功! (R is working!)\n")
12+
cat("这是 R 程序 (This is R program)\n\n")
13+
14+
# 变量操作
15+
name <- "CodeForge"
16+
version <- "R"
17+
number1 <- 10
18+
number2 <- 20
19+
result <- number1 + number2
20+
21+
cat("简单计算 (Simple calculation):\n")
22+
cat(sprintf("%d + %d = %d\n", number1, number2, result))
23+
cat("\n")
24+
25+
# 字符串操作
26+
cat("字符串操作 (String operations):\n")
27+
cat(sprintf("平台名称 (Platform): %s\n", name))
28+
cat(sprintf("语言版本 (Language): %s\n", version))
29+
cat(sprintf("完整信息 (Full info): %s - %s\n", name, version))
30+
cat("\n")
31+
32+
# 向量操作
33+
cat("向量示例 (Vector example):\n")
34+
fruits <- c("苹果", "香蕉", "橙子", "葡萄")
35+
for (i in 1:length(fruits)) {
36+
cat(sprintf("%d. %s\n", i, fruits[i]))
37+
}
38+
cat("\n")
39+
40+
# 数值向量
41+
numbers <- c(1, 2, 3, 4, 5)
42+
cat("数值向量示例 (Numeric vector example):\n")
43+
cat("原向量:", numbers, "\n")
44+
cat("向量和:", sum(numbers), "\n")
45+
cat("向量平均值:", mean(numbers), "\n")
46+
cat("向量长度:", length(numbers), "\n\n")
47+
48+
# 条件判断
49+
score <- 85
50+
cat("成绩评估 (Score evaluation):\n")
51+
if (score >= 90) {
52+
cat("优秀! (Excellent!)\n")
53+
} else if (score >= 80) {
54+
cat("良好! (Good!)\n")
55+
} else if (score >= 60) {
56+
cat("及格 (Pass)\n")
57+
} else {
58+
cat("需要努力 (Need improvement)\n")
59+
}
60+
cat("\n")
61+
62+
# 循环示例
63+
cat("循环输出 (Loop output):\n")
64+
for (i in 1:5) {
65+
cat(sprintf("第 %d 次输出 (Output #%d): Hello from CodeForge!\n", i, i))
66+
}
67+
cat("\n")
68+
69+
# while循环
70+
cat("While循环示例 (While loop example):\n")
71+
counter <- 1
72+
while (counter <= 3) {
73+
cat(sprintf("While循环: 第 %d 次\n", counter))
74+
counter <- counter + 1
75+
}
76+
cat("\n")
77+
78+
# 函数定义
79+
greet_user <- function(user_name) {
80+
return(paste("Hello,", user_name, "!"))
81+
}
82+
83+
add_numbers <- function(a, b) {
84+
return(a + b)
85+
}
86+
87+
fibonacci <- function(n) {
88+
if (n <= 1) return(n)
89+
return(fibonacci(n - 1) + fibonacci(n - 2))
90+
}
91+
92+
cat("函数示例 (Function example):\n")
93+
cat(greet_user("CodeForge用户"), "\n")
94+
cat("函数计算 5 + 3 =", add_numbers(5, 3), "\n\n")
95+
96+
# 递归示例
97+
cat("递归示例 (Recursion example):\n")
98+
fib_n <- 7
99+
fib_result <- fibonacci(fib_n)
100+
cat(sprintf("斐波那契数列第%d项: %d\n", fib_n, fib_result))
101+
cat("\n")
102+
103+
# 列表操作
104+
cat("列表示例 (List example):\n")
105+
person_list <- list(
106+
name = "张三",
107+
age = 25,
108+
height = 175.5,
109+
hobbies = c("读书", "游泳", "编程")
110+
)
111+
112+
cat("姓名:", person_list$name, "\n")
113+
cat("年龄:", person_list$age, "\n")
114+
cat("身高:", person_list$height, "cm\n")
115+
cat("爱好:", paste(person_list$hobbies, collapse = ", "), "\n\n")
116+
117+
# 数据框操作
118+
cat("数据框示例 (Data frame example):\n")
119+
students <- data.frame(
120+
姓名 = c("张三", "李四", "王五"),
121+
成绩 = c(85, 92, 78),
122+
年龄 = c(20, 21, 19),
123+
stringsAsFactors = FALSE
124+
)
125+
126+
cat("学生数据框:\n")
127+
print(students)
128+
cat("\n")
129+
130+
# 数据框统计
131+
cat("数据框统计 (Data frame statistics):\n")
132+
cat("平均成绩:", mean(students$成绩), "\n")
133+
cat("最高成绩:", max(students$成绩), "\n")
134+
cat("最低成绩:", min(students$成绩), "\n")
135+
cat("成绩标准差:", sd(students$成绩), "\n\n")
136+
137+
# 矩阵操作
138+
cat("矩阵示例 (Matrix example):\n")
139+
matrix_data <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, ncol = 3)
140+
cat("矩阵:\n")
141+
print(matrix_data)
142+
143+
cat("矩阵维度:", dim(matrix_data), "\n")
144+
cat("矩阵行数:", nrow(matrix_data), "\n")
145+
cat("矩阵列数:", ncol(matrix_data), "\n\n")
146+
147+
# 数学函数
148+
cat("数学函数示例 (Math function examples):\n")
149+
angle <- 45
150+
radians <- angle * pi / 180
151+
cat(sprintf("sin(%.0f°) = %.4f\n", angle, sin(radians)))
152+
cat(sprintf("cos(%.0f°) = %.4f\n", angle, cos(radians)))
153+
cat("sqrt(16) =", sqrt(16), "\n")
154+
cat("log(10) =", log(10), "\n")
155+
cat("exp(1) =", exp(1), "\n\n")
156+
157+
# 统计函数
158+
cat("统计函数示例 (Statistical function examples):\n")
159+
sample_data <- c(5, 2, 8, 1, 9, 3, 7, 4, 6)
160+
cat("数据:", paste(sample_data, collapse = ", "), "\n")
161+
cat("均值:", mean(sample_data), "\n")
162+
cat("中位数:", median(sample_data), "\n")
163+
cat("标准差:", sd(sample_data), "\n")
164+
cat("方差:", var(sample_data), "\n")
165+
cat("最小值:", min(sample_data), "\n")
166+
cat("最大值:", max(sample_data), "\n")
167+
cat("四分位数:\n")
168+
print(quantile(sample_data))
169+
cat("\n")
170+
171+
# 字符串处理
172+
cat("字符串处理示例 (String processing examples):\n")
173+
text <- "CodeForge R Example"
174+
cat("原字符串:", text, "\n")
175+
cat("字符串长度:", nchar(text), "\n")
176+
cat("大写:", toupper(text), "\n")
177+
cat("小写:", tolower(text), "\n")
178+
cat("子字符串:", substr(text, 1, 9), "\n")
179+
cat("替换:", gsub("R", "Programming", text), "\n\n")
180+
181+
# 逻辑运算
182+
cat("逻辑运算示例 (Logical operations example):\n")
183+
a <- TRUE
184+
b <- FALSE
185+
cat("a =", a, ", b =", b, "\n")
186+
cat("a & b =", a & b, "(AND)\n")
187+
cat("a | b =", a | b, "(OR)\n")
188+
cat("!a =", !a, "(NOT)\n\n")
189+
190+
# 向量化操作
191+
cat("向量化操作示例 (Vectorized operations example):\n")
192+
vec1 <- c(1, 2, 3, 4, 5)
193+
vec2 <- c(2, 4, 6, 8, 10)
194+
cat("向量1:", paste(vec1, collapse = ", "), "\n")
195+
cat("向量2:", paste(vec2, collapse = ", "), "\n")
196+
cat("向量相加:", paste(vec1 + vec2, collapse = ", "), "\n")
197+
cat("向量相乘:", paste(vec1 * vec2, collapse = ", "), "\n")
198+
cat("向量平方:", paste(vec1^2, collapse = ", "), "\n\n")
199+
200+
# 条件筛选
201+
cat("条件筛选示例 (Conditional filtering example):\n")
202+
test_scores <- c(85, 92, 78, 96, 73, 88, 91)
203+
cat("所有成绩:", paste(test_scores, collapse = ", "), "\n")
204+
high_scores <- test_scores[test_scores >= 90]
205+
cat("高分(>=90):", paste(high_scores, collapse = ", "), "\n")
206+
low_scores <- test_scores[test_scores < 80]
207+
cat("低分(<80):", paste(low_scores, collapse = ", "), "\n\n")
208+
209+
# 排序操作
210+
cat("排序操作示例 (Sorting operations example):\n")
211+
unsorted_data <- c(5, 2, 8, 1, 9, 3)
212+
cat("原数据:", paste(unsorted_data, collapse = ", "), "\n")
213+
sorted_asc <- sort(unsorted_data)
214+
cat("升序:", paste(sorted_asc, collapse = ", "), "\n")
215+
sorted_desc <- sort(unsorted_data, decreasing = TRUE)
216+
cat("降序:", paste(sorted_desc, collapse = ", "), "\n\n")
217+
218+
# 因子操作
219+
cat("因子示例 (Factor example):\n")
220+
grades <- c("A", "B", "C", "A", "B", "A", "C", "B")
221+
grade_factor <- factor(grades, levels = c("A", "B", "C"))
222+
cat("原始等级:", paste(grades, collapse = ", "), "\n")
223+
cat("因子等级:", paste(as.character(grade_factor), collapse = ", "), "\n")
224+
cat("等级计数:\n")
225+
print(table(grade_factor))
226+
cat("\n")
227+
228+
# 缺失值处理
229+
cat("缺失值处理示例 (Missing value handling example):\n")
230+
data_with_na <- c(1, 2, NA, 4, 5, NA, 7)
231+
cat("包含NA的数据:", paste(data_with_na, collapse = ", "), "\n")
232+
cat("是否为NA:", paste(is.na(data_with_na), collapse = ", "), "\n")
233+
clean_data <- data_with_na[!is.na(data_with_na)]
234+
cat("清理后的数据:", paste(clean_data, collapse = ", "), "\n")
235+
cat("NA数量:", sum(is.na(data_with_na)), "\n\n")
236+
237+
# apply函数族示例
238+
cat("apply函数族示例 (apply family example):\n")
239+
test_matrix <- matrix(1:12, nrow = 3, ncol = 4)
240+
cat("测试矩阵:\n")
241+
print(test_matrix)
242+
cat("行求和 (Row sums):", apply(test_matrix, 1, sum), "\n")
243+
cat("列求和 (Column sums):", apply(test_matrix, 2, sum), "\n\n")
244+
245+
# lapply示例
246+
cat("lapply示例 (lapply example):\n")
247+
number_list <- list(a = 1:5, b = 6:10, c = 11:15)
248+
list_means <- lapply(number_list, mean)
249+
cat("各组平均值:\n")
250+
print(list_means)
251+
cat("\n")
252+
253+
# 随机数生成
254+
cat("随机数生成示例 (Random number generation example):\n")
255+
set.seed(123) # 设置随机种子以获得可重复结果
256+
random_normal <- rnorm(5, mean = 0, sd = 1)
257+
cat("正态分布随机数:", paste(round(random_normal, 2), collapse = ", "), "\n")
258+
random_uniform <- runif(5, min = 0, max = 10)
259+
cat("均匀分布随机数:", paste(round(random_uniform, 2), collapse = ", "), "\n")
260+
random_integers <- sample(1:100, 5)
261+
cat("随机整数:", paste(random_integers, collapse = ", "), "\n\n")
262+
263+
# 日期和时间
264+
cat("日期时间示例 (Date and time example):\n")
265+
current_date <- Sys.Date()
266+
current_time <- Sys.time()
267+
cat("当前日期:", as.character(current_date), "\n")
268+
cat("当前时间:", as.character(current_time), "\n")
269+
cat("格式化日期:", format(current_date, "%Y年%m月%d日"), "\n\n")
270+
271+
# 简单的数据分析
272+
cat("数据分析示例 (Data analysis example):\n")
273+
analysis_data <- c(23, 45, 56, 78, 32, 67, 89, 12, 34, 56, 78, 90, 23, 45)
274+
cat("分析数据:", paste(analysis_data, collapse = ", "), "\n")
275+
cat("描述性统计:\n")
276+
cat(" 样本数:", length(analysis_data), "\n")
277+
cat(" 均值:", round(mean(analysis_data), 2), "\n")
278+
cat(" 中位数:", median(analysis_data), "\n")
279+
cat(" 标准差:", round(sd(analysis_data), 2), "\n")
280+
cat(" 最小值:", min(analysis_data), "\n")
281+
cat(" 最大值:", max(analysis_data), "\n")
282+
cat(" 范围:", max(analysis_data) - min(analysis_data), "\n\n")
283+
284+
# 相关性分析
285+
cat("相关性分析示例 (Correlation analysis example):\n")
286+
x_values <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
287+
y_values <- c(2, 4, 6, 8, 10, 12, 14, 16, 18, 20)
288+
correlation <- cor(x_values, y_values)
289+
cat("x值:", paste(x_values, collapse = ", "), "\n")
290+
cat("y值:", paste(y_values, collapse = ", "), "\n")
291+
cat("相关系数:", round(correlation, 4), "\n\n")
292+
293+
# 线性回归示例
294+
cat("线性回归示例 (Linear regression example):\n")
295+
# 使用更现实的数据,添加一些噪声
296+
set.seed(42)
297+
x_reg <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
298+
y_reg <- 2 * x_reg + 1 + rnorm(10, mean = 0, sd = 0.5) # 添加噪声
299+
lm_model <- lm(y_reg ~ x_reg)
300+
cat("x值:", paste(x_reg, collapse = ", "), "\n")
301+
cat("y值:", paste(round(y_reg, 2), collapse = ", "), "\n")
302+
cat("回归摘要:\n")
303+
cat("系数:\n")
304+
print(coef(lm_model))
305+
cat("R平方:", round(summary(lm_model)$r.squared, 4), "\n\n")
306+
307+
# 环境信息
308+
cat("环境信息示例 (Environment info example):\n")
309+
cat("R版本:", R.version.string, "\n")
310+
cat("平台:", R.version$platform, "\n")
311+
cat("工作目录:", getwd(), "\n")
312+
cat("搜索路径长度:", length(search()), "\n\n")
313+
314+
# 内存使用
315+
cat("内存使用示例 (Memory usage example):\n")
316+
cat("对象数量:", length(ls()), "\n")
317+
memory_info <- gc()
318+
cat("内存清理完成\n\n")
319+
320+
cat("CodeForge R 代码执行完成!\n")
321+
cat("CodeForge R execution completed!\n\n")
322+
cat("感谢使用 CodeForge 代码执行环境!\n")
323+
cat("Thank you for using CodeForge!\n")

src-tauri/src/plugins/manager.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use crate::plugins::nodejs::NodeJSPlugin;
1616
use crate::plugins::php::PHPPlugin;
1717
use crate::plugins::python2::Python2Plugin;
1818
use crate::plugins::python3::Python3Plugin;
19+
use crate::plugins::r::RPlugin;
1920
use crate::plugins::ruby::RubyPlugin;
2021
use crate::plugins::rust::RustPlugin;
2122
use crate::plugins::scala::ScalaPlugin;
@@ -56,6 +57,7 @@ impl PluginManager {
5657
plugins.insert("css".to_string(), Box::new(CssPlugin));
5758
plugins.insert("svg".to_string(), Box::new(SvgPlugin));
5859
plugins.insert("php".to_string(), Box::new(PHPPlugin));
60+
plugins.insert("r".to_string(), Box::new(RPlugin));
5961
plugins.insert(
6062
"javascript-nodejs".to_string(),
6163
Box::new(JavaScriptNodeJsPlugin),

src-tauri/src/plugins/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@ pub mod nodejs;
387387
pub mod php;
388388
pub mod python2;
389389
pub mod python3;
390+
pub mod r;
390391
pub mod ruby;
391392
pub mod rust;
392393
pub mod scala;

0 commit comments

Comments
 (0)