-3

Say, if we can save and load an exact same data from .R files, then why came the need for .RData. I tried figuring out some explanation from [R] foo.RData or foo.r?. So, I stumbled upon few queries:

  • Does .RData saves only final result or complete code just a .R scripts?
  • What is their exact relevance? Which one to prefer over other and when?
sync11
  • 1,224
  • 2
  • 10
  • 23
  • 4
    `.RData` is supposed to save the data structure (from memory), where as `.R` has scripts, or declaration. Am I wrong? – drmariod Aug 28 '17 at 11:14
  • Relevant post: https://stackoverflow.com/questions/21370132/r-data-formats-rdata-rda-rds-etc – zx8754 Aug 28 '17 at 11:37
  • @zx8754 I have gone through this post already and this ain't talk anywhere about how `.RData` is related/different from `.R`. – sync11 Aug 28 '17 at 11:42
  • 1
    Great, just mentioned the link for other readers to give more info about RData... In any case you are comparing apples with oranges. – zx8754 Aug 28 '17 at 11:53
  • 3
    .RData and its relatives are a binary representation of objects you save to this file from your R session. It can be related to .R, but not necessarily, i.e. some .R script can produce .RData files. But they can also be produced "by hand" without any script. It's just apples and oranges. – Roman Luštrik Aug 28 '17 at 12:33

2 Answers2

3

RData saves objects, not scripts — if you load it, you load objects inside your environment. It does not contain the code used to produce these elements.

A .R is a script without any object in it — if you open it, you'll see code and you'll need to source it to get the objects produced by the .R.

I would advice to use them this way

  • .R : store functions, and scripts used to create an object (for the sake of reproducibility, for example in /data-raw in packages)
  • use .RData to store objects you'll need later

This is basically how a package works : a /R folder with functions, and a /data folder containing the data objects necessary to the package.

Cath
  • 23,906
  • 5
  • 52
  • 86
Colin FAY
  • 4,849
  • 1
  • 12
  • 29
  • so do both of them compliments each other? – sync11 Aug 28 '17 at 11:22
  • @Vivek yes. To sum up: one is for saving objects (.RData), the other is for saving code (.R). – Colin FAY Aug 28 '17 at 11:36
  • I guess u can use what ever ending you want, but the mentioned one is the most common one. `.RData` files get `load()` into your R session, the `.R` files get `source()` into your session. – drmariod Aug 28 '17 at 11:51
0

In .R file you can save R code, in .RData file you can save data structures from R, eg vector, matrix, data frame or linear model.

rozowawoda
  • 27
  • 7