Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to point to a directory in an R package?

Tags:

r

r-package

I am making my first attempts to write a R package. I am loading one csv file from hard drive and I am hoping to bundle up my R codes and my csv files into one package later.

My question is how can I load my csv file when my pakage is generated, I mean right now my file address is something like c:\R\mydirectory....\myfile.csv but after I sent it to someone else how can I have a relative address to that file?

Feel free to correct this question if it is not clear to others!

like image 231
Mark Avatar asked Dec 11 '09 08:12

Mark


People also ask

How do I go to a directory in R?

You can think of R as having a file explorer window open invisibly in the background. You can see the folder that's open at the moment by typing getwd() at the console. setwd() tells R to open a different folder instead. setwd('../') tells R to go up to a parent directory.

How do I change the package path in R?

To do this two steps needed: Create the file named Renviron (without dot) in the folder \Program\etc\ (Program is the directory where R is installed--for example, for me it was C:\Program Files\R\R-4.0. 0\etc) Insert a line in Renviron with new path: R_LIBS_USER = "C:/R/Library"


2 Answers

You can put your csv files in the data directory or in inst/extdata. See the Writing R Extensions manual - Section 1.1.5 Data in packages.

To import the data you can use, e.g.,

R> data("achieve", package="flexclust")

or

R> read.table(system.file("data/achieve.txt", package = "flexclust"))
like image 105
rcs Avatar answered Sep 26 '22 22:09

rcs


Look at the R help for package.skeleton: this function

automates some of the setup for a new source package. It creates directories, saves functions, data, and R code files to appropriate places, and creates skeleton help files and a ‘Read-and-delete-me’ file describing further steps in packaging.

The directory structure created by package.skeleton includes a data directory. If you put your data here it will be distributed with the package.

like image 42
Michael Dunn Avatar answered Sep 22 '22 22:09

Michael Dunn