# Source another R script in the \`bin/\` directory

**URL:** <https://community.seqera.io/t/source-another-r-script-in-the-bin-directory/1059>\
**Category:** Tips & Tricks\
**Created:** [August 6, 2024, 8:54am UTC](https://community.seqera.io/t/source-another-r-script-in-the-bin-directory/1059 "2024-08-06T08:54:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ChrisHakk](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/chrishakk/32/21_2.png) [@ChrisHakk](https://community.seqera.io/u/ChrisHakk)\
**Post date:** [August 6, 2024, 8:54am UTC](https://community.seqera.io/t/source-another-r-script-in-the-bin-directory/1059/1 "2024-08-06T08:54:12Z")

</div>

When a process runs, your base directory is the working directory and R files are not within the same folder. Instead, the R files have been added to the `PATH` variable, which means you can call them but they are not directly accessible using source or any other local file operations.

One potential solution is to add the `bin/` path as a variable.

​If we have an R file in `bin/` called `source.R`:

```R
hello_world <- function() {
  print("Hello World!")
}

```

To source this file we can read the `PATH` variable, take the last value (which is the `bin/` added by Nextflow), and import the `source.R` file from this path explicitly.

```groovy
process IMPORT_R {
    container 'docker.io/library/r-base:4.3.1'

    output:
        stdout

    script:
    """
    #!/usr/bin/Rscript --vanilla

    path <- Sys.getenv("PATH") |> strsplit(":")
    bin_path <- tail(path[[1]], n=1)
    source(file.path(bin_path, "source.R"))

    hello_world()
    """
}

```

Another solution could be to combine your R scripts into one file or use the R file as an input to the process.

---

<div class="post-metadata">

**Author:** ![Adam\_Talbot](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/adam_talbot/32/62_2.png) [@Adam\_Talbot](https://community.seqera.io/u/Adam_Talbot)\
**Post date:** [August 8, 2024, 8:56am UTC](https://community.seqera.io/t/source-another-r-script-in-the-bin-directory/1059/2 "2024-08-08T08:56:06Z")

</div>

I’ve been playing around with the box library, which attempts to solve this problem but I haven’t been able to get it to read the `PATH` automatically.

Still, it’s possible if you put your source R script in a subfolder in `bin/`:

```console
> tree
├── bin
│ └── my_r_scripts
│ └── source.R
└── main.nf

```

main.nf:

```nextflow
process RUN_R {
    debug true

    output:
    stdout

    script:
    """
    #!Rscript --vanilla

    # Set base path of Box library
    path <- Sys.getenv("PATH") |> strsplit(":")
    bin_path <- tail(path[[1]], n=1)
    options(box.path = bin_path)
 
    # Load hello_world from bin/my_r_scripts/source.R
    box::use(my_r_scripts/source[hello_world]) # Load function
    hello_world()
    """
}

workflow {
    RUN_R()
}

```

---

<div class="post-metadata">

**Author:** ![pontus](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/pontus/32/369_2.png) [@pontus](https://community.seqera.io/u/pontus)\
**Post date:** [April 23, 2025, 6:47am UTC](https://community.seqera.io/t/source-another-r-script-in-the-bin-directory/1059/3 "2025-04-23T06:47:27Z")

</div>

This may work often enough, but I’d be worried something could switch up (e.g. the order `bin` for module binaries and pipelines are added) and getting the wrong path because of that. Seems safer sticking a binary in the corresponding `bin` that you can look up the path of.
