[R] User input(unknown name and number of files)
Joshua Wiley
jwiley.psych at gmail.com
Fri Jul 22 22:48:57 CEST 2011
On Fri, Jul 22, 2011 at 12:30 PM, Bansal, Vikas <vikas.bansal at kcl.ac.uk> wrote:
> Thanks for your reply.That is why I wrote in the message that I have sent this message before but did not get any help (first line of my message).
> can you please tell me that if user will input 50 files or 100 how my code will work.Do I need to write file1,file2 till file 100 in this code?
No, the user will have to write out all 100 of their file names.
## Inner function
reader <- function(x) {
paste(letters[x], collapse = "")
}
## Outer function
outerfoo <- function(name) {
int <- lapply(name, function(n) {
grep(strsplit(tolower(n), "")[[1]][1], letters)
})
stuff <- lapply(int, function(i) {
lapply(list(c(35, 47), c(38, 37, 31, 36, 42), c(31, 41),
c(42, 30, 23, 42), c(32, 43, 41, 42), c(24, 27, 25, 23, 43, 41, 27),
c(47, 37, 43), c(42, 37, 34, 26), c(35, 27), c(47, 37, 43),
c(45, 27, 40, 27), c(41, 38, 23, 35, 35, 31, 36, 29), c(35, 47),
c(35, 23, 31, 34, 24, 37, 46), c(26, 37, 27, 41), c(36, 37, 42),
c(35, 23, 33, 27), c(31, 42), c(37, 33, 23, 47)), `-`, i)})
output <- lapply(stuff, function(x) {
paste(unlist(lapply(x, reader)), collapse = " ")
})
return(output)
}
## Example (here is what the user will write)
## My example function is designed to work with names
outerfoo(c("Joshua", "Wiley"))
>
> lapply(c("file1", "file2", etc.), yourfunction)
> ________________________________________
> From: Joshua Wiley [jwiley.psych at gmail.com]
> Sent: Friday, July 22, 2011 8:26 PM
> To: Bansal, Vikas
> Cc: r-help at r-project.org
> Subject: Re: [R] User input(unknown name and number of files)
>
> On Fri, Jul 22, 2011 at 12:15 PM, Bansal, Vikas <vikas.bansal at kcl.ac.uk> wrote:
>> Dear all,
>>
>> I need your help as I was not able to find out the solution.I sent this message before but did not get any help.Please help me.
>
> You only sent the message yesterday!!! (then again a few hours ago,
> and *again* just now)
>
> rather than hard code what you are doing, create a function that takes
> a data file and outputs the type of data you want. Then just:
>
> lapply(c("file1", "file2", etc.), yourfunction)
>
> will create a list of all the output.
>
> Josh
>
>>
>> The thing is-
>> I am having a code which is reading file with this code-
>>
>> df=read.table("Case2.pileup",fill=T,sep="\t",colClasses="character")
>> but as am making a tool so that user can use it and can do analysis on his file.But the name of the file will not be Case2.pileup and I want to use this code so that user can input as many files as he want.My code is like this-
>>
>> df=read.table("Case2.pileup",fill=T,sep="\t",colClasses="character")
>> df$V9 <- apply(df, 1, function(x) gsub("\\:|\\$|\\^|!|\\-|1|2|3|4|5|6|7|8|10", "",x[9]))
>> df$V10 <- sapply(df$V10, function(a)
>> paste(as.integer(charToRaw(a)), collapse = ' '))
>> capture.output(print.data.frame(df,row.names=F), file = "end.txt", append = FALSE)
>>
>> I know it should do it with for loop and an array.I want that if user input 12 files,the dataframe name df should be different for all the 12 files.
>>
>> Can you please tell me how can I do this.
>>
>>
>> Thanking you,
>> Warm Regards
>> Vikas Bansal
>> Msc Bioinformatics
>> Kings College London
>> ______________________________________________
>> 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.
>>
>
>
>
> --
> Joshua Wiley
> Ph.D. Student, Health Psychology
> University of California, Los Angeles
> https://joshuawiley.com/
>
--
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
https://joshuawiley.com/
More information about the R-help
mailing list