Quantcast

Writing a function, want a string argument to define the name of the excel sheet to be called

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Writing a function, want a string argument to define the name of the excel sheet to be called

AOLeary
My question is this: is there a way I can make one of the arguments of the function be a string the user can enter, and then have that be the excel filename? ie,

foo <- function(x,y,NAME){

#make a matrix with x rows and y cols
M <- matrix(nrow=x,ncol=y)
#write the matrix
write.table(M, file = "result.csv",append=TRUE, sep = ",")
}

I've had a look but I couldn't find help for this particular problem and it's one I'd like to solve, so I can change make several excel files to solve the analysis my actual function does.

Thank you very much,
Aodhán
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Writing a function, want a string argument to define the name of the excel sheet to be called

Bryan Hanson
Sure, change your example as follows and then you can pass the name properly:

foo <- function(x,y,NAME = "filename.csv"){

#make a matrix with x rows and y cols
M <- matrix(nrow=x,ncol=y)
#write the matrix
write.table(M, file = NAME,append=TRUE, sep = ",")
}

Bryan
****************
Prof. Bryan Hanson
Dept of Chemistry & Biochemistry
DePauw University
Greencastle IN 46135 USA
academic.depauw.edu/~hanson/deadpezsociety.html
github.com/bryanhanson
academic.depauw.edu/~hanson/UMP/Index.html

On Dec 1, 2011, at 8:43 AM, AOLeary wrote:

> My question is this: is there a way I can make one of the arguments of the
> function be a string the user can enter, and then have that be the excel
> filename? ie,
>
> foo <- function(x,y,NAME){
>
> #make a matrix with x rows and y cols
> M <- matrix(nrow=x,ncol=y)
> #write the matrix
> write.table(M, file = "result.csv",append=TRUE, sep = ",")
> }
>
> I've had a look but I couldn't find help for this particular problem and
> it's one I'd like to solve, so I can change make several excel files to
> solve the analysis my actual function does.
>
> Thank you very much,
> Aodhán
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Writing-a-function-want-a-string-argument-to-define-the-name-of-the-excel-sheet-to-be-called-tp4128384p4128384.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Writing a function, want a string argument to define the name of the excel sheet to be called

AOLeary
Thank you very much, that does exactly what I want it to! :)

Aodhán
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Writing a function, want a string argument to define the name of the excel sheet to be called

Michael Weylandt
Just a heads up -- I don't think your code will work with an actual
.xls(x) file, only .txt, .csv, etc (aka, plain text files). I may be
wrong about that, but if you actually need to work with Excel files
directly you will need an additional package.

Michael

On Thu, Dec 1, 2011 at 9:10 AM, AOLeary <[hidden email]> wrote:

> Thank you very much, that does exactly what I want it to! :)
>
> Aodhán
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Writing-a-function-want-a-string-argument-to-define-the-name-of-the-excel-sheet-to-be-called-tp4128384p4128495.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Writing a function, want a string argument to define the name of the excel sheet to be called

AOLeary
Thanks Michael, I'll keep that in mind if I want to do anything more complicated.
Loading...