Over Christmas break I teased some screencaps:
A extra refined #rstats #swift “SwiftR” instance. Easy Picture view + some textual content views, a colour picker and a button that runs
R-in-Swift code (like {reticulate} does for Python in R)
Notice no ssd/hd storage round-trip for the plot.
Code snippet: https://t.co/fWaHnztUgd pic.twitter.com/y5m1I16tCB
— Caliban’s Struggle (@hrbrmstr) December 28, 2020
Remaining teaser for #rstats R-in-Swift (SwiftR) since I _think_ I’ve ‘d all reminiscence leaks and refined the instance mission. The displayed 10,… numbers come proper from pattern()’d R vector and the fill colour reside refreshes quick b/c {magick} is magical. Shld have repo up Thu. pic.twitter.com/boGa5ej9FZ
— Caliban’s Struggle (@hrbrmstr) December 29, 2020
of some almost-natural “R” trying code (this can be a snippet):
Button("Run") {
do { // calls to R can fail so there are many "strive"s; poking at much less ugly alternate options
// dealing with dots in named calls is a WIP
_ = strive R.evalParse("choices(tidyverse.quiet = TRUE )")
// in follow this wld be known as as soon as in a mannequin
strive R.library("ggplot2")
strive R.library("hrbrthemes")
strive R.library("magick")
// can combine initialiation of an R listing with Swift and R objects
let mvals: RObject = [
"month": [ "Jan", "Feb", "Mar", "Apr", "May", "Jun" ],
"worth": strive R.pattern(100, 6)
]
// ggplot2! `mvals` is above, `col.hexValue` comes from the colour picker
// cannot do R.as.information.body b/c "dots" so this can be a intentionally uncovered alternate name
let gg = strive R.ggplot(R.as_data_frame(mvals)) +
R.geom_col(R.aes_string("month", "worth"), fill: col.hexValue) + // helps each [un]named
R.scale_y_comma() +
R.labs(
x: rNULL, y: "# issues",
title: "Month-to-month Bars"
) +
R.theme_ipsum_gs(grid: "Y")
// a substitute for {magick} may very well be getting uncooked SVG from {svglite} gadget
// we get Picture view width/top and go that to {magick}
// both beats disk/ssd round-trip
let fig = strive R.image_graph(
width: Double(imageRect.width),
top: Double(imageRect.top),
res: 144
)
strive R.print(gg)
_ = R.dev_off() // cannot do R.dev.off b/c "dots" so this can be a intentionally uncovered alternate name
let res = strive R.image_write(fig, path: rNULL, format: "png")
imgData = Information(res) // "imgData" is a reactive SwiftUI certain object; when it adjustments Picture does too
} catch {
}
}
that works in Swift as a part of a SwiftUI app that shows a ggplot2 plot within a macOS software.
It doesn’t shell out to R, however makes use of Swift 5’s native skills to interface with R’s C interface.
I’m not able to reveal that SwiftR code/library simply but (break’s over and the core bits nonetheless want some tweaking) however I can present some interim assets with an internet e-book about working with R’s C interface from Swift on macOS. It’s uninspiringly known as SwiftR — Utilizing R from Swift.
There are, at current, six chapters that introduce the Swift+R ideas by way of command line apps. These aren’t terribly helpful (shebanged R scripts work simply effective, #tyvm) in and of themselves, however command line machinations are a a lot decrease barrier to entry than beginning proper in with SwiftUI (that begins in chapter seven).
FIN
When you’ve wished a cause to burn ~20GB of drive area with an Xcode set up and begin to be taught Swift (or be taught extra about Swift) then this can be a useful resource for you.
The matters within the chapters are additionally a reasonably first rate (albeit incomplete) overview of R’s C interface and in addition work with C code from Swift on the whole.
So, reap the benefits of the remaining pandemic time and provides it a .
Suggestions is welcome within the feedback or the e-book code repo (e-book supply repo is in progress).
Hope everybody has a secure and powerful new 12 months!
*** This can be a Safety Bloggers Community syndicated weblog from rud.is authored by hrbrmstr. Learn the unique publish at: https://rud.is/b/2021/01/04/bringing-r-to-swift-on-macos/