9

Couldn't see a solution online but I thought this might be quite common.

  • with write.csv I basically always have the argument row.name set to F. Is it possible to run a line once and update the default value of the argument for the rest of the session?
  • I tried paste <- paste(sep="") which ran and returned no error but seemed to do nothing (and didn't destroy the paste function). This is another one, I always set sep="" with paste...
  • like I always have exclude=NULL when I am using table so I can see the N/A values.

EDIT: So, I'm looking for a solution that will work for multiple functions if possible: paste, write.csv, table and other functions like these.

smci
  • 32,567
  • 20
  • 113
  • 146
nzcoops
  • 9,132
  • 8
  • 41
  • 52

2 Answers2

8

paste <- paste(sep="") puts the output of paste() into an object named "paste". You would need to do something like this instead.

paste <- function (..., sep = "", collapse = NULL) {
  base::paste(..., sep=sep, collapse=collapse)
}

You can also look at the Defaults package for this sort of thing, but it doesn't currently work for two of your examples.

Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
  • 1
    Hmm, I get a `Error: evaluation nested too deeply:` error with `setDefaults(paste, sep="")`. I'll have to look into that... – Joshua Ulrich Jul 11 '11 at 04:09
  • Yeah, and the table part unfortunately doesn't work either. From the help for 'setDefaults' "Assigning NULL to any argument will remove the argument from the Defaults list." – nzcoops Jul 11 '11 at 04:52
  • 2
    With regard to the first part of your answer, why did you do what you did with collapse? – nzcoops Jul 11 '11 at 04:52
  • @nzcoops: I'm not sure what you mean by "why did you do what you did with collapse?" All I did was create a new paste function in the global environment (which is higher on the search path than `package:base`) with the same arguments as `base::paste` but with a different default `sep` argument. – Joshua Ulrich Jul 11 '11 at 12:26
  • 3
    @joshua ...but in your example, you must call base::paste, not just paste! As it is written now, it causes infinite recursion... – Tommy Jul 11 '11 at 16:49
  • 1
    @Tommy: Thanks; good catch. I really should test the code in my answers or simply refrain from answering after midnight... – Joshua Ulrich Jul 11 '11 at 16:52
  • @Joshua testing would be good ;) by the collapse question, I just mean did you have to do that? Or could you take out the collapse argument from both parts of that? Not being picky, just trying to understand :) I've updated the setDefaults part of your answer to since neither parts were working. – nzcoops Jul 12 '11 at 00:46
  • You could remove the collapse argument but I'm not sure if doing so would cause issues with other functions that use `paste`. I'm working with the Defaults author to fix those issues. The code is 4 years old, so it's due for some updating. ;-) – Joshua Ulrich Jul 12 '11 at 00:57
5

Try this:

paste <- paste
formals(paste)$sep <- ""

This creates a new copy of paste in your workspace, and then modifies its default value for sep to "". Subsequent calls to paste will then use the modified copy, as it sits in front of the base environment in your search path.

Hong Ooi
  • 56,353
  • 13
  • 134
  • 187
  • Hmmm, this works for paste but not write.csv or table. Have updated question. – nzcoops Jul 11 '11 at 08:06
  • 1
    @nzcoops For `table` it's tricky (to be clear for set `NULL` as default). Try `formals(table)["exclude"] <- list(NULL)`. And `write.csv` is another story. I recommend to look at source of `write.csv` and `write.csv2` and try to write `write.csv3` to satisfy your needs. – Marek Jul 11 '11 at 23:30
  • 2
    Looking at the source to `write.csv` is likely to make you cry – hadley Jul 12 '11 at 16:04
  • @Anonymous_Downvotes: If you're going to down-vote, at least _hint_ at what makes the answer unhelpful so others can learn and/or the answer can be improved. – Joshua Ulrich Jul 13 '11 at 01:13