# Custom R Script

### Add your own custom R functions or variables with R Script

You might want to use your own R functions as part of your data wrangling steps. This is an introduction for such case. If you want to write your own R functions to extract data, please refer to [Write R Script for Data](https://docs.exploratory.io/data_import/r-script-data).

You can write your functions in an R script and register it to your project, then you can start calling the functions just like any other functions.

### Example - **alpha** function from **psych** package

This is an example to use alpha function from [psych](https://cran.r-project.org/web/packages/psych/) R package from Exploratory Desktop.

#### Install Required Package

In this example, psych is already installed as a default dependency package but the function you want to use might be from a package that we don't install as default. In that case, please install the package following this instruction.

[Install R Package](https://docs.exploratory.io/extend-with-r/r_package)

#### Add R Script

Create a new R script from the left tree.

![](https://2850417076-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4HLCK3olgduYoe3RVS%2F-M4oMvCUDQwHTJ0eWi_f%2F-M4oNF76vQRAa4_mL2kR%2Fadd_r_script.png?generation=1586795488657512\&alt=media)

#### Define the Function

Define a function. Please note that the first argument is data frame and the output is also data frame, so that it can be used from command line.

```r
get_alpha <- function(df, attr = "total", ...){
  num_df <- df %>%
    dplyr::select_if(is.numeric)

  # class of first arg in psych::alpha must be only data.frame class
  class(num_df) <- "data.frame"

  alpha_obj <- psych::alpha(num_df, ...)
  alpha_obj[[attr]]
}
```

Then, click "Save" button.

![](https://2850417076-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4HLCK3olgduYoe3RVS%2F-M4oMvCUDQwHTJ0eWi_f%2F-M4oNF7LUClm7H1S0XKw%2Fsave_script.png?generation=1586795488582252\&alt=media)

#### Use It as a Command

You can use the function from a data frame you want to apply. Click 'Add' (Plus) button and click "Custom Command".

![](https://2850417076-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4HLCK3olgduYoe3RVS%2F-M4oMvCUDQwHTJ0eWi_f%2F-M4oNF7Nf8OLKiH4X91W%2Fcommand_mode.png?generation=1586795488741749\&alt=media)

Then you can enter the function in the command line.

![](https://2850417076-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4HLCK3olgduYoe3RVS%2F-M4oMvCUDQwHTJ0eWi_f%2F-M4oNF7Wyg89my1tLQDz%2Frun_command.png?generation=1586795488927839\&alt=media)

#### Step-by-step

Here is a blog post that walk you through how to register the R script and use the functions.

* [Adding Custom R Scripts to Extend Exploratory Desktop](https://blog.exploratory.io/adding-custom-r-scripts-to-extend-exploratory-desktop-a054832b9562#.68ny44np1)
* [Calculating distances between two locations with geosphere package](https://blog.exploratory.io/calculating-distances-between-two-geo-coded-locations-358e65fcafae)
