简体   繁体   English

将 RDS 转换为 R 文件

[英]Convert RDS into R file

I had a .rds file code which im told it is a file with R codes.我有一个 .rds 文件代码,我告诉它是一个带有 R 代码的文件。 I tried to access it by using readRDS and is able to view the output.我尝试使用readRDS访问它并且能够查看输出。 May I know how can I convert the .rds into .r file or any editable format so that I could make some edits and view the codes?我可以知道如何将 .rds 转换为 .r 文件或任何可编辑的格式,以便我可以进行一些编辑并查看代码? I have tried looking for solutions but I could not find any related ones.我曾尝试寻找解决方案,但找不到任何相关的解决方案。

Link to the .rds file https://drive.google.com/file/d/1SGgKA1ejkF7_uq_27E6Qpaq_fdAcDL8O/view?usp=sharing链接到 .rds 文件https://drive.google.com/file/d/1SGgKA1ejkF7_uq_27E6Qpaq_fdAcDL8O/view?usp=sharing

Assuming the rds file contains R code, rather than data (which it can, because rds format can save any object including functions and expressions)... then, we can use dput to export the contents into a human readable format.假设 rds 文件包含 R 代码,而不是数据(它可以,因为 rds 格式可以保存任何对象,包括函数和表达式)......那么,我们可以使用dput将内容导出为人类可读的格式。

Here's a simple example:这是一个简单的例子:

hello = function() {
  print("Hello world")
}

saveRDS(hello, 'hello.rds')

dput(readRDS('hello.rds'), file='hello.r')

The file hello.r now contains the following:文件hello.r现在包含以下内容:

function () 
{
    print("Hello world")
}

If we do the same thing with your original file (ie, dput(readRDS('Fundamental_Model.rds'), file='hello.r') ), we get:如果我们对您的原始文件(即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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM