Wolfram Research

Function Repository Resource:

PersistResourceFunction

Source Notebook

Install a resource function persistently so that it can be used like a built-in function

Contributed by: Jesse Friedman

ResourceFunction["PersistResourceFunction"][rf]

installs the resource function f persistently.

ResourceFunction["PersistResourceFunction"][rf,"Uninstall"]

uninstalls the resource function f.

ResourceFunction["PersistResourceFunction"]["List"]

lists the currently installed resource functions.

Details and Options

Resource function rf can be either a ResourceFunction object, a function name (resolved as described in ResourceFunction), or a list of ResourceFunction objects or function names.
ResourceFunction["PersistResourceFunction"]["name"] is equivalent to ResourceFunction["PersistResourceFunction"][ResourceFunction["name"]].
PersistResourceFunction[rf] is equivalent to PersistResourceFunction[rf, "Install"].
PersistResourceFunction["List"] returns a list of installed ResourceFunction objects.
PersistResourceFunction["UninstallAll"] uninstalls all installed resource functions.
PersistResourceFunction["UninstallAll"] is equivalent to PersistResourceFunction[PersistResourceFunction["List"] , "Uninstall"].
ResourceFunction["PersistResourceFunction"] persists resource functions by storing InitializationValue objects corresponding to their names.
Care should be taken using installed resource functions in code to be shared with or distributed to others, as recipients are unlikely to have the same set of dependent resource functions installed in their own respective environments.
ResourceFunction["PersistResourceFunction"] accepts the option "PersistenceLocation" with default value $PersistenceBase to specify the scope of the persistence.

Examples

Basic Examples

Store ResourceFunction["RainbowText"] persistently:

In[1]:=
ResourceFunction["PersistResourceFunction"]["RainbowText"]
Out[1]=

Use it like a built-in function:

In[2]:=
RainbowText["hello world"]
Out[2]=

The definition persists across kernel sessions:

In[3]:=
Quit
In[4]:=
RainbowText["hello world"]
Out[4]=

Uninstall the function:

In[5]:=
ResourceFunction[
 "PersistResourceFunction"]["RainbowText", "Uninstall"]
Out[5]=

Scope

Multiple resource functions can be installed or uninstalled by supplying a list of ResourceFunction objects or function names as the first argument to PersistResourceFunction:

In[6]:=
ResourceFunction[
 "PersistResourceFunction"][{"RainbowText", ResourceFunction[
ResourceObject[
Association[
    "Name" -> "BirdSay", "ShortName" -> "BirdSay", "UUID" -> "a24858c8-45c5-4020-a8c2-c23bd5d26f04", "ResourceType" -> "Function", "Version" -> "3.0.0", "Description" -> "Have a bird say an expression", "RepositoryLocation" -> URL[
      "https://www.wolframcloud.com/objects/resourcesystem/api/1.0"], "SymbolName" -> "FunctionRepository`$\
55711938e45341c08ca516066b27f30c`BirdSay", "FunctionLocation" -> CloudObject[
      "https://www.wolframcloud.com/obj/9d5d3776-baed-4a5d-bf38-\
e857d53825e1"]], ResourceSystemBase -> "https://www.wolframcloud.com/objects/\
resourcesystem/api/1.0"]]}]
Out[6]=
In[7]:=
BirdSay[RainbowText["rainbow bird"]]
Out[7]=

The currently installed resource functions can be listed with PersistResourceFunction["List"]:

In[8]:=
ResourceFunction["PersistResourceFunction"]["List"]
Out[8]=
In[9]:=
ResourceFunction[
 "PersistResourceFunction"][{"RainbowText", ResourceFunction[
ResourceObject[
Association[
    "Name" -> "BirdSay", "ShortName" -> "BirdSay", "UUID" -> "a24858c8-45c5-4020-a8c2-c23bd5d26f04", "ResourceType" -> "Function", "Version" -> "3.0.0", "Description" -> "Have a bird say an expression", "RepositoryLocation" -> URL[
      "https://www.wolframcloud.com/objects/resourcesystem/api/1.0"], "SymbolName" -> "FunctionRepository`$\
55711938e45341c08ca516066b27f30c`BirdSay", "FunctionLocation" -> CloudObject[
      "https://www.wolframcloud.com/obj/9d5d3776-baed-4a5d-bf38-\
e857d53825e1"]], ResourceSystemBase -> "https://www.wolframcloud.com/objects/\
resourcesystem/api/1.0"]]}, "Uninstall"]
Out[9]=

Options

"PersistenceLocation"

The “PersistenceLocation” option allows for storing resource functions in a location other than $PersistenceBase:

In[10]:=
ResourceFunction["PersistResourceFunction"]["RainbowText", "PersistenceLocation" -> "Cloud"]
Out[10]=

Properties and Relations

The underlying PersistentObject storing a resource function can be found with InitializationObjects:

In[11]:=
ResourceFunction["PersistResourceFunction"]["RainbowText"]
Out[11]=
In[12]:=
InitializationObjects["Global`RainbowText"]
Out[12]=

Possible Issues

Installing a ResourceFunction will overwrite any values currently assigned to the function name:

In[13]:=
RainbowText[_] := "not a rainbow :(";
RainbowText["hello world"]
Out[14]=
In[15]:=
ResourceFunction["PersistResourceFunction"]["RainbowText"]
Out[15]=
In[16]:=
RainbowText["hello world"]
Out[16]=

Neat Examples

Install PersistResourceFunction as a persistent resource function:

In[17]:=
ResourceFunction["PersistResourceFunction"]["PersistResourceFunction"]
Out[18]=
In[19]:=
PersistResourceFunction["RainbowText"]
Out[19]=
In[20]:=
RainbowText["hello world"]
Out[20]=

Resource History

Related Resources

License Information