Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R-project filepath from concatenation

Tags:

function

r

I'm working through an R tutorial. I've been working on a function and one of the parts of the function is to take an argument and use it to define a directory in which to find data. It must then load that data.

As it stands the following works:

getmonitor <- function(id, directory){

csvfile <- function(id) {
    if (id < 10) { 
        paste0(0,0,id,".csv")
    } else if (id < 100) {
        paste0(0,id,".csv")
    } else paste0(id,".csv")
}

foo <- read.csv(csvfile(id))

}

Fine. But I now have to use the "directory" parameter to define the directory where the csv file must be read from. I've tried various things here to no avail.

Currently, the code works if the assumption is that the data are in the working directory. I need to say "go to the directory called (directory) and then read.csv.

The directory with all of the data files is called "specdata" and the parameter for directory is thus "specdata".

I tried the following:

getmonitor <- function(id, directory){

  csvfile <- function(id) {
      if (id < 10) { 
          paste0(0,0,id,".csv")
      } else if (id < 100) {
          paste0(0,id,".csv")
      } else paste0(id,".csv")
  }

  filepath <- append(directory,"/",csvfile(id))

  foo <- read.csv(filepath)

 }

But then I received an error message "Error in !after : invalid argument type "

I have tried a few various things and if I cut and paste all the code it would probably be more messy than help.

What would be a logical way to do this? Am I on the right track with append? What else should I sue if not? I need to take the parameter "directory" and then load data from that directory.

like image 909
Doug Fir Avatar asked Jan 15 '13 23:01

Doug Fir


People also ask

How do I set the path of a file in R?

Under Windows and MAC OSX Launch R by double-clicking on the icon. Specify your working directory to R: On Windows: File –> Change directory. On MAC OSX: Tools –> Change the working directory.

How do I concatenate a path in R?

To concatenate strings in r programming, use paste() function. The syntax of paste() function that is used to concatenate two or more strings.

How do I find the location of a file in R?

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. (You can do this using the Graphical User Interface in RStudio).

What is relative path in R?

Relative file paths. When R starts a session, it has a location to look for other files. This path is called the current working directory, and this is often shortened to the working directory. Relative paths in a program are specified as starting at the current working directory.


1 Answers

getmonitor <- function(id, directory=getwd(), ...){

  csvfile <- sprintf("%03d.csv", id)

  filepath <- file.path(directory, csvfile)

  foo <- read.csv(filepath, ...)

  foo

 }
like image 108
sebastian-c Avatar answered Oct 13 '22 01:10

sebastian-c