Saving Files Produced in R Environment to Computer

I am working on RStudio and trying to analyze 16S sequencing data using the workflow found on this website: 16S rDNA amplicon sequencing analysis using R. What I am expecting to happen is that R will transform my raw DNA sequencing files and trim them using the cutadapt environment in qiime2 (all this is outlined in the link above). The files should then be saved to my computer in a folder I named "trimmed".


[1] "/home/cory/Documents/Rtrial"

When I try the workflow above, characters are appearing in my R environment as expected, and their values are the files I am expecting. For example, when I run the code below I am getting values in the fnFs.cut row in my environment that show a file path. However, these values are not actually saving to my actual "trimmed" folder.

fnFs = fns[grep("L001_R1_001.fastq.gz", fns)]
fnFs.cut = file.path(trimmed, basename(fnFs))
fnFs.cut chr [1:37] trimmed/1_S8_L001_R1_001.fastq.gz ...

But when I try to find the file outputs of fnFs.cut, I get this error:


[1] "trimmed/1_S8_L001_R1_001.fastq.gz" ... There were 37 warnings (use warnings() to see them)


Warning messages: 1: In normalizePath(fnFs.cut) : path[1]="trimmed/1_S8_L001_R1_001.fastq.gz": No such file or directory

My question is how would I save the "trimmed/1_S8_L001_R1_001.fastq.gz" file to my computer from my R environment?

Could you please directly show us the code that you used to save these data?

I read the workflow and find no evident problem with. I want more specific about your code (eg.,Rscript, Rhistory).

Chose a destination folder in your current working directory as shown by getwd(). You can move that folder where you wish in the operating system. It's simpler and less error prone.

The second part is choosing a function to write the objects in the environment to that folder. That depends on what kind of objects they are. Here's an example with csv files.

# Create a folder named "trimmed" if it doesn't exist
folder_name <- "trimmed"
if (!dir.exists(folder_name)) {

# Get the names of all data frames in the environment
data_frames <- Filter(function(x), ls())

# Save each data frame as a CSV file
for (df_name in data_frames) {
  df <- get(df_name)  # Get the data frame
  # Construct the file path
  file_name <- paste0(folder_name, "/", df_name, ".csv")
  # Save the data frame as a CSV file
  write.csv(df, file = file_name, row.names = FALSE)

#> [1] "bar.csv" "baz.csv" "foo.csv"

Created on 2023-05-26 with reprex v2.0.2

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.