I'm creating an R package with several files in /data
. The way one loads data in the R package is to use the system.file()
,
system.file(..., package = "base", lib.loc = NULL, mustWork = FALSE)
The file in /data
I would like to load into an R data.table has the extension *.txt.gz
, my_file.txt.gz
. How do I load this into a data.table via read.table()
or fread()
?
Within the R script, I tried :
#' @import data.table
#' @export
my_function = function(){
my_table = read.table(system.file("data", "my_file.txt.gz", package = "FusionVizR"), header=TRUE)
}
This leads to an error via devtools
document()
:
Error in read.table(system.file("data", "my_file.txt.gz", package = "FusionVizR"), header = TRUE) (from script1.R#7) :
no lines available in input
In addition: Warning message:
In file(file, "rt") :
file("") only supports open = "w+" and open = "w+b": using the former
I appear to get the same issue via fread()
#' @import data.table
#' @export
my_function() = function(){
my_table = fread(system.file("data", "my_file.txt.gz", package = "FusionVizR"), header=TRUE)
}
This outputs the error:
Input is either empty or fully whitespace after the skip or autostart. Run again with verbose=TRUE.
So, it appears that system.file()
doesn't give an object to the file which I could load into an R data.table. How do I do this?
file.exists
before doing the read step. 2. if it does find the file, can read.csv cope with it? Test by running read.csv on the command line with the full path to the file. Perhaps you don't have gzip-reading capability in your R version? – Spacedmandevtools::document()
is running code inside your function unless its running tests or examples which you've not shown us. – Spacedmanlibrary(devtools)
and thendocument()
within the package root directory. Assystem.file()
is documented in Writing R Extensions, this question should be closed as poorly-researched or revised for poor English. – ShanZhengYangmy_function() = function(){
- you don't put()
in the function name: it should be:my_function = function(){
. When I rundocument
on a minimal example I see this warning:"Error in my_function() = function() { (from fnord.R#1) :"
. – Spacedman