Using R to Download and extract zip file that contains a folder - r

In Rstudio I wrote:
urlcoP <- "http://www.cophieu68.vn/datax123456/metastock_all_data.zip"
temp <- tempfile()
download.file(urlcoP,temp)
data <- read.table(unz(temp, "metastock_all_data.txt"))
But it's get the error:
Error in open.connection(file, "rt") : cannot open the connection
In addition: Warning message:
In open.connection(file, "rt") :
cannot locate file 'metastock_all_data.txt' in zip file 'C:\TEMP\RtmpaaH3PW\file135c1e97348'
The reason is that the metastock_all_data.txt is in a folder called datax123456 in the zip.

try this?
data <- read.table(unz(temp, "datax123456/metastock_all_data.txt"))
metastock_all_data.txt is in a folder called datax123456 in the zip, hence add the folder before metastock_all_data.txt

Related

Library of alternativeFunction() in R

I am trying to read 2 different types of files, using the alternativeFunction(). So if the first file in .txt format could not be found, I could try with the second type in .csv format, but R tells me that the function is not found. Does anyone know the library I have to install?
File <- try(read.table('File.txt',sep=c(',','|'), header=TRUE))
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
cannot open file 'File.txt': No such file or directory
if("try-error" %in% class(File)) alternativeFunction(File <- read_csv('File.csv'))
Error in alternativeFunction(File <- read_csv("File.csv")) :
could not find function "alternativeFunction"

Reading zip file off the internet: Error in open.connection(file, "rt") : cannot open the connection

I have a url I'm trying to directly read into R using the following code:
url <- "https://oehha.ca.gov/media/downloads/calenviroscreen/document/ces3results.xlsx"
temp <- tempfile()
download.file(url, temp)
data <- read.table(unz(temp, 'ces3results.xlsx'))
unlink(temp)
and I get the following error:
Error in open.connection(file, "rt") : cannot open the connection
In addition: Warning message:
In open.connection(file, "rt") :
cannot open zip file '/var/folders/94/1ldxgg4d3tzg4sqznhj8m2qr0000gn/T//RtmpcHqBEX/fileb6f920054f3a'
From what I've researched, this may be a matter of the website not allowing people to download data directly from their url. Is it that or is my code just faulty?
Your URL does not seem to point to a .zip archive, and you can actually open it directly using openxlsx::read.xlsx.
url <- "https://oehha.ca.gov/media/downloads/calenviroscreen/document/ces3results.xlsx"
data1 <- openxlsx::read.xlsx(url)
To first download, then open it you may adapt your method a little. Use mode="wz", leave out the unz, finally method as above.
temp <- tempfile()
download.file(url, destfile = temp, mode="wb")
data2 <- openxlsx::read.xlsx(temp)
unlink(temp) ## cleanup
stopifnot(all.equal(data1, data2))

read.delim no such file or directory in R

I got a list of files from list.files() in current directory such that:
setwd("~/Data/LUSC/unc.edu_LUSC.IlluminaHiSeq_RNASeq.Level_3.1.2.0")
exprs_files <- list.files()
exprs_files <- exprs_files[grepl('gene.quantification', exprs_files)]
exprs_files[88]
[1] "UNCID_586826.TCGA-60-2720-01A-01R-0851-07.110426_UNC12-SN629_0078_BB08WCABXX.6.trimmed.annotated.gene.quantification.txt"
But I got error when read it:
read.delim(exprs_files[88])
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
cannot open file 'UNCID_586826.TCGA-60-2720-01A-01R-0851-07.110426_UNC12-SN629_0078_BB08WCABXX.6.trimmed.annotated.gene.quantification.txt': No such file or directory
I am using RStudio Version 1.3.959 on Windows 10.
Any suggestions?
Try using a double bracket:
read.delim(exprs_files[[88]])

Can't read CSV file with R script

I'm still very new to R and I'm trying to read a moderate-size .csv file with a Script, but the script is unable to detect the file.
My code is at follows:
Saint<- read.csv("./Comp1.csv", stringsAsFactors = FALSE)
Saint
I have attempted several ways to indicate the file path. Including
path<- file.path("desktop", "saint", "Comp1.csv"
Then using the path variable in the read.csv function.
Also explicitly indicating the full path with forward slashes
It always throws the following error
> Saint<- read.csv("./Comp1.csv", stringsAsFactors = FALSE)
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
cannot open file './Comp1.csv': No such file or directory
>
> Saint
Error: object 'Saint' not found
Any ideas on what might be wrong here?

in file(file, "rt") : cannot open the connection In addition: Warning message: In file(file, "rt") : cannot open file: No such file or directory

I am trying to upload a file of csvs and other documents into R studio. My code looks like the following.
setwd("~/Dropbox/R")
getwd()
[1] "/Users/me/Dropbox/R"
myData <-read.csv("~/Dropbox/Exercise_Files")
The message returned is:
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
cannot open file '/Users/me/Dropbox/R/Exercise_Files': No such file or
directory
I can't understand why R is telling me no such directory when getwd() shows the directory I am using. Nor do I understand why I am being told that there are no such files--I put them into the directory! They are right there! Please help; I have tried everything.
You have to specify the .csv extension:
myData <-read.csv("~/Dropbox/Exercise_Files.csv")
list.files() worked for me. I discovered that all my text files were being seen in R as filename.txt.txt but were seen as filename.txt in the Windows file manager. I changed the files to filename (without '.txt') in Windows and now they read into R correctly.

Resources