How to open .rdb file using R - 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.

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.

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.

Related

importing excel files in r

i am trying to load an excel file in r studio but each time i run it
Error in read_excel("R/win-library/3.6/IMDB_data.xlsx", sheet = "IMDB_data",
:
could not find function "read_excel" this is displayed.
i have tried changing directory
saving the data to load, in the same as working directory
none of the articles resolve my issues concerned yet
tried changing directory
saving the file in the same place as my working directory
importing through choose directory
setwd("~/R/win-library/3.6")
library(readxl)
IMDB_data <- read_excel("R/win-library/3.6/IMDB_data.xlsx",
sheet = "IMDB_data", skip = 2)
Write R code using data “IMDB_data” to
Load CSV in R by skipping second row.
enter image description here
It seems like your readxl library is not loaded.
Do you get any errors when you run library(readxl)?
Your working folder shouldn't matter and you should probably avoid working in the R's library.
The read_excel command should read the file based on the path provided, but your error is not complaining about the missing file. It's complaining about the missing function.
Lastly, if you set the working directory to ~/R/win-library/3.6, then it would be enough to run the following code (provided your readxl library loaded correctly):
IMDB_data <- read_excel("IMDB_data.xlsx", sheet = "IMDB_data", skip = 2)

How to add external data file into developing R package?

I am building my R packages in Rstudio, I ran into some unexpected problem when I tired to create package' vignette. when I hit build/load panel in Rstudio, I got vignette error, while package's documentation was created. To possibly solve vignette error I got, I have to add external data to my packages, use this data to compile package vignette accordingly. I used devtools::install() command to install my packages, but inst/ directory is not created. extdata must be located in inst directory. I also used devtools::use_data() to add my data from my PC, but I can't able to add my external data. How can I load external data for my packages ? I think I should not manually create extdata and put external data over there. Why inst/ was not created when I used devtools::install() ? How to add set of csv files as external data into my packages ?
This is the toy helper function I am going to use in my vignette to read external data :
myFunc <- function(myDir, ...) {
files <- list.files(myDir, full.names = TRUE, "\\.csv$")
readMe <- lapply(files, read.csv)
return(readMe)
}
This is the first time I build R packages, getting some common error. My apology if my questions is not well stated.
to find files in inst/, I need to use system.file(), but I don't have this directory, plus myFunc accept file directory to to grab the files and read them as .csv, this is toy code chunk could be executed in vignette file :
```{r}
library(myPkg)
file.1 <- system.file("extdata", "xxx.csv", "myPkg")
file.2 <- system.file("extdata", "yyy.csv", "myPkg")
myFunc(list(file.1, file.2))
```
How can I load external data to my packages in order to compile package vignette by using this data ? Why inst/ not created when I hit devtools::install() ? Can anyone help me how to do this ?Thanks in advance :)
You should manually create inst/extdata/file.csv in the base directory for your project (where DESCRIPTION is). You can put all the files you want to access in that directory.
Then to get the files in function examples or your vignette:
files <- lapply(list.files(system.file('extdata', package = 'my_package'), full.names = TRUE), read.csv)
system.file() returns the path to the extdata folder, then list.files() will create a vector of all the files in extdata. Finally, running lapply() with read.csv() should read the contents of all the files into a single list for you.

Reading an Excel file into an R dataframe from a zipped folder

I have an Excel file (.xls extension) that is inside a zipped folder that I would like to read as a dataframe into R. I loaded the gdata library and set up my working directory to the folder that houses the zipped folder.
When I type in the following syntax:
data_frame1 <- read.xls( unz("./Data/Project1.zip","schools.xls"))
I get the following error messages:
Error in path.expand(xls) : invalid 'path' argument
Error in file.exists(tfn) : invalid 'file' argument
I'm guessing that I'm missing some arguments in the syntax, but I'm not entirely sure what else needs to be included.
Thanks for your help! This R newbie really appreciates it!
Unfortunately, after a quick survey of all the xls functions I know, there is no xls reading function that can recognize the unz output (I would love to be proven wrong here). If it were a 'csv' it would work fine. As it stands, until such a function is written, you must do the loading in two steps extraction and then loading.
To give you a little more control, you can specify which file to unzip as well as the directory to place the files with unzip.
# default exdir is current directory
unzip(zipfile="./Data/Project1.zip", files = "schools.xls", exdir=".")
dataframe_1 <- read.xls("schools.xls")
Sadly, this also means that you must do cleanup afterwards if you don't want the 'xls' file hanging around.

Open file generated by loading .rda files

I followed your advice about creating a loop that loads files in R and did:
dataFiles<-lapply(Sys.glob("kwo*.rda*"), load)
Now I have my dataFiles which contains the files I wanted to load
head(dataFiles)
[[1]]
[1] "kw"
[[2]]
[1] "kw"
[[3]]
[1] "kw"
Now I need to work with the information contained in the files I loaded, what should I do to open the files and to 'identify' them?
Standard behavior of load in this kind of loop is to create a temporary environment, load the data into it, and discarding this temporary enviroment again. If you want them in the global environment, you need to explicitly load them into the global environment, see this SO post for more info. This will load all the objects contained in all the .Rda files into your global environment, aka workspace.
Could you provide some more information as to what you are doing exactly? What generated the Rda files, and what do you want to do with that data you read in? More information can help us, help you. And you refer to an earlier SO question your (I followed your advice about creating a loop that loads files in R), I cannot find this question in your profile.

Convert RDA to csv

I need to convert an rda file to csv. I've tried to load it in R , but I get the following error:
In readChar(con, 5L, useBytes = TRUE) :
cannot open compressed file file 'data/matrix.rda', probable reason 'No such file or directory'
Here is a link to rda file (http://elisacarli.altervista.org/matrix.rda)
Thanks in advance for any suggestion
This appears to be an issue of not having the object you are trying to write out to your csv in your working environment. Did you load your .RDA file first? I was able to load your .RDA file into my R session and write it out the LDH.aap.ave object with write.csv() with no apparent problems.
I recommend you check:
What is in your current working environment? Check with ls(). Presumably, the contents of your .RDA file will not be in here. For cleanliness, maybe you want to clear your working environment first and start fresh? rm(list=ls()) will do the trick for you there.
Your current working directory with getwd()
The location of your .RDA file
Navigate to the appropriate directory if needed with setwd()
Use load("my.RDA")
Check the objects in your current working environment with ls(). I see one object in the attached .RDA file named "LDH.aap.ave"
You can check the structure of that object to make sure it was read in properly. head(), str(), summary() are your friends here.
Write our LDH.aap.ave with write.csv(LDH.aap-ave, file = "myFileName.csv")
for starters, if your data is at that url, you needs to open a connection to the url and then load the .rda file:
con <- url('http://elisacarli.altervista.org/matrix.rda')
load(con)
close(con)
if you have the file on your computer, then just:
load('[full path to file]/matrix.rda')
this should create and object called 'matrix', see what is in it by typing:
matrix
then you would use this function:
write.csv(matrix,file="mysavefile.csv")

Resources