0

similarly to this post and this one (but for rds instead of dput)

let's say that, for some reasons, you had to:

  1. use saveRDS (instead of dput)
  2. load the rds file into a program (as a binary file)
  3. get the content of that binary file as a string back into R

what function(s) would you use to re-build the model in R?

elfersi
  • 121
  • 2
  • 13
  • 4
    I'm not sure what you mean by (3). Saving to RDS and reading back (1/2) should work well to be able to manipulate the model in a subsequent session (provided it was built entirely using `lm(y ~ x, data = dat)` notation. – Thomas Aug 27 '18 at 01:23
  • 3
    It's not clear what is meant by "re-build the model". If you save the lm as RDS, you get the lm back with `readRDS`. If you mean recover the data from which the model was built, then that is part of the model object (`lm1$term` for a model named lm1). – neilfws Aug 27 '18 at 01:37

1 Answers1

2

It's not very clear what you mean by "re-build[ing] the model in R". I assume you want to store the output of a linear model, and then later (perhaps from a fresh R terminal) re-read the object.

Here is a reproducible & minimal example:

# Fit a linear model
fit <- lm(mpg ~ disp, data = mtcars)

# Save as RDS
saveRDS(fit, file = "my_fit.rds")

# Clean up the workspace (or quit R)
rm(fit)

fit <- readRDS("my_fit.rds")
fit
#
#Call:
#lm(formula = mpg ~ disp, data = mtcars)
#
#Coefficients:
#(Intercept)         disp
#   29.59985     -0.04122
Maurits Evers
  • 49,617
  • 4
  • 47
  • 68
  • i get the file back in R as a string. its content is what you'd see if you were to open the RDS file with the notepad. so i can't directly use readRDS. hence the question: how do you re-build the model in R? thanks so much – elfersi Aug 27 '18 at 01:48
  • 1
    @elfersi Your question is not clear. Again, what do you mean by *"re-build the model in R"*. In my example above, `fit` contains the full `lm` output object. **All information about the model and the data is contained in `fit`.** For example, you can access the data with `fit$model`. `saveRDS` stores the R object in a binary file. I'm not sure what you're trying to do with a text editor here. – Maurits Evers Aug 27 '18 at 02:04
  • Let me give some more details. I generated a model in R. I saved it as ab RDS file. I want to consume the model in an interactive Power BI R visual (see more details here: https://stackoverflow.com/questions/51956755/save-and-load-r-lm-model-in-universal-format-e-g-pfa-not-rds). Now, in Power BI service, you can't load files in R visuals. So I can't use readRDS. My workaround is to first load the RDS file in the Power Query interface as a string; and then 'load' that string in the R visual (this is possible). Question: how do I 're-build' the model from the string? Hope this clarifies. Thanks – elfersi Aug 27 '18 at 03:10
  • @elfersi I'm sorry but this is not clear to me at all. An RDS file contains the R object. If you store the output of `lm` in an RDS file, then the RDS file contains model details and data. I don't know what you want to "re-build", and I don't think this word makes sense here. If you mean *re-fitting* the model, this is not necessary because the `lm` object from the RDS file contains the full fitted model. I don't see what this has to do with Power BI. – Maurits Evers Aug 27 '18 at 03:34
  • Unfortunately your comments/clarifications are somewhat obscure. You state that you *"can't use readRDS"*, yet in the next sentence you say that you *"first load the RDS file in the Power Query interface as a string*". So are you or are you not using `readRDS`? If you have an RDS file, you have to read it with `readRDS`. – Maurits Evers Aug 27 '18 at 03:35
  • i cannot use the readRDS command in the Power BI R visual but I can load the RDS file (like i'd load a text file) in the Power Query interface. If you read vhcandido's answer here: https://stackoverflow.com/a/51993480/3847011 , I basically need the same thing but after using saveRDS command instead of a dput command. Can you help? – elfersi Aug 27 '18 at 04:05
  • @elfersi No sorry, I don't understand what you mean. An RDS file is not a text file, so obviously you can't read it as a text file. As I said before, you have to read an RDS file with `readRDS`. **If you can't use `readRDS`, don't save your R object as an RDS file.** I don't think this discussion is going anywhere so I'll stop here. Good luck and perhaps somebody else can help. – Maurits Evers Aug 27 '18 at 04:45