Skip to content

A simple method to import specific functions from a customized R script.

License

Notifications You must be signed in to change notification settings

fenditsim/import-specific-functions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Importing Specific Functions from a customized R script to another when being called/sourced

import-specific-fx

Table of Content

Introduction

In this repository, I present a simple method to import specific functions (as well as libraries and variables/data) from a cusomtized R script to another one when being called/sourced.

Example

Suppose there are three scripts:

  • Analysis (script_1.R and script_2.R): Perform different analyses
  • Function (functions.R): Contains two (or more) if-statements that store libraries, variables, data and customized functions to be used for each analysis R script

Suppose we would like to call functions from functions.R when we work with analysis.R. Hence, in analysis_1.R, we execute the following command:

source(file = "functions.R")

Meanwhile, functions.R examines whether if-statements are satisfied. Specifically, it examines whether the name of R script (analysis_1.R in this case) is being called from matches with the condition, which is as follows:

basename(rstudioapi::documentPath())=='script_1.R'

If the condition is satisfied, then all functions (as well as libraries, variables and data) are loaded to the global environment. The same is applicable for another analysis script (script_2.R in our case).

Remarks

  1. Specific functions must be called within the analysis script. It would not work if the same command is executed on console (as conditions in if-statements trace the basename of script being called from, and it would be NULL for console (which is not a script)).
  2. If the following message is shown in the Console after running, restart R session and try again:
Error in exists(cacheKey, where = .rs.WorkingDataEnv, inherits = FALSE) : 
  invalid first argument
  1. documentPath() and getSourceEditorContext()$path in rstudioapi print the path of the script existing the command (based on my testing, the former did not generate a result 'NULL' when running it within a script).
  2. basename() results in the name of R script being called from.
  3. See the package solution (link here)

References

This repository is inspired by this post on stackoverflow.

About

A simple method to import specific functions from a customized R script.

Topics

Resources

License

Stars

Watchers

Forks

Languages