python_enthusiast
python_enthusiast

Reputation: 946

How to open .rdb file using R

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.

Upvotes: 14

Views: 24280

Answers (2)

MichaelChirico
MichaelChirico

Reputation: 34703

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.

Upvotes: 3

python_enthusiast
python_enthusiast

Reputation: 946

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.

Upvotes: 17

Related Questions