[R] Sweave, SweaveHooks and printing the label
I would like to print the label of each code chunk, just before printing the code chunk itself. Is that possible using SweaveHooks, or by some other mechanism? Thanks David -- View this message in context: http://r.789695.n4.nabble.com/Sweave-SweaveHooks-and-printing-the-label-tp3453829p3453829.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] Storing user-defined R functions
Thanks to everyone for writing. A well-known phenomenon in mathematics, statistics and/or complex computing is that everything one already knows feels trivial and easy. It's as though one is permanently climbing a vertical cliff-face, while, if you look back from where you've come, you see a level plateau just a couple of inches lower than one's boots. I take statements about how easy it is to make packages with a pinch of salt---I'm sure it's easy when you already know how. I've been looking through http://127.0.0.1:31257/doc/manual/R-exts.html#Creating-R-packages http://127.0.0.1:31257/doc/manual/R-exts.html#Creating-R-packages with a mixture of bafflement and horror. Even to skim this document without understanding all that much of it would take me a couple of hours. To make my first package might take me a couple of days, or maybe longer. To follow David Scott's suggestion would take me 5 minutes max. And I have a huge workload with looming deadlines It might be different if there were a Dummies' guide to packages, allowing one to rapidly assemble a a few easy functions, and omitting all the optional features---a guide to the first steps up the cliff-face. If there is a Dummies' guide, I would be glad to hear of it. David -- View this message in context: http://r.789695.n4.nabble.com/Storing-user-defined-R-functions-tp3402983p3404751.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
[R] Storing user-defined R functions
Hello, I don't want to find out how to make packages unless that becomes necessary. Also, I don't want to clog up the computer memory with functions that I'm not using. (It would be great if someone in this forum would explain how memory is used when I type library(MASS) and then use only one function from MASS. Are all the many MASS functions then residing in memory, or only the one I called?) Is there some standard way of 1. storing the R functions that I define, each in a separate file in some standard directory, and then 2. calling one of these functions without having to include the path to the relevant directory? 3. If so, are there conventional places to keep such files, akin to /usr/local/bin in Unix? I'm thinking of a facility like Matlab's, when one has user-defined paths where Matlab will look for my Matlab functions. In my Matlab code, I don't need to hard-wire in the path of the storage directory, and I can make the function call as though my function is in the current directory. Thanks, David -- View this message in context: http://r.789695.n4.nabble.com/Storing-user-defined-R-functions-tp3402983p3402983.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
[R] Sweave, white space and code blocks
Sweave is very useful, and I'm gradually getting used to it. I've just been battling Sweave over the re-use of code chunks. As I am pretty ignorant in the byways of both Sweave and R, this took a chunk of time to sort out. Here is what I learned: If one re-uses a code chunk, then Sweave (but not Stangle) will insist that start in column 1. In particular, white space to its left is not allowed. I can't find any mention of this in the Sweave manual, nor in the Sweave FAQ. I'm planning to ask for this to be included in the Sweave FAQ, as it has certainly been frequently asked by me today. David -- View this message in context: http://r.789695.n4.nabble.com/Sweave-white-space-and-code-blocks-tp3393811p3393811.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
[R] Convert Sweave document to a function
I like Sweave, which I consider to be a great contribution. I have just written a .Rnw document that comes to about 6 pages of mixed code and mathematical explanation. Now I want to turn the R code into a function. My R code currently contains statements like N-1000 and theta- pi/10. In the next version of the document, I want N and theta to be parameters of a function, so that they can be easily varied. My explanation of the code is still valid, and it seems to me that, if I only knew how to manage the trick, I would need to change almost nothing in the latex. The document contains about 6 different code chunks, and 7 different chunks of latex. I tried putting functionname - function(N,theta) { into the first code chunk and } into the last code chunk, but Sweave said this was poor grammar and rejected it. Is there a reasonable way to make my .Rnw source into a function definition? I would like maintainability of the code to be a criterion for reasonable, and I would like to keep latex explanations of what the code is doing adjacent to the code being explained. One other point is that I will want to export some of the variables computed in the function to outside the function, so that they are not variables local to the function body. I mention this only because it may affect the solution, if any, to my problem. Thanks for any help David -- View this message in context: http://r.789695.n4.nabble.com/Convert-Sweave-document-to-a-function-tp3391654p3391654.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
[R] puzzles with assign()
I'm trying to get code along the following lines to work: temp.name - paste(TimePt,'df',sep='.') # invent a relevant name/symbol as a character string. assign(temp.name,IGF.df[IGF.df$TPt==TimePt,]) # this works. The relevant variable is now a data frame lm(b ~ Strain+BWt+PWt+PanPix, data=temp.name)) # this gives an error, namely Error in eval(predvars, data, env) : invalid 'envir' argument I think it's obvious what I want to achieve, but how is it done? I tried data=as.name(temp.name) but that also didn't work. I can't find anything relevant in Introduction to R. Here is a secondary question: While trying to understand what assign() does, I looked up help(assign) and found the example a - 1:4 assign(a[1], 2) a[1] == 2 #FALSE get(a[1]) == 2 #TRUE Could someone explain this puzzling example, or point me to an explanation of environments and how to operate with them? Thanks -- View this message in context: http://r.789695.n4.nabble.com/puzzles-with-assign-tp2130691p2130691.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] puzzles with assign()
Duncan Murdoch-2 wrote: ...snip... That's because constructing names like this is generally a bad idea. But you can do it; you use get() to get the object whose name is in temp.name. So put data=get(temp.name) into your lm() call. I had another useful answer (sent privately) from Patrick Burns. His R inferno seems like a helpful source of information. Here's a quote from page 12 that has been very helpful in simplifying my code and making it more comprehensible. The key seems to be to index a list by character strings. Patrick Burns wrote: A fairly common question from new users is: “How do I assign names to a group of similar objects?” Yes, you can do that, but you probably don’t want to—better is to vectorize your thinking. Put all of the similar objects into one list. Subsequent analysis and manipulation is then going to be much smoother. Thanks to both. David -- View this message in context: http://r.789695.n4.nabble.com/puzzles-with-assign-tp2130691p2130887.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
[R] Sweave: centering with echo=TRUE
In a .Rnw file I want to insert the R command pairs(mydataframe) and achieve the following effects 1. the command itseld is echoed into the tex document generated by Sweave fig=TRUE,echo=TRUE= 2. The graphics generated appears in the tex document, with the graphics centred. 3. The R command pairs(mydataframe) is not centered. Sweave-manual.pdf gives the following code chunk results=tex,echo=FALSE= for(i in 1:4){ file=paste(myfile, i, .eps, sep=) postscript(file=file, paper=special, width=6, height=6) plot(rnorm(100)+i) dev.off() cat(\\includegraphics{, file, }\n\n, sep=) } @ This could obviously be adapted to my centering problem, but is it the simplest way? It seems complicated compared with just pairs(mydataframe). Also, Sweave will normally make both postscript and pdf plots, but this would involve further complication in the code. I'm sure it's completely standard to produce two copies of graphics, one a pdf and one an eps, but it's another obstacle in my way when I want to get on with writing the program and its latex description. Thanks David -- View this message in context: http://r.789695.n4.nabble.com/Sweave-centering-with-echo-TRUE-tp2065018p2065018.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
[R] unorder an ordered factor
Given an ordered factor, how does one unorder it? I tried various commands with reorder, with order=F, but they all gave me error messages. I also tried to drop the order with various 'as dot something' commands, but these didn't work either. -- View this message in context: http://n4.nabble.com/unorder-an-ordered-factor-tp2014745p2014745.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
[R] simple question about contrasts, lm and factors
I have a data frame with two variables that are factors. One is actually a TRUE/FALSE factor, and I have coded it as 1/0, a continuous variable, but I could turn it back into a factor. The second is an ordered factor and consists of five timepoints. There are several continuous variables as well. Now I want to fit a linear model to my data, using lm (or another R procedure if recommended). Question: should I use polynomial contrasts? My timepoints are very far from being evenly spaced, so ordinary R contrasts seem more natural. But I'm totally inexperienced (this is my first serious regression). I also want to choose my base value. In the first call to lm, I want to choose base value equal to FirstTimePoint. In my second call to lm, I want to choose base value to be the interaction term FirstTimePoint:FALSE or FirstTimePoint:0. Does all this make sense? Please excuse my inexperience if it doesn't. If it makes sense, then there must be two simple calls to lm. But I'm floundering. Thanks a lot David -- View this message in context: http://n4.nabble.com/simple-question-about-contrasts-lm-and-factors-tp1835964p1835964.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] perhaps regular expression bug with | sign ??
William Dunlap wrote: sub(x='|t|',pattern = '\|t',replacement='zz') [1] zz|t| Warning messages: 1: '\|' is an unrecognized escape in a character string 2: unrecognized escape removed from \|t How can \| be an unrecognized escape? This flatly contradicts help('regex'), It would be a bit clearer if the warnings indicated that they were from the R parser (the function that converts your text input to R expressions which are later evaluated). The parser is trying to say that it is treating \| as |. The backlash has special meaning in things like \n (newline), \t (tab), and \123 (character number in octal), but not before a vertical bar. What is the procedure for requesting an enhancement to code in the base package? I would like to see the error message changed to something a bit less puzzling to non-experts. '\|' is not permitted in a character string. Did you mean '\\|' or '|'? -- View this message in context: http://n4.nabble.com/perhaps-regular-expression-bug-with-sign-tp1819872p1836437.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] how does one print code
I downloaded the code, as Duncan Murdoch suggested. I also used sink() as suggested by others and found that the two methods gave identical results. I then fixed the bug in a private file and tried it out on a number of examples. It now seems to work fine, as far as I can tell. From CRAN, I found a publication date in 2005, and the author's email address. Should I do anything else, in addition to writing to the author, which I have already done? Thanks David -- View this message in context: http://n4.nabble.com/how-does-one-print-code-tp1788686p1810973.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
[R] perhaps regular expression bug with | sign ??
Here is my interaction with R: sub(x='|t|',pattern = '|t',replacement='zz') [1] zz|t| So I say to myself Clearly the | signs need to be escaped, so let's try this sub(x='|t|',pattern = '\|t',replacement='zz') [1] zz|t| Warning messages: 1: '\|' is an unrecognized escape in a character string 2: unrecognized escape removed from \|t How can \| be an unrecognized escape? This flatly contradicts help('regex'), or am I misunderstanding the help? The first pattern above works if one uses extended=F. What do R experts think? David -- View this message in context: http://n4.nabble.com/perhaps-regular-expression-bug-with-sign-tp1819872p1819872.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
[R] how does one print code
There is quite a long piece of code defining a certain function in one of the R packages. I think the code has a bug and I want to get the code into a file so that I can take a proper look, and possibly fix it. how does one do this? (I mean getting the code into a file, not fixing the bug.) I suppose I could copy and paste, but that's a bit error prone for various reasons. I want the same arrangement of code formatting as in the original---copy and paste often messes this up. I tried using as.character(functionname) but that wasn't at all appreciated. I tried searching the archives of this forum but couldn't figure out exactly what to search for (got too many hits). I also tried Google, but that was also no help. Thanks David -- View this message in context: http://n4.nabble.com/how-does-one-print-code-tp1788686p1788686.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
[R] appending an R-object to a list
How do I append an R-object to a list? I want to start with an empty list, and append R-objects one by one. Does this start with a command like mylist - NULL ?? I have read a few answers on R-help to questions like this, but they all seem to be well off the point. Sometimes it's assumed that the list is a vector---not my case. One answer I read said that the object appended must be a list. This doesn't make sense to me. I don't want a list of lists. I want a list of R-objects. I tried using c(with.stuff.inside.the.brackets). This seemed to unwrap my structures, storing the individual components rather than the R-objects themselves. I tried looking in Intro to R but could find nothing relevant. Thanks for any help. David -- View this message in context: http://n4.nabble.com/appending-an-R-object-to-a-list-tp1753544p1753544.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] appending an R-object to a list
Thanks. A nice simple answer and exactly what I want. David -- View this message in context: http://n4.nabble.com/appending-an-R-object-to-a-list-tp1753544p1753665.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.