Hüsamettin Tayşi
Hüsamettin Tayşi

Reputation: 574

Import file from environment instead of read.table

I am using a package of someone else. As you see, there is a ImportHistData term in the function. I want to import the file from environment as rainfall name instead of rainfall.txt. When I replace rainfall.txt with rainfall, I got this error:

Error in read.table(x, header = FALSE, fill = TRUE, na.strings = y) : 'file' must be a character string or connection

So, to import file not as a text, which way should I follow?

Original shape of the function

DisagSimul(TimeScale=1/4,BLpar=list(lambda=l,phi=f,kappa=k,
                                          alpha=a,v=v,mx=mx,sx=NA),CellIntensityProp=list(Weibull=FALSE,
                                                                                          iota=NA),RepetOpt=list(DistAllowed=0.1,FacLevel1Rep=20,MinLevel1Rep=50,
                                                                                                                 TotalRepAllowed=5000),NumOfSequences=10,Statistics=list(print=TRUE,plot=FALSE),
                 ExportSynthData=list(exp=TRUE,FileContent=c("AllDays"),file="15min.txt"),
                 ImportHistData=list("rainfall.txt",na.values="NA",FileContent=c("AllDays"),
                                     DaysPerSeason=length(rainfall$Day)),PlotHyetographs=FALSE,RandSeed=5)

Source of ImportHistData part in the function

ImportHistDataFun(mode = 1, x = ImportHistData$file, 
                     y = ImportHistData$na.values, z = ImportHistData$FileContent[1], 
                     w = TRUE, s = ImportHistData$DaysPerSeason, timescale = 1)

Upvotes: 0

Views: 141

Answers (1)

Parfait
Parfait

Reputation: 107577

First, check documentation of the package and see if the method (?DisagSimul) allows a data frame in memory to be used for ImportHistData argument instead of reading from an external .txt file.

If the function is set up to only read a file from disk and you do not want to save your rainfall data frame permanently as a file, consider using a tempfile that exists only in the R session or until you use unlink():

# INITIALIZE TEMP FILE
tf <- tempfile(pattern = "", fileext = ".txt")    

# EXPORT rainfall to FILE
write.table(rainfall, tf, row.names=FALSE)    
...

# USE TEMPFILE IN METHOD   
DisagSimul(...
           ImportHistData = list(tf, na.values="NA", FileContent=c("AllDays"),

Upvotes: 0

Related Questions