1

The 'mow.R' now contains:

library(RgoogleMaps);
png(filename="RgoogleMaps-package_%03d_med.png", width=480, height=480);

MyMap <- GetMap(markers='&40.702147,-74.015794,blues%7C40.711614,-74.012318,greeng%7C40.718217,-73.998284,redc', destfile="MyTile1.png");

This results in the following new error:

> source('mow.R')
[1] "Note that when center and zoom are not specified, no meta information on the map tile can be stored. This basically means that R cannot compute proper coordinates. You can still download the map tile and view it in R but overlays are not possible. Do you want to proceed ? (y/n)"
y
[1] "&40.702147,-74.015794,blues%7C40.711614,-74.012318,greeng%7C40.718217,-73.998284,redc"
[1] "http://maps.google.com/maps/api/staticmap?size=640x640&maptype=terrain&format=png32&sensor=true&40.702147,-74.015794,blues%7C40.711614,-74.012318,greeng%7C40.718217,-73.998284,redc"

Error in readChar(con, 5L, useBytes = TRUE) : cannot open the connection
In addition: Warning message:
In readChar(con, 5L, useBytes = TRUE) :
  cannot open compressed file 'MyTile1.png.rda', probable reason 'No such file or directory'
> 

What's the way to correct this error?

R version 2.15.1
RgoogleMaps version 1.2.0


> sessionInfo() 
R version 2.15.1 (2012-06-22)
Platform: x86_64-unknown-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=C                 LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] RgoogleMaps_1.2.0 png_0.1-4        
> 
Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411
  • I will be cross posting this on the R's mailing list too. – Aquarius_Girl Jul 17 '12 at 04:45
  • 3
    I give up, suggest reading http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example and http://www.catb.org/~esr/faqs/smart-questions.html – mdsumner Jul 17 '12 at 04:50
  • @mdsumner sorry, I still didn't get what else do I have to add here besides versions which I'll be adding soon. If you run the file presented above, you'll notice the errors I am taking about. – Aquarius_Girl Jul 17 '12 at 04:57
  • copied this example from here: http://rgm2.lab.nig.ac.jp/RGM2/func.php?rd_id=RgoogleMaps:RgoogleMaps-package – Aquarius_Girl Jul 17 '12 at 05:01
  • @mdsumner also they have used here a png file name: `RgoogleMaps-package_%03d_med.png` I don't know what does that represent. – Aquarius_Girl Jul 17 '12 at 05:03
  • @mdsumner added more info? Is that fine now? – Aquarius_Girl Jul 17 '12 at 05:26
  • I ran the code in your example. It produces the first notes, but not he second error. The code as posted doesn't refer to `MyTile1.pnd.rda`, so you haven't given all the information. – Andrie Jul 17 '12 at 05:27
  • PS. you don't have a `dev.off()` in your code. – Andrie Jul 17 '12 at 05:28
  • I basically cannot run this at all, it crashes my R there's a lot of room for improvement - I'll take off the grumpy hat and have a closer look later :) – mdsumner Jul 17 '12 at 05:30
  • @Andrie thanks for responding. You said: `The code as posted doesn't refer to MyTile1.pnd.rda` pardon me, I don't know what to make out from that. should I provide some screenshots here? what exact more info should be presented here? P.S. Also, I don't have any extra png file anywhere stored. Also I added dev.off() but that didn't solve the problem. – Aquarius_Girl Jul 17 '12 at 05:34
  • @mdsumner well, thanks, please see if you can help me here. Something else should I provide? – Aquarius_Girl Jul 17 '12 at 05:36
  • @Andrie that link also says: ***"NOTE: To do anything but downloading static map tiles, RgoogleMaps needs EITHER rgdal OR ReadImages installed!"*** I don't have these installed, can this be the reason of the error? – Aquarius_Girl Jul 17 '12 at 05:41
  • As I've said, I ran your code as posted, and I don't get this error. All I've done in my previous comment was to read back the error message to you. The error message refers to a file that is not stated in your code. Therefore I assume that you have other code in this same script that generates the error. If you want us to help you, make sure the error is reproducible, – Andrie Jul 17 '12 at 05:43
  • I don't know, what happens if your install either `rgdal` or `ReadImages`? – Andrie Jul 17 '12 at 05:44
  • @Andrie You said: `Therefore I assume that you have other code in this same script that generates the error` No, absolutely not, I don't have any other script in any other file. now, I'll put this file in a separate folder, and then see how it behaves. – Aquarius_Girl Jul 17 '12 at 05:47

2 Answers2

0

Solved. Here's the working code:

library(RgoogleMaps);
png(filename="Rg.png", width=480, height=480);


lat    = c(40.702147,40.718217,40.711614);
lon    = c(-74.012318,-74.015794,-73.998284);
center = c(mean(lat), mean(lon));
zoom <- min(MaxZoom(range(lat), range(lon)));

MyMap <- GetMap(center=center, zoom=zoom, markers='&40.702147,-74.015794,blues%7C40.711614,-74.012318,greeng%7C40.718217,-73.998284,redc', destfile="My.png");

tmp <- PlotOnStaticMap(MyMap,lat=c(40.702147,40.711614,40.718217),lon=c(-74.015794,-74.012318,-73.998284),cex=1.5,pch=20,col=c('red', 'blue', 'green'),add=F);

dev.off();

This results in:

dd@linux-y3pi:~/RTest> ls
new.R

dd@linux-y3pi:~/RTest> R

R version 2.15.1 (2012-06-22) -- "Roasted Marshmallows"
Copyright (C) 2012 The R Foundation for Statistical Computing
ISBN 3-900051-07-0
Platform: x86_64-unknown-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

[Previously saved workspace restored]

> source('new.R')
Loading required package: png
[1] "&40.702147,-74.015794,blues%7C40.711614,-74.012318,greeng%7C40.718217,-73.998284,redc"
[1] "http://maps.google.com/maps/api/staticmap?center=40.7106593333333,-74.0087986666667&zoom=15&size=640x640&maptype=terrain&format=png32&sensor=true&40.702147,-74.015794,blues%7C40.711614,-74.012318,greeng%7C40.718217,-73.998284,redc"
> q()
Save workspace image? [y/n/c]: y

dd@linux-y3pi:~/RTest> ls
My.png  My.png.rda  new.R  Rg.png

dd@linux-y3pi:~/RTest> 
Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411
  • 6
    +1, in addition could you specify what the problem was and describe the solution? Right now we need to compare the code ourselves, your explanation would make the answer much more clear. – Paul Hiemstra Jul 17 '12 at 08:52
  • @PaulHiemstra Of course I do understand that, but the problem is that I am NOT too well versed with R to "explain" the problem myself. Really sorry. Perhaps some R expert can compare the code, and tell what "exactly" made this code work. :) – Aquarius_Girl Jul 17 '12 at 09:04
0

2 common causes

  1. The file doesn't exist
  2. There's a typo in the file path.

Both will result in the error:

Error in readChar(con, 5L, useBytes = TRUE) : cannot open the connection
In addition: Warning message:
In readChar(con, 5L, useBytes = TRUE) :
  cannot open file 

but once corrected, load() will work as expected.

Some tips that may help

  • Make sure you have no typos in the file path
  • Make sure you have the full file name (e.g. birds.rda)
  • If you provide a relative path, run getwd() to check that the relative path is relative to your current working directory. (if not, change either the relative path or your current working directory with setwd())
stevec
  • 41,291
  • 27
  • 223
  • 311