Why Swift?
Data science is dominated by Python and R, with some usage of Julia, Scala, Java, and C++. While Swift may not be the most popular choice, it offers several notable benefits—especially for developers already invested in the Apple ecosystem.
Key Advantages
-
Performance Considerations
As a compiled language, Swift often runs faster than languages like Python or R. This can be especially beneficial when handling large datasets or complex computations. -
Safety & Reliability
Swift’s strong type system, optional handling, and memory safety features help you write more robust and secure code with fewer runtime errors. -
Ecosystem & Tooling
Several libraries and frameworks support data science in Swift, such as Swift Numerics for numerical computing and Swift for TensorFlow for machine learning. -
Integration with Existing Codebases
Swift integrates smoothly with existing iOS and macOS projects or C/C++ libraries. This allows teams to unify app logic and data science components under one language and codebase.
Commercialization Potential
For projects intended for the Apple ecosystem, Swift-based development can streamline the path from prototype to product. Reusing large parts of your data science pipeline directly within an iOS or macOS app reduces development overhead. This level of end-to-end integration is often more cumbersome when using non-Swift languages, making Swift an attractive option for commercial applications.
Initial Configuration
In data science workflows, the Swift REPL (Read-Eval-Print Loop) provides an interactive environment that runs Swift code line by line, making it easy to test ideas and quickly prototype. In this blog post, I will use the Swift REPL within an R Markdown document by leveraging the knitr
package. This setup allows me to execute Swift code blocks directly while seamlessly incorporating the output into the rendered document, streamlining both experimentation and content creation.
Adding Swift as engine to knitr
The first step is to integrate a custom Swift engine into knitr. We have several goals for this setup:
-
Maintain a cumulative namespace across chunks.
If a variablevarA
is defined in the first chunk andvarB
in the second, the environment for the second chunk should include bothvarA
andvarB
. -
Limit scope retroactively.
Newly defined variables remain isolated to the chunk in which they appear, so a variablevarC
defined in the third chunk does not retroactively affect the first or second. -
Reduce redundant output.
Minimize repeatedprint
statements, ideally preserving only the final output for clarity.
This can be done using the below command 1.
|
|
What happens here:
- Function
knitr::knit_engines$set
registers new engine. Engine is define as new function calledswift
. - The call
swift_chunk_names[seq_len(Position( \(x) x == knitr::opts_current$get("label"), swift_chunk_names ))]
identifies current chunk and ensures that only the previous and the current chunk are passed into evaluation engine. FunctionalPosition
will return a number of element meting criteria. Notation\(x)
was introduced in R 4.1.0 and is a shorthand forfunction(x)
, e.g.\(x) x + 1
is parsed asfunction(x) x + 1
. - Call
Reduce( \(x, y) { paste(x, knitr::knit_code$get(y), sep = "\n") }, prior_chunk_names, init = "")
combines the previous Swift code blocks in one text. - Subsequent calls do a trivial vector substitution and remove all other than penultimate print statement.
Testing
Let’s attempt to evaluate a trivial statement
|
|
|
|
Let’s see if we can continue using the variables created below and re-use variable from the previous statement
|
|
|
|
Conclusion
By setting up a custom Swift engine in knitr, you can seamlessly execute Swift REPL commands within an R Markdown document and capture the output for immediate display. This allows for rapid experimentation, straightforward debugging, and convenient sharing of code alongside explanatory text—qualities essential for any data science workflow. With just a few lines of configuration, Swift’s performance and safety become accessible in an interactive environment, letting you prototype data manipulation, statistical analysis, or even machine learning models right in your R Markdown files.
-
The original code was contributed via StackOverflow discussion; I’ve re-wrote it using R’s functional programming and reduced the code to a few lines. ↩︎