how to download and display an image from an URL in R?
If I try your code it looks like the image is downloaded. However, when opened with windows image viewer it also says it is corrupt.
The reason for this is that you don't have specified the mode
in the download.file
statement.
Try this:
download.file(y,'y.jpg', mode = 'wb')
For more info about the mode is see ?download.file
This way at least the file that you downloaded is working.
To view the image in R, have a look at
jj <- readJPEG("y.jpg",native=TRUE)
plot(0:1,0:1,type="n",ann=FALSE,axes=FALSE)
rasterImage(jj,0,0,1,1)
or how to read.jpeg in R 2.15
or Displaying images in R in version 3.1.0
Is there a way to read a saved plot in to R?
No, once the plot is saved as a .jpeg or .pdf or whatever image format you use, the back end data that is stored in the R object is lost.
You can save the R plot object using the save()
function and then call that back with the load()
function. However this will not be saved in a format that most other programs will recognize as an image. It is not something you could load into powerpoint.
If all you need is loading a straight up image into R, then see the answers to this question: how to read.jpeg in R 2.15
Created R Package, Unable to Display Photo
Your dependencies are not handled correctly. Here you explicitly load packages with library(...)
. That is not how one does that in an R package. You should add your dependencies to the Imports:
section of the DESCRIPTION
file and use the package::function()
syntax when calling the function. c.f. http://r-pkgs.had.co.nz/description.html#dependencies.
In addition, if you want the images to be installed with your package, you should place them for example in inst/pics
. You can then get the path to these files with
system.file("pics", <file-name>, package = "RanglaPunjab")
can't open saved plot in R
You did it backwards. Try this
pdf("deleteIt.pdf")
plot(1:10)
dev.off()
Start the device. Write to it. Turn it off.
Alternatively, as pointed out by @Spacedman in a comment, you can create a pdf with whatever is currently plotted by using dev.copy
like this:
plot(1:10)
dev.copy(pdf, "deleteIt.pdf")
dev.off()
Warning message: In file(file, rt)
As to the missing EOF (i.e. last line in file is corrupted)...
Usually, a data file should end with an empty line. Perhaps check your file if that is the case.
As an alternative, I would suggest to try out readLines()
. This function reads each line of your data file into a vector. If you know the format of your input, i.e. the number of columns in the table, you could do this...
number.of.columns <- 5 # the number of columns in your data file
delimiter <- "\t" # this is what separates the values in your data file
lines <- readLines("path/to/your/file.csv", -1L)
values <- unlist(lapply(lines, strsplit, delimiter, fixed=TRUE))
data <- matrix(values, byrow=TRUE, ncol=number.of.columns)
Related Topics
Change Color of Only One Bar in Ggplot
Differencebetween These Two Comparisons
Plotting a Large Number of Custom Functions in Ggplot in R Using Stat_Function()
Generate Matrix with Iid Normal Random Variables Using R
Add Annotation and Segments to Groups of Legend Elements
R:Ggplot2:Facet_Grid:How Include Math Expressions in Few (Not All) Labels
Add Vline to Existing Plot and Have It Appear in Ggplot2 Legend
What's My User Agent When I Parse Website with Rvest Package in R
How to Fill Nas with Locf by Factors in Data Frame, Split by Country
Keeping Zero Count Combinations When Aggregating with Data.Table
Email Dataframe as Table in Email Body with Sendmailr
Error with Ggplot2 Mapping Variable to Y and Using Stat="Bin"
How to Open an .Xlsb File in R
Assign New Data Point to Cluster in Kernel K-Means (Kernlab Package in R)