admin管理员组

文章数量:1122832

I want to save a .txt file using write.table in R. The file needs to utilize placeholders and wildcards.

I have successfully used a for loop and the sprintf command to loop through subjects and save files for each of them. For example:

subjectlist = c("001")

for (subs in subjectlist){
 test <- cbind(3,2,1)
 write.table(test, file = sprintf("/home/subject_%s/test-1908/test.txt", subs)
}

this saves the "test" variable as a file in path /home/subject_001/test-1908/.

Now, I'd like to update the script to write more files but, because within each subject's folder, there is a single folder that starts with "test" but ends with a unique number (e.g., subject_001/test-1908, subject_002/test-143, etc.), I want to use a wildcard, so that regardless of what the test number is, the script knows to save the file in the folder that begins with "test-".

I tried using Sys.glob with the sprintf command:

write.table(test, Sys.glob(file = (sprintf("/home/subject_%s/test-*/test.txt", subs))

but got this error:

Error in if (file == "") file <- stdout() else if (is.character(file)) { : 
  argument is of length zero

How can I do this?

I want to save a .txt file using write.table in R. The file needs to utilize placeholders and wildcards.

I have successfully used a for loop and the sprintf command to loop through subjects and save files for each of them. For example:

subjectlist = c("001")

for (subs in subjectlist){
 test <- cbind(3,2,1)
 write.table(test, file = sprintf("/home/subject_%s/test-1908/test.txt", subs)
}

this saves the "test" variable as a file in path /home/subject_001/test-1908/.

Now, I'd like to update the script to write more files but, because within each subject's folder, there is a single folder that starts with "test" but ends with a unique number (e.g., subject_001/test-1908, subject_002/test-143, etc.), I want to use a wildcard, so that regardless of what the test number is, the script knows to save the file in the folder that begins with "test-".

I tried using Sys.glob with the sprintf command:

write.table(test, Sys.glob(file = (sprintf("/home/subject_%s/test-*/test.txt", subs))

but got this error:

Error in if (file == "") file <- stdout() else if (is.character(file)) { : 
  argument is of length zero

How can I do this?

Share Improve this question edited Nov 22, 2024 at 3:28 Phil 8,0973 gold badges40 silver badges76 bronze badges asked Nov 21, 2024 at 9:44 AFMAFM 11 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

There are few typos (or some confusion) with parentheses, so perhaps try without nested function calls, but the main issue here is that you seem to expect Sys.glob() to find a file that does not yet exist.

Prepare dir tree for reprex:

root_p <- fs::path("so-79210566")
fs::dir_create(c(
  root_p / "subject_001" / "test-1908",
  root_p / "subject_002" / "test-143"
))
fs::dir_tree(root_p)
#> so-79210566
#> ├── subject_001
#> │   └── test-1908
#> └── subject_002
#>     └── test-143

Build a wildcard pattern, then find a match with Sys.glob(), then build final file path; prefer file.path() for constructing paths:

root <- "so-79210566"
subjectlist <- c("001", "002")
for (subs in subjectlist){
  test <- cbind(3,2,1)
  
  test_glob <- file.path(root, sprintf("subject_%s", subs), "test-*")
  message("test_glob: ", test_glob)
  
  # keep the 1st match
  test_dir <- Sys.glob(test_glob)[1]
  message("test_dir:  ", test_dir)
  
  write.table(test, file = file.path(test_dir, "test.txt"))
}
#> test_glob: so-79210566/subject_001/test-*
#> test_dir:  so-79210566/subject_001/test-1908
#> test_glob: so-79210566/subject_002/test-*
#> test_dir:  so-79210566/subject_002/test-143

Resulting dir tree:

fs::dir_tree(root_p)
#> so-79210566
#> ├── subject_001
#> │   └── test-1908
#> │       └── test.txt
#> └── subject_002
#>     └── test-143
#>         └── test.txt

Created on 2024-11-22 with reprex v2.1.1

本文标签: printfusing writetable function in R with sprintf with a wildcardStack Overflow