18) Remove Variables in R.

This Blog entry is from the Getting Started in R section in Learn R.

In the event that long and complex scripts are being processed, where the objects might be using a substantial amount of memory (such as a large table from a database), it may be prudent to remove the objects when the script no longer needs it. 

To remove an object, the remove() function is used taking an argument as the name of the variable to be removed.  In this example, the String variable will be removed.  Type:

remove("String")
using-the-remove-function-in-a-r-script.png

Run the line of script to console:

remove-function-string-written-out-to-r-console.png

It can be seen that the String variable no longer appears in the environment pane:

remove-function-rstudio-environment-window.png

Naturally the variable will not be available in the session upon inspection of the ls() function.  Type:

ls()
r-script-with-ls-to-confirm-variable-removed.png

Run the line of script to console:

ls-function-written-out-to-r-console.png

It can be observed that the return is now minus the String variable.

17) List Variables in R

This Blog entry is from the Getting Started in R section in Learn R.

While RStudio will display the variables in the session at a given point in time,  the function can be replicated to console also.  The ls() function,  which has hitherto been used to identify the functions in a package, is by default used to identify objects in the session.  In the script editor,  type:

ls()
the-ls-function-being-used-in-an-r-script.png

Run the line of script to console:

the-ls-function-written-out-to-r-console.png

The variable names are returned to the console.  To reference these,  it is simply a matter of typing the variable name:

String
taking-a-return-value-from-the-ls-function.png

Run the line of script to console:

writing-a-string-value-out-to-r-console.png