Undo command in R

后端 未结 2 1275
自闭症患者
自闭症患者 2020-12-28 23:08

I can\'t find something to the effect of an undo command in R (neither on An Introduction to R nor in R in a Nutshell). I am particularly interested in undoing/deleting when

相关标签:
2条回答
  • 2020-12-28 23:13

    You should consider a different approach which leads to reproducible work:

    • Pick an editor you like and which has R support
    • Write your code in 'snippets', ie short files for functions, and then use the facilities of the editor / R integration to send the code to the R interpreter
    • If you make a mistake, re-edit your snippet and run it again
    • You will always have a log of what you did

    All this works tremendously well in ESS which is why many experienced R users like this environment. But editors are a subjective and personal choice; other people like Eclipse with StatET better. There are other solutions for Mac OS X and Windows too, and all this has been discussed countless times before here on SO and on other places like the R lists.

    0 讨论(0)
  • 2020-12-28 23:24

    In general I do adopt Dirk's strategy. You should aim for your code to be a completely reproducible record of how you have transformed your raw data into output.

    However, if you have complex code it can take a long time to re-run it all. I've had code that takes over 30 minutes to process the data (i.e., import, transform, merge, etc.). In these cases, a single data-destroying line of code would require me to wait 30 minutes to restore my workspace. By data destroying code I mean things like:

    • x <- merge(x, y)
    • df$x <- df$x^2

    e.g., merges, replacing an existing variable with a transformation, removing rows or columns, and so on. In these cases, it's easy, especially when first learning R to make a mistake.

    To avoid having to wait this 30 minutes, I adopt several strategies:

    • If I'm about to do something where there's a risk of destroying my active objects, I'll first copy the result into a temporary object. I'll then check that it worked with the temporary object and then rerun replacing it with the proper object. E.g., first run temp <- merge(x, y); check that it worked str(temp); head(temp); tail(temp) and if everything looks good x <- merge(x, y)
    • As is common in psychological research, I often have large data frames with hundreds of variables and different subsets of cases. For a given analysis (e.g., a table, a figure, some results text), I'll often extract just the subset of cases and variables that I need into a separate object for the analysis and work with that object when preparing and finalising my analysis code. That way, I'm less likely to accidentally damage my main data frame. This assumes that the results of the analysis does not need to be fed back into the main data frame.
    • If I have finished performing a large number of complex data transformations, I may save a copy of the core workspace objects. E.g., save(x, y, z , file = 'backup.Rdata') That way, If I make a mistake, I only have to reload these objects.
    • df$x <- NULL is a handy way of removing a variable in a data frame that you did not want to create

    However, in the end I still run all the code from scratch to check that the result is reproducible.

    0 讨论(0)
提交回复
热议问题