<p>I am trying to run a nextflow pipeline but the output file is not created.</p>
<p>The main.nf file looks like this:</p>
<pre><code>#!/usr/bin/env nextflow
nextflow.enable.dsl=2 

process my_script {

    &quot;&quot;&quot;
    Rscript script.R
    &quot;&quot;&quot;
}

workflow {
  my_script 
}
</code></pre>
<p>In my nextflow.config I have:</p>
<pre><code>
process {
   executor = 'k8s'
   container = 'rocker/r-ver:4.1.3'
}
</code></pre>
<p>The script.R looks like this:</p>
<pre><code> FUN &lt;- readRDS(&quot;function.rds&quot;);
 input = readRDS(&quot;input.rds&quot;);
 output = FUN(
singleCell_data_input = input[[1]], savePath = input[[2]], tmpDirGC = input[[3]]
);
 saveRDS(output, &quot;output.rds&quot;)
</code></pre>
<p>After running <strong>nextflow run main.nf</strong>  the output.rds is not created</p>

Denisa Buzan
<p>Nextflow processes are run independently and isolated from each other from inside the working directory. For your script to be able to find the required input files, these must be localized inside the process working directory. This should be done by defining an <a href="https://www.nextflow.io/docs/latest/process.html#inputs" rel="nofollow noreferrer"><code>input</code></a> block and declaring the files using the <a href="https://www.nextflow.io/docs/latest/process.html#input-type-path" rel="nofollow noreferrer"><code>path</code></a> qualifier, for example:</p>
<pre><code>params.function_rds = './function.rds'
params.input_rds = './input.rds'


process my_script {

    input:
    path my_function_rds
    path my_input_rds

    output:
    path &quot;output.rds&quot;

    &quot;&quot;&quot;
    #!/usr/bin/env Rscript

    FUN &lt;- readRDS(&quot;${my_function_rds}&quot;);
    input = readRDS(&quot;${my_input_rds}&quot;);
    output = FUN(
      singleCell_data_input=input[[1]], savePath=input[[2]], tmpDirGC=input[[3]]
    );
    saveRDS(output, &quot;output.rds&quot;)
    &quot;&quot;&quot;
}

workflow {

    function_rds = file( params.function_rds )
    input_rds = file( params.input_rds )

    my_script( function_rds, input_rds )
    my_script.out.view()
}
</code></pre>
<p>In the same way, the script itself would need to be localized inside the process working directory. To avoid specifying an absolute path to your R script (which would not make your workflow portable at all), it's possible to simply embed your code, making sure to specify the Rscript shebang. This works because process scripts are not limited to Bash<sup><a href="https://www.nextflow.io/docs/latest/process.html#scripts-a-la-carte" rel="nofollow noreferrer">1</a></sup>.</p>
<p>Another way, would be to make your Rscript executable and move it into a directory called <code>bin</code> in the the root directory of your project repository (i.e. the same directory as your 'main.nf' Nextflow script). Nextflow automatically adds this folder to the <code>$PATH</code> environment variable and your script would become automatically accessible to each of your pipeline processes. For this to work, you'd need some way to pass in the input files as command line arguments. For example:</p>
<pre><code>params.function_rds = './function.rds'
params.input_rds = './input.rds'


process my_script {

    input:
    path my_function_rds
    path my_input_rds

    output:
    path &quot;output.rds&quot;

    &quot;&quot;&quot;
    script.R &quot;${my_function_rds}&quot; &quot;${my_input_rds}&quot; output.rds
    &quot;&quot;&quot;
}

workflow {

    function_rds = file( params.function_rds )
    input_rds = file( params.input_rds )

    my_script( function_rds, input_rds )
    my_script.out.view()
}
</code></pre>
<p>And your R script might look like:</p>
<pre><code>#!/usr/bin/env Rscript

args &lt;- commandArgs(trailingOnly = TRUE)

FUN &lt;- readRDS(args[1]);
input = readRDS(args[2]);
output = FUN(
  singleCell_data_input=input[[1]], savePath=input[[2]], tmpDirGC=input[[3]]
);
saveRDS(output, args[3])
</code></pre>
