Issues with write_csv/csv2/etc.

Goal

I know this is not the most efficient way to tidy a data set but I have tab delimitated files I am reading in, removing some rows, transposing, and then copying the first row and inserting it every other line. I can't seem to write the file into my directory though.

Reprex

pacman::p_load(
  here,
  dplyr,
  readr
)

path <- here()

# CI <- read_delim("CI.pfl", "\t", escape_double = FALSE, 
#                  col_names = FALSE, trim_ws = TRUE, skip = 3)

data <- data.frame(x1 = 1:5,           
                   x2 = 2:6,
                   x3 = 3:7)

row.names(data) <- LETTERS[1:5]
data  

data2 <- t(data)

data3 = matrix(nrow = ((nrow(data2)-1)*2), ncol = ncol(data2))

j = 2

for (i in 1:((nrow(data2)-1)*2)) {
  
  if((i %% 2) == 0) {
    data3[i,] = data2[j,]
    j = j +1
  } else (data3[i,] = data2[1,])
}

Seismic_Unix_Input <- as.data.frame(data3)

write_csv2(x = Seismic_Unix_Input,file = here() ,na = FALSE, append = TRUE, col_names = FALSE)

When I go to try to write the file, I keep getting this:

Error

Error in open.connection(file, "ab") : cannot open the connection
In addition: Warning messages:
1: In file(path, "") :
  'raw = FALSE' but '/Users/wouldntYouLikeToKnow/wouldntYouLikeToKnow/wouldntYouLikeToKnow/Basetest2/500by500modelbasetest2' is not a regular file
2: In open.connection(file, "ab") :
  cannot open file '/Users/wouldntYouLikeToKnow/wouldntYouLikeToKnow/wouldntYouLikeToKnow/Basetest2/500by500modelbasetest2': Is a directory

Am I using the write_x functions incorrectly here? Is it an issue with how my data is set up? Thank you in advance for all the help.

You are providing a path to a folder instead of a file name for the file parameter, see this example:

library(readr)

# This doesn't work
write_csv(iris, file = "~/")
#> Warning in file(path, ""): 'raw = FALSE' but '~/' is not a regular file
#> Warning in open.connection(file, "wb"): no fue posible abrir el archivo '/home/
#> andres/': Es un directorio
#> Error in open.connection(file, "wb"): no se puede abrir la conexión

# This works
write_csv(iris, file = "~/something.csv")

Created on 2021-02-03 by the reprex package (v1.0.0)

Wonderful, this solved my issue but also showed me I don't fully understand here(). I was under the impression I could add downstream arguments within here()

here("/something.csv") 

and thought it would do something like

path = here() 
sprintf("%s/somethins.csv", path)

Any ideas on this one?

Do not use the slash

Some examples from the documentation

library(here)
#> here() starts at /home/muelleki/git/R/here
here()
#> [1] "/home/muelleki/git/R/here"
here("DESCRIPTION")
#> [1] "/home/muelleki/git/R/here/DESCRIPTION"
here("R", "here.R")
#> [1] "/home/muelleki/git/R/here/R/here.R"

Ah, shux. Shame I didn't catch that. Thank you so much!

This topic was automatically closed 7 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.