在R中运行代码的特定区域
我正在 R 中工作,我需要重新运行我正在工作的行上方编写的代码区域。我的意思是,只是代码的特定部分,几行。例如:
1 x <- c(0:10)
2
3 #Start of region to rerun ----------------------
4
5 y <- c(0,1,0,0,2,2,3,3,4,4,5)
6 z <- c(rep(100,3), rep(200, 3), rep(300,5))
7 table <- cbind(x,y,z)
8
9 #End of region to rerun ------------------------
10
11
12 plot(table, type = "o") #plot numer1
13
14 #Modification of one variable
15 x <- x*1.5
16
17 # I would like tu rerun the region above, form line 3 to line 9
18
19
20 plot(table, type = "o") #plot numer2
谢谢您的帮助!
I'm working in R and I need to re-run a region of the code that is written above the line where I 'm working at. I mean, just a specific part of the code, a few lines. For example :
1 x <- c(0:10)
2
3 #Start of region to rerun ----------------------
4
5 y <- c(0,1,0,0,2,2,3,3,4,4,5)
6 z <- c(rep(100,3), rep(200, 3), rep(300,5))
7 table <- cbind(x,y,z)
8
9 #End of region to rerun ------------------------
10
11
12 plot(table, type = "o") #plot numer1
13
14 #Modification of one variable
15 x <- x*1.5
16
17 # I would like tu rerun the region above, form line 3 to line 9
18
19
20 plot(table, type = "o") #plot numer2
Thank you for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我假设您想重新运行代码,因为您更改了 x 的值,对吧?
此外,您似乎不再需要
x
和y
的变量。因此,函数可能对您来说是理想的选择:
我不知道您想要重复绘图的频率,但如果存在定义的一系列
x
值,您可以在中执行绘图函数for
-循环或lapply
-循环,正如其他答案所建议的那样。我只是想解释如何主动重新运行某段代码。函数的优点是,除了返回变量之外,函数内的所有变量都被删除,因此它们不会阻塞您的环境并使您感到困惑。
I assume you want to rerun the code because you changed the value of x, right?
also you do not seem to need the variables of
x
andy
further.Therefore a function might be ideal for you:
I dont know how often you want to repeat the plot, but if there is a defined series of
x
values, you can execute the plot function within afor
-loop or alapply
-loop, as the other answer suggest. I just wanted to explain how one can actively rerun a certain piece of code.The advantage of a function is that except for the return variable, all variables within the function are deleted, so they do not clog your environment and confuse you.
只需使用一个for循环:
您可以在每个图之前使用
png(“ path/filename.png”)
保存图。图完成后,您必须编写dev.off()
才能关闭png文件:请注意,PNG中的“ I”是为了记录您保存的绘图数量,以便在以后的工作中不会混淆。
Just use a for loop:
You can save the plots with
png("path/filename.png")
before every plot. After a plot is done, you must writedev.off()
just to close the png file:Note that "i" in png is written to record the number of plot you're saving in order to not confuse in a future work.