How to preserve changes to function with fix() between R sessions?

If I edit a function with R v2.14.0 using fix(), those fixes are applied during the session.

For example, I might make the following edit to get a white background in a hive plot:

> library(HiveR)
> fix(plotHive)
... :%s/black/white/g
... :w
... :q
> plotHive(myHiveData)

I then get a white background in the hive plot, as expected.

But if I quit and reopen R, I have lost those changes, and the plot has a black background again.

How do I preserve the edits I make with fix() between R sessions?

EDIT

If I source() the modified plotHive() function, I get the following error:

> modifiedPlotHive <- source("modifiedPlotHive.R")

Error in source("modifiedPlotHive.R") : 
  modifiedPlotHive.R:1160:1: unexpected '<'
1159: }
1160: <
      ^
In addition: Warning message:
In readLines(file) : incomplete final line found on 'modifiedPlotHive.R'

The final line in the modified plotHive() function is:

<environment: namespace:HiveR>

If I remove this line before source()-ing, then the function no longer works.

Answers


Here's the safer way of doing what you want, referenced by @joran.

The sink/source pair is fine for dealing with R code files. But saving to text files and then reading back in other types of objects can strip them of important attributes, especially those relating to environments. That's what you just experienced.

The save/load pair stores objects in R's own binary format, so is much less liable to lose important information/environments attached to functions.

In this example, I define a personal version of ls, which differs from the base function in that it by default lists objects that start with a dot/period:

my_ls <- ls
fix(my_ls)
# 1) On the first line, change 'all.names=FALSE' to 'all.names=TRUE'
# 2) Say "Yes", I want to save the changes
save("my_ls", file="my_ls.Rdata")

# Then, in a later session, test that it works
load("my_ls.Rdata")
.TrysToHide <- 99
my_ls()
# [1] ".TrysToHide" "my_ls"  

One more note: it's much cleaner to give your modified function a name of its own. To really edit a packaged function, and have the changes persist, you'd need to edit the sources and recompile the package. But if you do that, beware, as you may well break the function for other packaged functions that depend on it.


Sorry I missed this when it came out, but the latest version of HiveR has the option to control the background color (available on CRAN 0.2-1) Bryan


There are a couple of options:

  1. Save your workspace before quiting and load it again when you reopen R.
  2. Save the modified function to script file and source it:

    sink("modified_plotHive.r")
    plotHive
    sink()
    

In the next session:

    plotHive <- source("modified_plotHive.r")

HTH


Need Your Help

Testing Hoodie.js app on mobile: What URL to use?

mobile mobile-safari ip-address

Previously I was testing my app on mobile by using my IP followed by whichever localhost I was on. For instance, if I was on localhost:3000, I would plug 172.20.10.2:3000 into my mobile safari URL ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.