N.Ugartondo
N.Ugartondo

Reputation: 61

Error R csv: Error in read.table(file = file, header = header, sep = sep, quote = quote, : 'file' must be a character string or connection

I'm struggling with an R tutorial for the package MiRLAB although I'm not sure whether the problem is related to the package's function.

I want to use the function Pearson(), which only accepts .csv files. I've loaded a file perfectly but when I've tried the function Pearson() (the same happens with MI, IDA and Lasso), this error message appeared:

Error in read.table(file = file, header = header, sep = sep, quote = quote, : 'file' must be a character string or connection

dataset=read.csv("ArabPrueba1.csv", sep = ";")
cause= 3:23
effect= 24:44
pearson=Pearson(dataset,cause,effect)

Any idea on how can I change the file or the command line so it will be recognized by the function? Might it be a problem with the csv file or something else? In the example of the tutorial the file is directly taken from the package so this approach is not covered.

Thank you in advance

Upvotes: 3

Views: 48221

Answers (2)

N.Ugartondo
N.Ugartondo

Reputation: 61

I've finally solved the problem, even if it doesn't seem a clean solution, it works. I guess the problem was that some extra "" were added to the original files when I took them to the console so I rewrite the files and re-added the recently created files to the console.

RNA <- read.table("RNA.csv", dec = ",", sep = ";", stringsAsFactors = FALSE, header = TRUE, blank.lines.skip = TRUE)
tRNA <- t(RNA)
write.table(tRNA, file = "tRNA_2.csv", quote = FALSE, dec = ".", sep = ",", qmethod = "escape")
tRNAt <- read.csv("tRNA_2.csv")

miRNA <- read.table("miRNA.csv", dec = ",", sep = ";", stringsAsFactors = FALSE, header = TRUE, blank.lines.skip = TRUE)
tmiRNA <- t(miRNA)
write.table(tmiRNA, file = "tmiRNAt.csv", quote = FALSE, dec = ".", sep = ",", qmethod = "escape")
tmiRNAt <- read.csv("tmiRNAt.csv")

dataset <- cbind2(x=tmiRNAt, y = tRNAt)
write.table(dataset, file = "dataset_2.csv", quote = FALSE, dec = ".", sep = ",", qmethod = "escape")

# MiRLAB:
library(miRLAB)
cause = 1:278
effect = 279:length(dataset)
ps = Pearson("dataset_2.csv", cause = cause, effect = effect)

I know it's not a good idea to write and read so many times but it's actually the way that it have worked better (worked at all).

Thank you for your help!

Upvotes: 3

cccmir
cccmir

Reputation: 1003

here is an example:

# working:
# create csv file with "," seprator
write.table(x = mtcars, file = "data.csv", sep = ",", row.names = F)
Pearson(datacsv = "data.csv", cause = 2:3, effect = 3:4)

#           cyl      disp
#disp 0.9020329 1.0000000
#hp   0.8324475 0.7909486


# not work:
# create csv file with ";" seprator
write.table(x = mtcars, file = "data1.csv", sep = " ; ", row.names = F, col.names = F)
Pearson(datacsv = "data1.csv", cause = 2:3, effect = 3:4)

the inner read.csv uses default seprator "," just change your file and replace all semicolon with comma

Upvotes: 1

Related Questions