{"action":"created","issue":{"url":"https://api.github.com/repos/hadley/svglite/issues/46","labels_url":"https://api.github.com/repos/hadley/svglite/issues/46/labels{/name}","comments_url":"https://api.github.com/repos/hadley/svglite/issues/46/comments","events_url":"https://api.github.com/repos/hadley/svglite/issues/46/events","html_url":"https://github.com/hadley/svglite/pull/46","id":117718319,"number":46,"title":"Allowing outputing SVG in different streams (terminal/file/string)","user":{"login":"yixuan","id":216398,"avatar_url":"https://avatars.githubusercontent.com/u/216398?v=3","gravatar_id":"","url":"https://api.github.com/users/yixuan","html_url":"https://github.com/yixuan","followers_url":"https://api.github.com/users/yixuan/followers","following_url":"https://api.github.com/users/yixuan/following{/other_user}","gists_url":"https://api.github.com/users/yixuan/gists{/gist_id}","starred_url":"https://api.github.com/users/yixuan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/yixuan/subscriptions","organizations_url":"https://api.github.com/users/yixuan/orgs","repos_url":"https://api.github.com/users/yixuan/repos","events_url":"https://api.github.com/users/yixuan/events{/privacy}","received_events_url":"https://api.github.com/users/yixuan/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":7,"created_at":"2015-11-19T01:13:02Z","updated_at":"2016-01-08T17:49:55Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/hadley/svglite/pulls/46","html_url":"https://github.com/hadley/svglite/pull/46","diff_url":"https://github.com/hadley/svglite/pull/46.diff","patch_url":"https://github.com/hadley/svglite/pull/46.patch"},"body":"This PR is an attempt for the proposal in https://github.com/hadley/svglite/issues/40.\r\n\r\nApart from saving SVG to a file in hard disk, this patch adds two additional way to save the result: to print the content of SVG in the R terminal, and to save the SVG as a string.\r\n\r\nThe design consists of the following aspects:\r\n- The C-style printing functions are replaced by C++ style streams.\r\n- In this way, we can use a common parent class `std::ostream` to unify different ways of output. Terminal stream is handled by `Rcpp::Rcout`, file stream by `std::ofstream` and string stream by `std::ostringstream`.\r\n- To tell the device that SVG should be printed to the R terminal, use `svglite(file = \":terminal:\")` in the driver function; similarly, specify `svglite(file = \":string:\")` for writing to string stream.\r\n- The string stream version is a bit tricky, since we cannot expect `dev.off()` to return the wanted string. Instead, I added another function `stringSVG()` to extract it. The usage will look like\r\n```r\r\nsvglite(\":string:\")\r\nplot.new()\r\ndev.off()\r\nsvgstr <- stringSVG()\r\ncat(svgstr)\r\n```\r\n`stringSVG()` always returns the last plot created by `svglite(\":string:\")`.\r\n\r\nAny further comments or suggestions?\r\n"},"comment":{"url":"https://api.github.com/repos/hadley/svglite/issues/comments/170072840","html_url":"https://github.com/hadley/svglite/pull/46#issuecomment-170072840","issue_url":"https://api.github.com/repos/hadley/svglite/issues/46","id":170072840,"user":{"login":"noamross","id":571752,"avatar_url":"https://avatars.githubusercontent.com/u/571752?v=3","gravatar_id":"","url":"https://api.github.com/users/noamross","html_url":"https://github.com/noamross","followers_url":"https://api.github.com/users/noamross/followers","following_url":"https://api.github.com/users/noamross/following{/other_user}","gists_url":"https://api.github.com/users/noamross/gists{/gist_id}","starred_url":"https://api.github.com/users/noamross/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/noamross/subscriptions","organizations_url":"https://api.github.com/users/noamross/orgs","repos_url":"https://api.github.com/users/noamross/repos","events_url":"https://api.github.com/users/noamross/events{/privacy}","received_events_url":"https://api.github.com/users/noamross/received_events","type":"User","site_admin":false},"created_at":"2016-01-08T17:49:55Z","updated_at":"2016-01-08T17:49:55Z","body":"I had missed this and realized I implemented some what @jeroenooms in #53.  The goal there was to support live preview, too.\r\n\r\n@yixuan, I did this by writing `</svg>`, flushing the graphics stream, and then setting the location back 7 bytes after each callback.  This way the document is alwyas valid SVG and the next command will overwrite the `</svg>`. \r\n\r\n```\r\nfputs(\"</svg>\\n\", svgd->file)\r\nfflush(svgd->file);\r\nfseek(svgd->file, -7, SEEK_CUR);\r\n```\r\n\r\nThere are equivalents of each of these for `ofstream`."}}