[英]Can you convert an R raw vector representing an RDS file back into an R object without a round trip to disk?
[英]Convert RDS into R file
我有一個 .rds 文件代碼,我告訴它是一個帶有 R 代碼的文件。 我嘗試使用readRDS
訪問它並且能夠查看輸出。 我可以知道如何將 .rds 轉換為 .r 文件或任何可編輯的格式,以便我可以進行一些編輯並查看代碼? 我曾嘗試尋找解決方案,但找不到任何相關的解決方案。
鏈接到 .rds 文件https://drive.google.com/file/d/1SGgKA1ejkF7_uq_27E6Qpaq_fdAcDL8O/view?usp=sharing
假設 rds 文件包含 R 代碼,而不是數據(它可以,因為 rds 格式可以保存任何對象,包括函數和表達式)......那么,我們可以使用dput
將內容導出為人類可讀的格式。
這是一個簡單的例子:
hello = function() {
print("Hello world")
}
saveRDS(hello, 'hello.rds')
dput(readRDS('hello.rds'), file='hello.r')
文件hello.r
現在包含以下內容:
function ()
{
print("Hello world")
}
如果我們對您的原始文件(即dput(readRDS('Fundamental_Model.rds'), file='hello.r')
)做同樣的事情,我們會得到:
list(Fundamental_Model = structure(list(handle = <pointer: (nil)>,
raw = as.raw(c(0x00, 0x00, 0x00, 0x3f, 0x0b, 0x00, 0x00,
# ... many lines omitted
0x00, 0x00, 0x00, 0x6e, 0x69, 0x74, 0x65, 0x72, 0x03, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x31, 0x32, 0x38)), niter = 129,
call = xgb.train(data = df_train, nrounds = best.nrounds,
paras = xgb.params), params = list(paras = list(colsample_bytree = 0.85,
subsample = 0.85, booster = "gbtree", max_depth = 6,
eta = 0.03, eval_metric = "rmse", objective = "reg:linear",
gamma = 0), silent = 1), callbacks = list(cb.print.evaluation = structure(function (env = parent.frame())
{
if (length(env$bst_evaluation) == 0 || period == 0 ||
NVL(env$rank, 0) != 0)
return()
i <- env$iteration
if ((i - 1)%%period == 0 || i == env$begin_iteration ||
i == env$end_iteration) {
stdev <- if (showsd)
env$bst_evaluation_err
else NULL
msg <- format.eval.string(i, env$bst_evaluation,
stdev)
cat(msg, "\n")
}
}, call = cb.print.evaluation(period = print_every_n), name = "cb.print.evaluation")),
feature_names = c("X6", "X7", "X8", "X9", "X10", "X11", "X12",
"X13", "X14", "X15", "X16"), nfeatures = 11L), class = "xgb.Booster"))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.