Code Monkey home page Code Monkey logo

import-specific-functions's Introduction

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.

import-specific-functions's People

Contributors

fenditsim avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.