Importing a csv into R using read.csv in Ubuntu - r

I have just installed Ubuntu on my computer and I am re-running some codes that previously worked in Windows. I have copied my directories into Ubuntu with all my files.
When I run this line of code to import a database into R, I get the following error:
Annot <- read.csv("~/Documents/DATABASES/Functional_Annotations/Salar_2_Annot_light.csv", header = TRUE)
Error in file(file, "rt") : cannot open the connection
In addition: Warning message: In file(file, "rt") : cannot open file
'/home/cd46/Documents/DATABASES/Functional_Annotations/Salar_2_Annot_light.csv':
No such file or directory
The code is right, hasn't changed since before. In fact if I run:
setwd("~/Documents/DATABASES/Functional_Annotations")
It works fine and recognize the directory. And the file it there too.
I am not sure what this can be, does anyone have a suggestion? The only thing I have done was to switch over to Ubuntu, so I would imagine the problem would lie there.

I have installed the readr package in R
then simply write this :
df <- read_csv("/your path/file.csv")
And this work for me and solved the problem.

Related

cannot open file 'test.csv': No such file or directory Error in file(file, "rt") : cannot open the connection

I'm having issues reading the data on the desktop version of Rstudio on mac. When I do the usual read.csv it shows the error shown in the title. This is the first time this has happened. I have tried to change my working directory in 'Sessions' at the top and also to read the file with its granularity
You could try rstudioapi library to set an unique wd for your script path and then use read.csv() where path= "test.csv"
library(rstudioapi)
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
Test <- read.csv("test.csv")

Do you need Microsoft Office (Excel) to read .csv files with R?

I just downloaded R onto my computer, but when I try to read a .csv file I get the following error:
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
cannot open file '[filename].csv': No such file or directory
This happens both when I command the program to read the .csv or when I try to import it manually (although that error code is slightly different).
Could this be because I don't have Microsoft Office (and subsequently, Excel) on my computer? The .csv file exists and is not corrupt because I have no problem uploading the file to Google sheets and displaying the data there. Either that, or I didn't install R correctly. Any suggestions?
No, you don't need any special packages to read plain text CSV files in R.
Looking at your error message:
Warning message: In file(file, "rt") : cannot open file '[filename].csv': No such file or directory
I would guess that the problem is your path to the file in question. As a quick fix, you can try using the fully qualified path to your file. For example, on Windows you might try this:
data <- read.csv(file="c:/path/to/filename.csv", header=TRUE, sep=",")
If this works, then the issue was the location of your CSV file. Then, if you want to continue using a relative path, you'll have to figure out what that would be given your current R setup.

Error while reading csv file

I have a xlsx file and to read from Rstudio i saved it as .csv file. Now when i try to read the file from Rstudio, receiving the below error.
setwd("D:/DATA SCIENCE/CCPP-Linear regression")
ccpp<- read.csv("Folds5x2_pp.csv", header = TRUE)
Error in file(file, "rt") : cannot open the connection In addition:
Warning message: In file(file, "rt") : cannot open file
'Folds5x2_pp.csv': No such file or directory
As already mentioned in the comments, the "cannot open the connection" part of the error message confirms that where R is looking is not where the file is.
A few things to keep in mind
use getwd() to see the current R working directory
use setwd() to change R's wd
You should use RStudio projects to organize your projects - this helps with the working directory thing
spaces in paths are difficult sometimes. Looks like you have "D:/DATA SCIENCE", that space can cause problems (you may need to escape the space like "DATA\ SCIENCE".
You can read the file using the full path (e.g. read.csv("D:/DATA SCIENCE/path/file.csv")) or if you are doing this a lot, set a variable to your path data_path ="D:/DATA SCIENCE/path/" and then read.csv(file.path(data_path, "file.csv")) where file.path concatenates with your OS specific path delimiter.
You can import files into RStudio using the Import Dataset option under Tools in the menu bar.

Importing a CSV file into R

I am Using a MACbook and new to R. I have the social network file on my desktop and I'm trying to get R to read it. I saved it as CSV
command typed:
sn.csv <- read.csv("C:\Users\Opemipo Akinosun\Desktop\social_network.csv", header = T)
I keep getting the following errors:
error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
cannot open file 'C:/Users/Opemipo Akinosun/Desktop/social_network.csv': No such file or directory
the resource I'm learning with doesn't ask me to set my directory so I'm a little reluctant to doing that as suggested
You should use file.path that is OS-independant :
ff <- file.path("C:","Users","Opemipo Akinosun","Desktop","social_network.csv")
Then you can check if the file exists:
> file.exists(ff)
[1] FALSE
I worked and I could resolve the issue I was facing. Try changing the directory and then use the above code. So it will be as below
setwd("C:/Users/user/Desktop")
sn <- read.csv("social_network.csv" , header = TRUE)
sn
Run the above command and you should be all set to work further.

list.files error (easy?) [R]

Im trying to r.bind all files in certain directory with the following code in R (all files are formatted the same way):
gene_list <- list.files("/nethome/genelist/")
gene_CH <- do.call("rbind",lapply(gene_list, FUN=function(files)
{read.table(files, header=TRUE, sep="\t", stringsAsFactors=FALSE)}))
write.table(gene_CH,"/nethome/genelist/all.genes.CH_v2t.txt",sep="\t",quote=F,row.names=F)
However, I get the following error:
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
cannot open file 'A2ML1_v2t.txt': No such file or directory
Thing is, A2ML1_v2t.txt is in the directory. Also, what is more puzzling (to me anyway) is that this code worked this morning. But for some reason, its not working now.
Any suggestions?
Two possibilities:
This morning, your working directory was /nethome/genelist/, but now, it isn't. list.files() only gives filenames, not absolute paths, so your second line will search through the current working directory. Try getwd() to check and setwd() to change it.
You do have read permission in the directory, do you?

Resources