0

I have an R script that takes in a dataset and a list of keywords. It then creates a plot based on how many times each of the keywords occur in the dataset, and saves it as a png image. Now, I want to make the keyword list dynamic, i.e. I want the keywords to be user inputs. Somehow, in the environment I am working in, it so happens that once the user has entered a list of keywords, I execute the R script and present the user with the plot. Now when the user enters some more keywords, they are appended to the previous list and since the complete list of keywords are sent to the R script, it performs the complete task again. It is obvious that it is doing a lot of unnecessary work of finding the keywords that were there in the first run too.

For example, in the first run, list of keywords:- "One", "Two", "Three". In the second run, list of keywords:- "One", "Two", "Three", "Four", "Five", "Six"

In the second run, it wastes a lot of time working on keywords "One", "Two" and "Three". Since the dataset will be huge and so will the number of keywords, it will take a lot of time to execute. My question is, is there a way I could prevent this, retain the previous plot and modify the previous plot to present the new keywords as well?

  • 3
    It might help if you could post some code and example data. See: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – harkmug Aug 02 '13 at 19:28
  • 1
    It's not making png take time but data preparation. Save previous results in file (csv, RData) and use it in next request. – Marek Aug 02 '13 at 20:29
  • 1
    You can add new items to a plot (think as a new layer) using points etc. but cannot modify an existing plot without redrawing it completely. If your concern is about processing new data then that can be optimized. Either way you should post some code and example data. – Rohit Das Aug 02 '13 at 20:41
  • Is it somehow possible to parse a png image as a plot in R? – thenonacmguy Aug 03 '13 at 03:46

1 Answers1

0

In theory it is possible to read in a png file and use it as a background, then add additional points/lines to it, but it would be much more complicated (and probably produce much uglier results) than saving the information to create the plot and just recreating the plot from scratch.

To do this (using base graphics) you could read the existing png file using the readPNG function from the png package. Then create a new plot using plot.new and plot.window. You would then need to use par to set the plotting region to the entire device region. Then use rasterImage to plot the png file as a background. Now you need to use par again to set the plotting region and user coordinates to match the previous png plot (this could be the most complicated part and if you don't get it right then the results will be all wrong). Then you can use points and lines to add to the plot. Note that the quality of the points and labels from the first plot will probably decrease every time that you do this (a png file does not save the text, but rather information on what colors to make which pixels).

It would be better (as mentioned in the comments) to just save the information needed to create the plot, then recreate the plot with the added information.

You could also look at the ggplot2 package which stores all the information to create a plot in an object, then plots it when printed. You can add (using +) options, data, etc. to the plot information and it will take care of the replotting.

Greg Snow
  • 48,497
  • 6
  • 83
  • 110
  • As you say that parsing a png is not a good option, how do you suggest I avoid recalculations for keywords that have occurred previously? – thenonacmguy Aug 04 '13 at 05:52
  • @user1523478, we would need to know what calculations your are making and how you are calling your code to have any hope of making a useful suggestion on how to store the calculations between calls. – Greg Snow Aug 06 '13 at 12:46
  • I have a text, and a set of keywords and i am plotting a graph that shows how many times the keyword occurs in the text. – thenonacmguy Aug 06 '13 at 19:08