11

My question is quite simple, but I couldn't find the answer anywhere. How do I open a .rdb file using R?

It is placed inside an R package.

MT0
  • 143,790
  • 11
  • 59
  • 117
python_enthusiast
  • 896
  • 2
  • 7
  • 26
  • 3
    What do you mean not working? What error message did you get? – pogibas Sep 12 '17 at 19:46
  • I mean they do not exist. I am making up functions based on other functions I know that exist, like readRDS (which reads the metadata file) or read.csv (which reads a csv file). – python_enthusiast Sep 12 '17 at 19:52
  • Please see this post related to RDB files. https://stats.stackexchange.com/questions/242431/what-are-rdx-and-rdb-files-for-r – Sagar Sep 12 '17 at 19:53
  • 1
    Ok, then you should edit your question and explain what `rdb` files are. – pogibas Sep 12 '17 at 19:54
  • @Sagar yes, I have seen this, but it does not tell me how to open the file. – python_enthusiast Sep 12 '17 at 19:55
  • @LauraSimonsenLeal - From what I read, it seems you cannot open those files in R. Just thought of sharing though. Good luck. – Sagar Sep 12 '17 at 19:56
  • what package is this ? you can probably easily find the source code, no need to look at rdb files – moodymudskipper Sep 12 '17 at 20:12
  • The package is called Rsafd. I have actually found a function that might help opening the file. It is called lazyLoad. It is at the end of [this link](http://www.quantide.com/ramarro-chapter-06/). I still hav e to figure out how it works. – python_enthusiast Sep 12 '17 at 20:19

2 Answers2

15

I have been able to solve the problem, so I am posting the answer here in case someone needs it in the future.

#### Importing data from .rdb file ####

setwd("path...\\Rsafd\\Rsafd\\data")  # Set working directory up to the file that contains
# your .rds and .rdb files.

readRDS("Rdata.rds")  # see metadata contained in .rds file

# lazyLoad is the function we use to open a .rdb file:
lazyLoad(filebase = "path...\\Rsafd\\Rsafd\\data\\Rdata", envir = parent.frame())
# for filebase, Rdata is the name of the .rdb file.
# envir is the environment on which the objects are loaded.

The result of using the lazyLoad function is that every database contained in the .rdb file shows up in your variable environment as a "promise". This means that the database will not be opened unless you want it to be.

The way to open it is the following:

find(HOWAREYOU)  # open the file named HOWAREYOU
head(HOWAREYOU)  # look at the first entries, just to make sure

Edit: readRDS is not part of the process to open the .rdb file, it is just to look at the metadata. The lazyLoad function indeed opens .rdb files.

python_enthusiast
  • 896
  • 2
  • 7
  • 26
2

Posting a slightly more direct answer since I keep Googling to this Q&A when trying to examine .rdb objects inside an R package (in particular the help/package.rdb file) and not seeing the answer clearly enough.

R keeps the help Rd objects for the installed package pkg at help/$pkg.{rdb,rdx}.

We can load these Rd objects into environment e like so:

lazyLoad(
  file.path(system.file("help", package=pkg), pkg),
  envir = e
)

Note that we can't use system.file("help", pkg, package=pkg) because system.file() requires the file to exist or it returns "", and here we've truncated the .rdb/.rdx extension as required by lazyLoad().

We can skip supplying envir=e, but the objects will be loaded into the global environment (assuming you're running this interactively) and I wanted my default answer to avoid polluting it.

See ?lazyLoad for more.

MichaelChirico
  • 33,841
  • 14
  • 113
  • 198