将 for 循环内的 R 命令和结果传送到文件

发布于 2024-08-17 12:12:43 字数 887 浏览 4 评论 0原文

我们希望将 R 脚本的命令和结果记录到文本报告文件中。文本文件中的管道可以与 sink() 配合使用,但不能在 for 循环内使用。

我们需要

source("myscript.r",echo=TRUE)

循环将 data.frame 的所有行连续提取到向量中,并对每个向量进行一些基于向量的分析。 这是一个简短的示例:

#pipe output to file
sink("myfile.txt",append=TRUE,split=TRUE)
#some data
c1<-rnorm(10,mean=90,sd=10) 
c2<-rnorm(10,mean=75,sd=8)
c3<-rnorm(10,mean=98,sd=12)
#data in a data.frame
cData<-data.frame(c1,c2,c3)
#print data.frame
cData  
#loop over frame 
for (i in 1:ncol(cData))  
{
  #extract vector
  x<-cData[,i]
  #do something with vector
  n = length(x)
  #... more code
  #print result
  print(n)    
}
#close output
sink()

我使用 sink()txtStart() 进行了尝试,但 sink() 截断了命令并将结果放在循环之后, txtStart() 似乎重复了命令,但没有重复结果。

我也看了brew,但我只需要一个文本文件,没有任何格式化。

We want to log the commands and results of a R script into a text report file. The pipe into the text file works well with sink(), but not within a for loop.

The script is called with

source("myscript.r",echo=TRUE)

We need the loop to extract all rows of a data.frame consecutively into a vector and do some vector based analysis with each vector.
Here's a short example:

#pipe output to file
sink("myfile.txt",append=TRUE,split=TRUE)
#some data
c1<-rnorm(10,mean=90,sd=10) 
c2<-rnorm(10,mean=75,sd=8)
c3<-rnorm(10,mean=98,sd=12)
#data in a data.frame
cData<-data.frame(c1,c2,c3)
#print data.frame
cData  
#loop over frame 
for (i in 1:ncol(cData))  
{
  #extract vector
  x<-cData[,i]
  #do something with vector
  n = length(x)
  #... more code
  #print result
  print(n)    
}
#close output
sink()

I tried it with sink() and txtStart() but sink() truncates the commands and puts results after the loop, txtStart() seems to repeat the commands but not the results.

I looked also at brew, but I just need a text file, nothing formatted.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

孤芳又自赏 2024-08-24 12:12:43

我建议不要考虑

source("myscript.r",echo=TRUE)

Rscript(R 附带的)

Rscript myscript.r     # on windows, linux or os x

littler

r myscript.r           # on linux or os x

这使您能够查询命令行参数(通过 CRAN 包 getopt 和 optparse)等等。

I would recommend not to think in terms of

source("myscript.r",echo=TRUE)

but rather in terms of Rscript (which comes with R)

Rscript myscript.r     # on windows, linux or os x

or in terms of littler

r myscript.r           # on linux or os x

This gives you the ability to query command-line parameters (via CRAN packages getopt and optparse) and much more.

鹊巢 2024-08-24 12:12:43

一些想法:

循环之后的结果是 R 提供输出的标准方式 - 这是因为 R 在表达式完成之前不会执行循环。这不是 sink 特有的问题。要看到这一点,请执行除 sink 命令之外的所有代码片段(因此输出将转到 R 控制台),您将看到相同的效果。 添加一行

cat("Results for iteration", i, "\n")

为了清楚起见,您可以在 for 循环的开头


。正如 juba 指出的,在这种情况下,您似乎不需要 for 循环 - 向量化或 apply 会更好。


在 R 中将代码和输出组合成某种形式的报告的标准方法是使用 Sweave。这将创建 Latex 标记,然后您可以将其编译成 PDF 或 PostScript 文档(一旦您设置了一些 Latex 工具,只需很少的努力)。这样做的明显优点是您也可以包含数字。


编辑:还有 brew 包< /a> 用于混合文本和代码报告。

A few thoughts:

The results coming after the loop is the standard way for R to provide output – this is because R does not execute the loop until the expression is complete. It is not a problem that is specific to sink. To see this, execute all of your code snippet except the sink commands (so the output goes to the R console) and you will see the same effect. For clarity, you could add a line like

cat("Results for iteration", i, "\n")

at the start of your for loop.


As juba pointed out, you don't seem to need a for loop in this instance – either vectorisation or apply would be better.


The standard way of combining code and output into some form of report in R is to use Sweave. This will create Latex markup that you can then compile into a PDF or PostScript document (with reasonably little effort, once you have some Latex tools set up). The obvious advantage of this is that you can include figures as well.


EDIT: There is also the brew package for a mixed text and code report.

仅此而已 2024-08-24 12:12:43

我不确定您到底想要什么,特别是为什么要将源代码放入文本文件中。

首先,我最好使用 *apply 系列函数来浏览数据框,而不是 for 循环,尤其是 lapply 和 sapply。

使用以下代码,您将获得三种不同的输出演示。第一个应该与您通过代码得到的类似。第二个和第三个有点不同,它们以列表或数据帧的形式输出值。

sink("/tmp/myfile.txt", append=TRUE, split=TRUE)

myfunc <- function(v) {
  variance <- var(v)
  mean <- mean(v)
  return(list(variance=variance,mean=mean))
}

myotherfunc <- function(v) {
  cat("And the mean is:\n")
  print(mean(v))
  cat("And the variance is:\n")
  print(var(v))
}

# results printed directly
invisible(lapply(cData, myotherfunc))

# results as a list
lapply(cData, myfunc)

# results as a data frame
sapply(cData, myfunc)


sink()

无论如何,我不知道这些输出之一是否是您正在寻找的。

I'm not sure to understand exactly what you want, in particular why you want to put the source code in your text file.

First, I'd better use the *apply family of functions to browse the data frame instead of a for loop, especially lapply and sapply.

With the following code you will get three different presentations of your output. The first one should be similar to what you get with your code. The second and third ones are a bit different, they output values as a list or a data frame.

sink("/tmp/myfile.txt", append=TRUE, split=TRUE)

myfunc <- function(v) {
  variance <- var(v)
  mean <- mean(v)
  return(list(variance=variance,mean=mean))
}

myotherfunc <- function(v) {
  cat("And the mean is:\n")
  print(mean(v))
  cat("And the variance is:\n")
  print(var(v))
}

# results printed directly
invisible(lapply(cData, myotherfunc))

# results as a list
lapply(cData, myfunc)

# results as a data frame
sapply(cData, myfunc)


sink()

Anyway, I don't know if one of these outputs could be what you're looking for.

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