Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't load files using system.file or file.path in R?

Tags:

r

I am using a program that requires me to load a "sam" file. However, the code given does not create the data.file variable necessary and instead produces "", a blank instead.

This is the code given:

data.file <- system.file(file.path('extdata', 'vignette-sam.txt'), package='flipflop')

I put in:

data.file <- system.file(file.path("Users", "User1", "Desktop", "Cond_18", 
                                   "Sorted_bam_files", "Cond_18_1.bam_sorted.sam"), 
                         package='flipflop')

The path is definitely correct and the package name is flipflop. However every time I check what the variable data.file is, it produces "". So the file is never being loaded and the script can't run.

I also put in the entire file path into one version of it:

data.file <- system.file('/Users/User1/Desktop/Cond_18/Sorted_bam_files/DBM_18_1.bam_sorted.sam', 
                         package='flipflop')

That version does not include file.path, but it is one of the script examples.

The line in the code that uses these variables is this:

if(preprocess.instance==''){
  print('PRE-PROCESSING sam file ....')
  data.file <- path.expand(path=data.file) # In case there is a '~' in the input path
  if(data.file==''){ print('Did you forget to give a SAM file?') ; return(NULL) } 
  annot.file   <- path.expand(path=annot.file)
  samples.file <- path.expand(path=samples.file)

And since data.file is "" it defaults to NULL.

like image 847
L. Li Avatar asked Jun 27 '17 14:06

L. Li


1 Answers

You use system.file when the file you want to reference is from a package. Here is what the help file for system.file says:

Finds the full file names of files in packages etc.

You just want to reference a file on your machine. You can just directly store the path and use that as your data.file

data.file <- "C:/path/to/your/file"

The script uses file.path because it's a safer way to generate paths in a platform-independent way. For example:

> file.path("C:", "Users", "BotsRule", "Code", "myawesomeRscript.R")
[1] "C:/Users/BotsRule/Code/myawesomeRscript.R"

So the solution to your problem is to just make sure you're actually doing something just anything to correctly specify the path to your file.

like image 50
Dason Avatar answered Oct 14 '22 16:10

Dason