【发布时间】: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)
提供以下输出
新发现
当你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