Re 运行 R中代码的特定区域
Re run a specific region of the code in 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
感谢您的帮助!
只需使用 for 循环:
nplot <- 3 # number of plot you want
x <- c(0:10) # x, y and z will not be different at this time
y <- c(0,1,0,0,2,2,3,3,4,4,5)
z <- c(rep(100,3), rep(200, 3), rep(300,5))
table <- cbind(x,y,z) # keep going in a table
for (i in 1:nplot){ # how many plots do you want?
if (i==1){ # if is the first case...
plot(table, type = "o") # do the plot
}
else{ # if is the second plot, third plot, ...
x <- x*1.5 # now x takes other values but y and z remains constant
table <- cbind(x,y,z) # make a new table with updated x values
plot(table, type = "o") # just plot that new values
}
}
您可以在每个图之前用 png("path/filename.png")
保存图。绘图完成后,您必须写 dev.off()
只是为了关闭 png 文件:
nplot <- 3 # number of plot you want
x <- c(0:10) # x, y and z will not be different at this time
y <- c(0,1,0,0,2,2,3,3,4,4,5)
z <- c(rep(100,3), rep(200, 3), rep(300,5))
table <- cbind(x,y,z) # keep going in a table
for (i in 1:nplot){ # how many plots do you want?
if (i==1){ # if is the first case...
png(paste("yourpath/filename_",i,".png",sep=""))
plot(table, type = "o") # do the plot
dev.off()
}
else{ # if is the second plot, third plot, ...
x <- x*1.5 # now x takes other values but y and z remains constant
table <- cbind(x,y,z) # make a new table with updated x values
png(paste("yourpath/filename_",i,".png",sep=""))
plot(table, type = "o") # just plot that new values
dev.off()
}
}
请注意,png中的“i”是为了记录您保存的绘图数量,以免在以后的工作中混淆。
我假设您想重新运行代码,因为您更改了 x 的值,对吗?
您似乎也不需要 x
和 y
的变量。
因此,一个函数可能最适合您:
make_my_table <- function(x){
y <- c(0,1,0,0,2,2,3,3,4,4,5)
z <- c(rep(100,3), rep(200, 3), rep(300,5))
table <- cbind(x,y,z)
# return says to return this variable, if the function is called
return(table)
}
x <- c(0:10)
# first Plot
# make_my_table(x) will execute the function with the value for x and return the table
plot(make_my_table(x), type = "o")
# change of x
x <- 1.5*x
# second plot
plot(make_my_table(x), type = "o")
我不知道你想多久重复一次绘图,但如果有一系列定义的 x
值,你可以在 for
循环或 lapply
-loop,正如其他答案所建议的那样。我只是想解释一下如何主动重新运行某段代码。
函数的优点是除了return变量外,函数内的所有变量都被删除,所以它们不会阻塞你的环境并使你感到困惑。
我在 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
感谢您的帮助!
只需使用 for 循环:
nplot <- 3 # number of plot you want
x <- c(0:10) # x, y and z will not be different at this time
y <- c(0,1,0,0,2,2,3,3,4,4,5)
z <- c(rep(100,3), rep(200, 3), rep(300,5))
table <- cbind(x,y,z) # keep going in a table
for (i in 1:nplot){ # how many plots do you want?
if (i==1){ # if is the first case...
plot(table, type = "o") # do the plot
}
else{ # if is the second plot, third plot, ...
x <- x*1.5 # now x takes other values but y and z remains constant
table <- cbind(x,y,z) # make a new table with updated x values
plot(table, type = "o") # just plot that new values
}
}
您可以在每个图之前用 png("path/filename.png")
保存图。绘图完成后,您必须写 dev.off()
只是为了关闭 png 文件:
nplot <- 3 # number of plot you want
x <- c(0:10) # x, y and z will not be different at this time
y <- c(0,1,0,0,2,2,3,3,4,4,5)
z <- c(rep(100,3), rep(200, 3), rep(300,5))
table <- cbind(x,y,z) # keep going in a table
for (i in 1:nplot){ # how many plots do you want?
if (i==1){ # if is the first case...
png(paste("yourpath/filename_",i,".png",sep=""))
plot(table, type = "o") # do the plot
dev.off()
}
else{ # if is the second plot, third plot, ...
x <- x*1.5 # now x takes other values but y and z remains constant
table <- cbind(x,y,z) # make a new table with updated x values
png(paste("yourpath/filename_",i,".png",sep=""))
plot(table, type = "o") # just plot that new values
dev.off()
}
}
请注意,png中的“i”是为了记录您保存的绘图数量,以免在以后的工作中混淆。
我假设您想重新运行代码,因为您更改了 x 的值,对吗?
您似乎也不需要 x
和 y
的变量。
因此,一个函数可能最适合您:
make_my_table <- function(x){
y <- c(0,1,0,0,2,2,3,3,4,4,5)
z <- c(rep(100,3), rep(200, 3), rep(300,5))
table <- cbind(x,y,z)
# return says to return this variable, if the function is called
return(table)
}
x <- c(0:10)
# first Plot
# make_my_table(x) will execute the function with the value for x and return the table
plot(make_my_table(x), type = "o")
# change of x
x <- 1.5*x
# second plot
plot(make_my_table(x), type = "o")
我不知道你想多久重复一次绘图,但如果有一系列定义的 x
值,你可以在 for
循环或 lapply
-loop,正如其他答案所建议的那样。我只是想解释一下如何主动重新运行某段代码。
函数的优点是除了return变量外,函数内的所有变量都被删除,所以它们不会阻塞你的环境并使你感到困惑。