[R] How to loop over two files ...

Rasmus Liland jr@| @end|ng |rom po@teo@no
Fri Jun 19 23:31:33 CEST 2020


On 2020-06-19 16:07 -0500, Ana Marija wrote:
> HI Rasmus,
> 
> I tried it:
> 
> library(base)
> 
> > r <- readRDS(paste0(population.name, ".rds"))
> Error in gzfile(file, "rb") : cannot open the connection
> In addition: Warning message:
> In gzfile(file, "rb") :
>   cannot open compressed file '1000GENOMES:phase_3:KHV.rds', probable
> reason 'No such file or directory'

Because I run my script again and again after 
every little small change using the program 
entr[1] as opposed to using Emacs Speaks 
Statistics or RStudio, I find it useful to 
save partial outputs in rds files, but it 
also make sense to not call ensembl.org again 
and again ...

Right, so you would run the commented bit 
before that first, then save the output list 
to the rds to not send too many requests to 
the list.  I have attached my rds here.  

	files <- c("1g.txt", "1n.txt")
	files <- lapply(files, readLines)
	server <- "http://rest.ensembl.org"
	population.name <- "1000GENOMES:phase_3:KHV"
	ext <- apply(expand.grid(files), 1, function(x) {
	  return(paste0(server, "/ld/human/pairwise/",
	    x[1], "/", x[2],
	    "?population_name=", population.name))
	})
	
	r <- lapply(ext, function(x) {
	  httr::GET(x, httr::content_type("application/json"))
	})
	names(r) <- ext
	file <- paste0(population.name, ".rds")
	
	saveRDS(object=r, compress="xz", file=file)  # <--- Then save the list here for another time!
	# r <- readRDS(paste0(population.name, ".rds"))  # Read it back like this
	
	r <-
	sapply(r, function(x) {
	  x <- jsonlite::fromJSON(jsonlite::toJSON(httr::content(x)))
	  length(x)
	})
	names(r) <- NULL
	r

[1] http://eradman.com/entrproject/

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20200619/e6b6ddea/attachment.sig>


More information about the R-help mailing list