Retrieve path of supplementary data file of developed package - r

While developing a package I encountered the problem of supplementary data import - this has been 'kind of' solved here.
Nevertheless, I need to make use of a function of another package, which needs a path to the used file. Sadly, using GlobalEnvironment variables here is not an option.
[By the way: the file needs to be .txt, while supplementary data should be .RData. The function is quite picky.]
So I need to know how to get the path supplementary data file of a package. Is this even possible to do?
I had the idea of reading the .RData into the global environment and then saving it into a tmpfile for further processing. I would really like to know a clean way - the supplementary data is ~100MB large...
Thank you very much!

Use system.file() to reliably find the path to the installed package and sub-directories, typically these are created in your-pkg-source/inst/extdata/your-file.txt and then referenced as
system.file(package="your-pkg", "extdata", "your-file.txt")

Related

How to add CSS external files to be used by my R package function?

I'm making a personal package and would like to load a css file in one of the functions in this package in order to have a "default style". What is the optimal way of doing this?
I cannot find any documnetation that helps me understand how to work with external data like a css file, mostly what I find is about exporting external datasets to be loaded separately, etc.
I found the problem. I was using the inst/extdata folder to export my style.css file, but was trying to read it directly in the code. Thanks to #MrFlick docpage, I found out I had to use the system.file() function in order to use the exported file.

read in xls file in r from business objects

I have downloaded a xls file from business objects and want to read it in R.
I have tried several options, the easiest one being:
library("readxl")
txt=read_excel("file.xls", sheet = 2)
The problem is that it gives me an empty tibble. However, if I open the xls file, do absolutely nothing, save it and try again, it does work!
Since I need to make a data pipeline I want it to work right away without this weird workaround.
Any idea what the problem is? My own thoughts went to some kind of security, read-only, adminstrator permission kind of property but couldn't figure it out.
Kind regards!
Piet
I always try to avoid importing .xls files due to such problems. Where possible I always import it as a .csv file. Depending on the structure of the .xls file, however, this is not always possible or may be extra work if you have many tabs within your .xls file.
If possible, export your .xls as an .csv file and then import it using read.table() or use a function through the many available packages such as data.table or tidyverse.
I don't know much, but it's a bug with the package. You can go down to readxl 1.0.0 and it works.
GitHub issue mentioning dropping versions: https://github.com/tidyverse/readxl/issues/474
How to go down to the version you want: https://support.rstudio.com/hc/en-us/articles/219949047-Installing-older-versions-of-packages
You can use the package data.table that provides a very easy and faster method to read and write .csv or .xls/.xlsx with the fwrite and fread functions. It package already has an automatic separate detector.
You cand find more information about this package here.

"filename.rdata" file Exploring and Converting to CSV

I'm no R-programmer (because of the problem I started learning it), I'm using Python, In a forcasting task I got a dataset signalList.rdata of a pheomenen called partial discharge.
I tried some commands to load, open and view, Hardly got a glimps
my_data <- get(load('C:/Users/Zack-PC/Desktop/Study/Data Sets/pdCluster/signalList.Rdata'))
but, since i lack deep knowledge about R, I wanted to convert it into a csv file, or any type that I can deal with in python.
or, explore it and copy-paste manually.
so, i'm asking for any solution whether using R or Python or any tool to get what's in the .rdata file.
Have you managed to load the data successfully into your working environment?
If so, write.csv is the function you are looking for.
If not,
setwd("C:/Users/Zack-PC/Desktop/Study/Data Sets/pdCluster/")
signalList <- load("signalList.Rdata")
write.csv(signalList, "signalList.csv")
should do the trick.
If you would like to remove signalList from your working directory,
rm(signalList)
will accomplish this.
Note: changing your working directory isn't necessary, it just makes it easier to read in a comment I feel. You may also specify another path for saving your csv to within the second argument of write.csv.

Renaming files in RStudio that have been sourced other places

I have a few R files that contain functions imported and used by several other R files. I import these functions with the source function. Naturally, the scope of a particular file might change over time, and recently I wanted to rename a file I had already sourced in many other places.
I'm using RStudio, and I have been unable to find a way to do this except for either manually updating each dependent file, or creating some external code to scan through the files.
Is there no way to do consistent renaming in RStudio? Alternatively, am I doing something wrong by using source to add functions?
You may or may not find this satisfactory. Create a parent script with the old name that sources the script with the new name.
Extending this, you could just create a general preamble script, called something like "preamble.R", that sources all general utility scripts you have. Such an approach is common (I believe) with TeX. Then you only have one place to update file names.

How to point to a directory in an 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!
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"))
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.

Resources