R error in fwrite: name of file to write is an invalid argument? - r

I've got a long code in R where I want to write a csv file multiple times. I use fwrite for this. The code has worked in the past, but now gives an error:
fwrite(IDs, "IDS.csv")
Error in fwrite(IDs, "IDS.csv") : Invalid argument: 'IDS.csv'
IDs.csv is supposed to be the name of the csv file written, I don't see how this can be an invalid argument? Adding file= doesn't solve the problem.
I can use write.csv instead but the fwrite function is used a lot in this code and I don't want to have to change all of them.

Related

Open a file with special characters in the name with R function read.dbf

I'm trying to use the read.dbf function in R, but the name of the file contains a special character 'ñ' from spanish.
I changed the name of the file, removing the special character. The thing is that there are a lot of files with similar names and maybe this won't be the best to do.
read.dbf('Soporte_Plantilla_Vigor_Total_Daños_201806_Inicial_Vigor.dbf')
I'm getting the following error:
Error in read.dbf("Soporte_Plantilla_Vigor_Total_Daños_201806_Inicial_Vigor.dbf") :
unable to open DBF file
I want to be able to read this type of file names without changing them.

write r file to csv -- "object not found"

Hello I need to make my R file into a CSV file. I have searched stackoverflow for different methods to accomplish this, but nothing is working. Each time, it says:
Error in is.data.frame(x) : object 'refine_clean' not found
My file is definitely named refine_clean.R. I have linked a screenshot here:
R Studio CSV Error Message
Your file is named refine_clean but you have no data.frame or object named refine_clean, which is what write_table and write.csv are looking for.
based on the screen shot, it is not clear what you want to produce. If it is the last object you created (d5), you may want something like this
write.csv(d5, "refine_clean.csv")
you cant convert all your file as an csv, you need to select the object, like d5
write.csv(d5,file = "name_of_the_file")

Writing dataframe to dbf file in R

I'm looking to export a dataframe as a dbf file in R, and am using write.dbf but I get an error that says:
Error in write.dbf(mydata, "mydata.dbf") :
could not find function "write.dbf"
I've tried several variations of the filename portion for write.dbf (i.e no quotations, no extension, with the path included) but the all say the function can't be found.
Why is this the case?
Try this:
install.packages("foreign")
library(foreign)
write.dbf(mydata, "mydata.dbf")
As commented, you should first load the package containing this function. if you don't remember which package the function is from, you might retrieve it using ??write.dbf

Sys.glob Directory Error

I'm attempting to load in multiple tripdata csv files from a directory that I defined in R as my current directory. I keep getting this dirmark error, and I'm sure that it's a syntax error in the way I'm defining my path.
data_dir <- '.'
Error in Sys.glob("%s/*-tripdata.csv", data_dir) :
invalid 'dirmark' argument
It appears that you are trying to use sprintf formats (i.e. %s) without actually calling sprintf
Something like
Sys.glob('./*-tripdata.csv')
should work
or
Sys.glob(sprintf('%s/*-tripdata.csv',data_dir))

write.xlsx function gives error when defining path with the file name but read.xlsx is fine

I have a problem with the write.xlsx2 function of xlsx R package. For instance, see the code below.
main_path<-"~/mydir/"
read.xlsx2(paste0(main_path,"my_input_excel.xlsx"), sheetIndex=1)
a<-1
write.xlsx2(a, paste0(main_path,"my_output_excel.xlsx"), sheetName="Sheet1", col.names=TRUE, row.names=FALSE, append=FALSE)
While read.xlsx2 function works fine, write.xlsx2 function gives the error
Error in .jnew("java/io/FileOutputStream", jFile) :
java.io.FileNotFoundException: /mydir/my_output_excel.xlsx (No such
file or directory)
When I remove the paste0 part and write only the file name everything is fine again. So the problem is to define path.
p.s I am wondering, perhaps the write.xlsx ignores the tilde ~ so path definition becomes garbage.
Replacing "~/" with "Users//" works for Mac (probably for Linux as well). Though, it still eludes me how read.xlsx and write.xlsx could differ in such a fundamental way.

Resources