1

I'm very sorry about this post, I'm sure its way too simple but I've been looking and looking and can't find any answers! (yes my wifi is switched on =P haha) I'm building a pipeline for DNA analysis using BASH and it would be really useful to use this script in it. (I take 0% of the credit for its making but it has been posted for all to use) https://forum.qiime2.org/t/automatic-manifest-maker-in-r/2921 I have never done any work in R before so again I'm sorry if this is a dumb question.

I suspect the issue is with the first few lines or perhaps the way its being interpreted, here are the first 3 lines

library(tidyverse)

SamplesF <- list.files(path = "Data", pattern = "*.R1.fastq.gz", all.files = 
FALSE,
full.names = TRUE, recursive = FALSE,
ignore.case = FALSE, include.dirs = FALSE, no.. = FALSE)

here are the errors

/home/qiime2/Taxonomy.R: line 1: syntax error near unexpected token `tidyverse'
/home/qiime2/Taxonomy.R: line 1: `library(tidyverse)'

Just to add some context, this is in a Ubuntu VM, the following commands were run to install and set up the packages

conda create -n R-Env -y
source activate R-Env
Install r-essentials and tidyverse package by running:
conda install r-essentials -y
conda install -c r r-tidyverse -y
conda install -c r r-gdata -y

I called the script Taxonomy.R and gave it chmod +x it was created and is currently in the ~ directory

Gus Bishop
  • 23
  • 6
  • Did you install the `tidyverse` package in R? – LAP Sep 25 '18 at 08:53
  • yes i have using the following; conda install r-essentials -y conda install -c r r-tidyverse -y conda install -c r r-gdata -y – Gus Bishop Sep 25 '18 at 09:04
  • It may be helpful if you added information *in the post* about how you installed the packages **and** called the R script. I assume you followed steps from the link you give above, meaning you're doing all this through Anaconda, which many R users may not assume. I haven't used it, but in that link it seems you're making the script executable and calling it from the terminal like `./Taxonomy.R`. If so, have you tried adding `#!/usr/bin/env Rscript` to the top of your script? That's what I'd normally if making an R script executable, but then again, it may be different in the Anaconda context. – duckmayr Sep 25 '18 at 09:47
  • @duckmayr Thanks!!!! So i have now sought out the location of Rscript and found it in /home/qiime2/miniconda/envs/R-Env/lib/R/bin/Rscript I therefore changed the first line to # /home/qiime2/miniconda/envs/R-Env/lib/R/bin/ which gave me the following error bash: /home/qiime2/Taxonomy.R: /home/qiime2/miniconda/envs/R- Env/lib/R/bin/: bad interpreter: Permission denied – Gus Bishop Sep 25 '18 at 10:17
  • Attempting to use #!/home/qiime2/miniconda/envs/R-Env/bin Rscript did not work either bash: /home/qiime2/Taxonomy.R: /home/qiime2/miniconda/envs/R-Env/bin/: bad interpreter: Permission denied – Gus Bishop Sep 25 '18 at 10:24
  • Following the advice of [this question](https://stackoverflow.com/questions/41131901/shebangs-in-conda-managed-environments) in a related but different context, I'd say put at the top of the script `#!/usr/bin/env Rscript`, then in a terminal, before you run `./Taxonomy.R`, run `source activate R-Env`. If that works, I'll roll all of this into an answer, so that anyone else coming to this question with this problem from a google search or something won't have to sift through the comments. If not, I can try to investigate further. – duckmayr Sep 25 '18 at 10:37
  • `(R-Env) qiime2@qiime2core2018-8:~/Desktop/MACE_DEMUX_FASTQs/barcode08$ ~/Taxonomy.R ── Attaching packages tidyverse 1.2.1 ── ✔ ggplot2 2.2.1 ✔ purrr 0.2.4 ✔ tibble 1.4.2 ✔ dplyr 0.7.5 ✔ tidyr 0.8.1 ✔ stringr 1.3.1 ✔ readr 1.1.1 ✔ forcats 0.3.0 ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ── ✖ dplyr::filter() masks stats::filter() ✖ dplyr::lag() masks stats::lag() Error in `[<-.data.frame`(`*tmp*`, "direction", value = "forward") : replacement has 1 row, data has 0 Calls: [<- -> [<-.data.frame Execution halted` – Gus Bishop Sep 25 '18 at 10:45
  • To give some context for anyone reading this in the future, I followed the advice of duckmayer and put `#!/usr/bin/env Rscript#!` at the top of the script, HOWEVER, I had to deactivate then re activate my R-Env before it gave the above output, otherwise it gave a "permission denied" error The script was also not successful in producing the "Manifest.csv" file we want it too – Gus Bishop Sep 25 '18 at 10:49
  • Alright! This is actually good progress. That means we've solved your syntax error got your R script running, but now there's a new error unrelated to your previous one. I believe the correct thing to do in that situation is I'll post the solution to the syntax error as an answer here, which you could signal as accepted as it solved that error, then for the new error you'd post a new question. – duckmayr Sep 25 '18 at 10:52
  • You're an absolute legend! Thanks so much!!! Will do!! – Gus Bishop Sep 25 '18 at 10:56
  • @GusBishop Glad this helped (somewhat). Cheers! – duckmayr Sep 25 '18 at 10:59

1 Answers1

0

When running an R script as an executable file, you need to add a "shebang" (see, for example, this discussion); so, as the very first line of your R script, add the following:

#!/usr/bin/env Rscript

Then, from the terminal, you should be able to run

source activate R-Env
./Taxonomy.R

(See a brief discussion of shebangs and Anaconda in a different context at this Stack Overflow question). Note that you may need to deactivate then reactivate your R-Env for this to work.

While that will solve the syntax error and allow your R script to run, it does not guarantee your R script will run without unrelated errors.

duckmayr
  • 16,303
  • 3
  • 35
  • 53