Answers.Body: <p>Your problem is that Snakemake is passing all the input data to the R script simultaneously instead of running them individually :)</p>
  <p>As written your rule takes a list of input files and gives a list of output files (<code>expand</code> is just a formatting helper function that returns a list based on its arguments).  Instead maybe have one rule that calls the R script with one input and one output, and another rule that asks for all those outputs as an easy way run the whole thing at once.</p>
  <p>Maybe something like this?  I changed <code>tables</code> to <code>table</code> here just to emphasize it's one by one.</p>
  <pre><code>rule all:
      input: expand(&quot;results/{table}_edited.txt&quot;, table = [&quot;1&quot;, &quot;2&quot;])
  
  rule somerule:
      input:
          table = &quot;results/{table}_table.txt&quot;
      output:
          edit = &quot;results/{table}_edited.txt&quot;
      conda: &quot;../envs/r.yaml&quot;
      script: &quot;../scripts/script.R&quot;
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-10-18T18:14:37.190Z
Answers.Text: thank you so much. That worked!
Answers.UserDisplayName: Classy Q
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-10-18T13:42:30.567Z
Answers.LastActivityDate: 2023-10-21T14:57:34.473Z
Answers.LastEditDate: 2023-10-21T14:57:34.473Z
Answers.UserDisplayName: Jesse
Body: <p>I am facing the following issue:</p>
  <p>I have a rule in Snakemake that looks something like this:</p>
  <pre><code>rule somerule:
  input:
      tables = expand(&quot;results/{tables}_table.txt&quot;, tables = [&quot;1&quot;, &quot;2&quot;]),
  output:
      edit= expand(&quot;results/{tables}_edited.txt&quot;, tables = [&quot;1&quot;, &quot;2&quot;]),
  conda:
      &quot;../envs/r.yaml&quot;
  script:
      &quot;../scripts/script.R&quot;
  </code></pre>
  <p>The script used in this rule is as follows:</p>
  <pre class="lang-r prettyprint-override"><code>library()
  
  tab &lt;- read.table(snakemake@input[[&quot;tables&quot;]], sep = &quot;\t&quot;, header = TRUE)
  edit &lt;- some_function(tab)
  write.table(edit, file = snakemake@output[[&quot;edit&quot;]], sep = &quot;\t&quot;, quote = FALSE, row.names = TRUE, col.names = TRUE)
  </code></pre>
  <p>When I execute the code, I get this error:</p>
  <pre class="lang-r prettyprint-override"><code>Error in file(file, 'rt'): invalid 'description' argument
  Called from: read.table -&gt; file
  Execution halted
  </code></pre>
  <p>However, when I input the data individually (not as wildcards), the script works fine and produces the correct output files. Where could the problem be? My suspicion is that Snakemake is passing all the input data to the R script simultaneously instead of running them individually, but I'm not sure if that's the case and even less sure how to fix it.</p>
  
Tags: <r><snakemake>
Title: How can I incorporate wildcards in Snakemake in an R script?
ThreadText: # How can I incorporate wildcards in Snakemake in an R script?
  
  (Asked by: username_0 on 2023-10-17)
  
  I am facing the following issue:
  
  I have a rule in Snakemake that looks something like this:
  
  ```
  rule somerule:
  input:
      tables = expand("results/{tables}_table.txt", tables = ["1", "2"]),
  output:
      edit= expand("results/{tables}_edited.txt", tables = ["1", "2"]),
  conda:
      "../envs/r.yaml"
  script:
      "../scripts/script.R"
  
  ```
  
  The script used in this rule is as follows:
  
  ```r
  library()
  
  tab <- read.table(snakemake@input[["tables"]], sep = "\t", header = TRUE)
  edit <- some_function(tab)
  write.table(edit, file = snakemake@output[["edit"]], sep = "\t", quote = FALSE, row.names = TRUE, col.names = TRUE)
  
  ```
  
  When I execute the code, I get this error:
  
  ```r
  Error in file(file, 'rt'): invalid 'description' argument
  Called from: read.table -> file
  Execution halted
  
  ```
  
  However, when I input the data individually (not as wildcards), the script works fine and produces the correct output files. Where could the problem be? My suspicion is that Snakemake is passing all the input data to the R script simultaneously instead of running them individually, but I'm not sure if that's the case and even less sure how to fix it.
  
  **Tags:** r, snakemake
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2023-10-18. Score: 4 | Accepted answer
  
  Your problem is that Snakemake is passing all the input data to the R script simultaneously instead of running them individually :)
  
  As written your rule takes a list of input files and gives a list of output files (`expand` is just a formatting helper function that returns a list based on its arguments). Instead maybe have one rule that calls the R script with one input and one output, and another rule that asks for all those outputs as an easy way run the whole thing at once.
  
  Maybe something like this? I changed `tables` to `table` here just to emphasize it's one by one.
  
  ```
  rule all:
      input: expand("results/{table}_edited.txt", table = ["1", "2"])
  
  rule somerule:
      input:
          table = "results/{table}_table.txt"
      output:
          edit = "results/{table}_edited.txt"
      conda: "../envs/r.yaml"
      script: "../scripts/script.R"
  
  ```
  
  > **Comments:**
  > **username_0**: thank you so much. That worked!  
  
  ---
  
  