Hi there, I hope I am sending this message to the right list. This is a ?where should I start?? type question. I am a JavaScript developer, working on ideas for more accessible Maths and Stats coursework. I have worked with RMarkdown and knitr extensively to create HTML coursework but I?m by no means an R developer! I recently came across a project called mermaid.js, which is designed for diagrams but has limited charting syntax: https://mermaid.js.org/syntax/xyChart.html This project aims to have plain text representation of graphics in markdown files, which when converted to HTML (either at compile time or at runtime), can turn these ?mermaid? code blocks into a diagram/plot via a JavaScript parser and plotting library. This has inspired me to attempt to create a proof-of-concept for R along these lines. For example this R code: library(ggplot2) library(palmerpenguins) ggplot(data, aes(x=body_mass_g,fill = species)) + geom _histogram() Could output something like: title "The body mass (g) of penguin species" x-axis "Body mass (g)" 3000 --> 5550 y-axis "Count" 0 --> 2 histogram Adelie [3000, 3250, 3400] ChinStrap [3250, 3600] Gentoo [4300, 5050, 5200, 5300, 5450] How should I go about this in R? I feel I should be aiming to create a new ?device? or ?output format?, but I haven?t found any examples on how to do this. One of my colleagues suggested I hook into ggplot_build, but then of course the project would be limited to ggplot plots. Any help greatly appreciated ? Thanks, David McArthur School of Mathematics & Statistics University of Glasgow [[alternative HTML version deleted]]
You might get more/additional feedback from the r-help list: r-help at r-project.org On Tue, Jun 25, 2024 at 2:18?PM David McArthur < David.McArthur.2 at glasgow.ac.uk> wrote:> Hi there, I hope I am sending this message to the right list. > > This is a ?where should I start?? type question. > > I am a JavaScript developer, working on ideas for more accessible Maths > and Stats coursework. I have worked with RMarkdown and knitr extensively > to create HTML coursework but I?m by no means an R developer! > > I recently came across a project called mermaid.js, which is designed for > diagrams but has limited charting syntax: > https://mermaid.js.org/syntax/xyChart.html > > This project aims to have plain text representation of graphics in > markdown files, which when converted to HTML (either at compile time or at > runtime), can turn these ?mermaid? code blocks into a diagram/plot via a > JavaScript parser and plotting library. > > This has inspired me to attempt to create a proof-of-concept for R along > these lines. > > For example this R code: > > library(ggplot2) > library(palmerpenguins) > > ggplot(data, aes(x=body_mass_g,fill = species)) + > geom _histogram() > > Could output something like: > > title "The body mass (g) of penguin species" > x-axis "Body mass (g)" 3000 --> 5550 > y-axis "Count" 0 --> 2 > histogram > Adelie [3000, 3250, 3400] > ChinStrap [3250, 3600] > Gentoo [4300, 5050, 5200, 5300, 5450] > > How should I go about this in R? I feel I should be aiming to create a > new ?device? or ?output format?, but I haven?t found any examples on how to > do this. > > One of my colleagues suggested I hook into ggplot_build, but then of > course the project would be limited to ggplot plots. > > Any help greatly appreciated ? > > Thanks, > > David McArthur > School of Mathematics & Statistics > University of Glasgow > > [[alternative HTML version deleted]] > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >[[alternative HTML version deleted]]
? Tue, 25 Jun 2024 09:42:59 +0000 David McArthur <David.McArthur.2 at glasgow.ac.uk> ?????:> ggplot(data, aes(x=body_mass_g,fill = species)) + > geom _histogram() > > Could output something like: > > title "The body mass (g) of penguin species" > x-axis "Body mass (g)" 3000 --> 5550 > y-axis "Count" 0 --> 2 > histogram > Adelie [3000, 3250, 3400] > ChinStrap [3250, 3600] > Gentoo [4300, 5050, 5200, 5300, 5450] > > How should I go about this in R?R graphics devices are very low-level: https://cran.r-project.org/doc/manuals/r-devel/R-ints.html#Graphics-devices Instead of drawing histograms, they are asked to draw points, lines, polygons, and text. If you're curious what it's like to implement such a device, packages such as 'devEMF' and 'ragg' will provide examples. You could instead go the 'txtplot' route and implement your own functions that would return text in mermaid syntax, unrelated to existing plotting engines. An alternative would be to carefully deconstruct 'ggplot2' and 'lattice' objects and translate what you can into mermaid diagrams, but that will always be limited to the intersection of the source and target featuresets. -- Best regards, Ivan
On 25/06/2024 10:42, David McArthur wrote:> Hi there, I hope I am sending this message to the right list.This or r-package-devel.> This is a ?where should I start?? type question.With the R-Internals manual and the grDevices sources and the examples there. Another possibility is to translate one of the several 'text-based' graphics formats already supported, including postscript ASCII PDF svg xfig pictex and others in packages (R2SWF devEMF httpgd tikzDevice ...). The quality of R graphics depends heavily on the precise positioning of (possibly non-ASCII) text, and so font-handling is by far the hardest part of writing a graphics device (and I have written several). So it makes sense to leverage what is already done, by modifying sources or translating output. This would have been easier if graphics standards and R-at-the-time supported UTF-8, and the lack of such support is why some of these have been deprecated.> > I am a JavaScript developer, working on ideas for more accessible Maths and Stats coursework. I have worked with RMarkdown and knitr extensively to create HTML coursework but I?m by no means an R developer! > > I recently came across a project called mermaid.js, which is designed for diagrams but has limited charting syntax: > https://mermaid.js.org/syntax/xyChart.html > > This project aims to have plain text representation of graphics in markdown files, which when converted to HTML (either at compile time or at runtime), can turn these ?mermaid? code blocks into a diagram/plot via a JavaScript parser and plotting library. > > This has inspired me to attempt to create a proof-of-concept for R along these lines. > > For example this R code: > > library(ggplot2) > library(palmerpenguins) > > ggplot(data, aes(x=body_mass_g,fill = species)) + > geom _histogram() > > Could output something like: > > title "The body mass (g) of penguin species" > x-axis "Body mass (g)" 3000 --> 5550 > y-axis "Count" 0 --> 2 > histogram > Adelie [3000, 3250, 3400] > ChinStrap [3250, 3600] > Gentoo [4300, 5050, 5200, 5300, 5450] > > How should I go about this in R? I feel I should be aiming to create a new ?device? or ?output format?, but I haven?t found any examples on how to do this. > > One of my colleagues suggested I hook into ggplot_build, but then of course the project would be limited to ggplot plots. > > Any help greatly appreciated ? > > Thanks, > > David McArthur > School of Mathematics & Statistics > University of Glasgow > > [[alternative HTML version deleted]] > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel-- Brian D. Ripley, ripley at stats.ox.ac.uk Emeritus Professor of Applied Statistics, University of Oxford