【问题标题】:Saving output to text file in markdown将输出保存到markdown中的文本文件
【发布时间】:2019-05-04 06:20:15
【问题描述】:

我一直在Rmarkdown 中使用H2O,当我想将输出保存在文本文件中时,只保存了第一部分(页面),但是在控制台中没问题

我使用了以下代码:

fileConn<-file(".\\h2o.randomForest\\output.txt")
writeLines(capture.output(summary(rf1)), fileConn)
close(fileConn)

如何将所有输出保存到文本文件?

由于再现性要求

您可以在 Rmarkdown 中尝试以下代码

library(h2o)
h2o.init()

# import the iris dataset:
# this dataset is used to classify the type of iris plant
# the original dataset can be found at https://archive.ics.uci.edu/ml/datasets/Iris
iris <-h2o.importFile("http://h2o-public-test-data.s3.amazonaws.com/smalldata/iris/iris_wheader.csv")

# convert response column to a factor
iris['class'] <-as.factor(iris['class'])

# set the predictor names
predictors <-colnames(iris)[-length(iris)]

# split into train and validation
iris_splits <- h2o.splitFrame(data = iris, ratios = .8, seed = 1234)
train <- iris_splits[[1]]
valid <- iris_splits[[2]]

# try using the `estimate_k` parameter:
# set k to the upper limit of classes you'd like to consider
# set standardize to False as well since the scales for each feature are     very close
iris_kmeans <- h2o.kmeans(x = predictors, k = 10, estimate_k = T, standardize = F,
                      training_frame = train, validation_frame=valid, seed = 1234)

# print the model summary to see the number of clusters chosen


fileConn<-file("output2.txt")
writeLines(capture.output(summary(iris_kmeans)), fileConn)
close(fileConn)

提供以下输出

Console output

Rmarkown output

新发现

当你knitRmarkdown 时还可以,但是当run the current chunk 时就异常了。

更新

在 Windows 和 Ubuntu 上结果相同

会话信息:

R version 3.5.1 (2018-07-02)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1258   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

【问题讨论】:

  • 鉴于提供的详细信息,我无法重现此内容。使用 stock iris 示例,显示在控制台中的输出将使用 rmarkdown 文件中的代码输出到文本文件。
  • 好的,我加了。
  • 谢谢:我试过 h2o.randomForest!
  • 好的。我仍然无法重现。控制台的输出与文本文件中的相同。请问您缺少输出的哪一部分?
  • 请在Rmarkdown中复现,并将结果与​​控制台输出进行比较。

标签: r r-markdown


【解决方案1】:

我实际上无法重现您的问题;即,当我使用 R 控制台knitRun Current Chunk 时,它在我身边运行良好。

我推测file 连接和/或writeLines 函数存在(模糊)问题。因此,我建议您使用以下选项之一将输出写入文件。我还建议您在将输出发送到文件之前将其保存到变量中。

选项1:使用cat代替writeLines,如下:

fileConn<-file("output2.txt")
output <- capture.output(summary(iris_kmeans))
cat(output, file=fileConn)
close(fileConn)


选项 2: 使用 catsink,如下所示:

sink("output2.txt")
output <- capture.output(summary(iris_kmeans))
cat(output)
sink()

如果有帮助,请告诉我。

【讨论】:

    【解决方案2】:

    不确定我是否正确理解您想要实现的目标,但如果您愿意

    将所有输出写入文本文件

    最简单的方法是使用 Rscript 并从命令行重定向输出:

    Rscript yourscriptfilename.R > output.txt
    

    或直接来自 R,例如通过 system(或 Windows 上的 shell):

    system("Rscript yourscriptfilename.R > output.txt")
    

    这将运行您的 R 脚本,而不是将输出写入控制台,而是将它们写入 output.txt

    knitr 将summary 渲染到 R Mardown 文件的输出

    只需将summary(iris_kmeans) 添加到 R Markdown 文件中块的底部

    【讨论】:

    • 谢谢,我想知道为什么writeLines(capture.output(summary(iris_kmeans)), fileConn)run the current chunk的行为与控制台不同
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-12
    • 2021-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多