在R中运行代码的特定区域

发布于 2025-01-21 03:32:31 字数 586 浏览 0 评论 0原文

我正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷路的信 2025-01-28 03:32:31

我假设您想重新运行代码,因为您更改了 x 的值,对吧?

此外,您似乎不再需要 xy 的变量。

因此,函数可能对您来说是理想的选择:

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-循环,正如其他答案所建议的那样。我只是想解释如何主动重新运行某段代码。

函数的优点是,除了返回变量之外,函数内的所有变量都被删除,因此它们不会阻塞您的环境并使您感到困惑。

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 and y further.

Therefore a function might be ideal for you:

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")

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 a for-loop or a lapply-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.

无可置疑 2025-01-28 03:32:31

只需使用一个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”是为了记录您保存的绘图数量,以便在以后的工作中不会混淆。

Just use a for loop:

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
  }
}

You can save the plots with png("path/filename.png") before every plot. After a plot is done, you must write dev.off() just to close the png file:

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()
  }
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文