Marwan Naciri
Marwan Naciri

Reputation: 31

How to update/overwrite a csv file currently imported in R

I'm trying to overwrite a csv file using write_csv. This works fine until I try to overwrite a file that I imported in R beforehand. For instance, running

dummy_df <- read_csv("dummy_data.csv")

dummy_df_2 <- dummy_df %>%
  mutate(ones = 1)

write_csv(dummy_df_2, "dummy_data.csv")

will return this error:

Error: Cannot open file for writing:
* 'D:\dummy_name_of_project_directory\dummy_data.csv'

(note that you won't encounter this error if you created the dummy_data.csv file in the same R session. I had to close Rstudio and open it again to encounter the error). The csv is not opened.

Removing dummy_df from the environment (using rm()) before trying to save dummy_df_2 doesn't solve the problem.

I don't think I had this problem before, so I reckon something changed during the last update of R. I'm running on R version 4.1.1, and Rstudio version 1.4.1717.

Do you have any idea how to circumvent this error?

Thanks !

Upvotes: 1

Views: 1403

Answers (1)

guyabel
guyabel

Reputation: 8366

Try and update readr (towards 2.1). I ran into the same problem and I think the error message has stopped now - I couldn't get a concrete reproducible example working to be 100% sure.

I think the problem is related to lazy reading in readr 2.0: https://www.tidyverse.org/blog/2021/11/readr-2-1-0-lazy/

Upvotes: 1

Related Questions