[R] accessing files from subfolders

Henrique Dallazuanna wwwhsd at gmail.com
Thu Jun 9 00:05:51 CEST 2011


Try this:

lapply(dir("myPathDir", recursive = TRUE, pattern = "myFile.txt$",
full.names = TRUE), readLines, warn = FALSE)

On Wed, Jun 8, 2011 at 6:35 PM, J <jonsleepy at gmail.com> wrote:
> Hi,
>   There must be an easy way to do this, but I'm not finding it..
>
> I'd just like to know the syntax to move up and down folder levels, without necessarily entering a full file path.  Also, how to construct file and folder paths using variables.
>
> For example 1, if I wanted to print to the screen the contents of a file called myFile.txt using the bash shell, I'd use the following:
>
> cat ../myFile.txt
>
> Also, for example 2, if I want to cd into a folder that contains my files, from within a loop, where the counter serves as one of the folders in the path.
>
> In bash:
>
> for i in {1..5} A B C # A, B, and C are also folder names
> do
> cd ~/${i} # move into (change working directory to?) my folder of interest, which is 1,2,3,4,5,A,B,or C
> cat myFile.txt # print corresponding file of interest to screen
> cd - # move back to the previous folder
> done
>
>
> I wonder if there's an easy corresponding way to accomplish this in R.
>
> Any ideas most welcome!
>
> Jonathan
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O



More information about the R-help mailing list