使用 R 语言实现新媒体艺术作品

新媒体艺术

Authot: Tealeaf, Contra, AnYong Cui

2012 除夕夜北京地区短信数据的可视化作品

What Is Processing

Processing is a flexible software sketchbook and a language for learning how to code within the context of the visual arts

A Programmer's Perspective

  • 输出是图形或者动画的编程语言
  • 语法与 Java 类似,部分复用了 Java 的 compiler

What Is Processing

What could Processing do?

What could Processing do?

What could Processing do?

What Is R

You know better than me!

What Is Processing.R

Outline

  • Installation

  • HOWTO

    • 2D

    • Event System

    • 3D

    • Libraries

    • R Packages​​

  • ​​Implementation

关于我

  • 上海交通大学 软件学院 研究生二年级
  • Processing.R 作者

Blog GitHub Resume Resume in Chinese SJTU Master

How to Install

2D

Event

settings <- function() {
    size(480, 120)
}

draw <- function() {
    if (mousePressedVar == TRUE) {
        fill(0)
    } else {
        fill(255)
    }
    ellipse(mouseX, mouseY, 80, 80)
}

Event

3D

settings <- function() {
    size(640, 360, P3D)
}

draw <- function() {
    background(0)
    camera(mouseX, height/2, 
      (height/2) / tan(PI/6), width/2, height/2, 0, 0, 1, 0)
    translate(width/2, height/2, -100)
    stroke(255)
    noFill()
    box(200)
}

3D

Library

Library

R Packages Support

Documentation

Technologies Behind Processing.R

  • Java RMI
  • JVM-based R Interpreter

Architecture

Renjin

Renjin is an interpreter for the R programming language for statistical computing written in Java much like JRuby and Jython are for the Ruby and Python programming languages.

The biggest advantage of Renjin is that the R interpreter itself is a Java module which can be seamlessly integrated into any Java application.

public class TryRenjin {
  public static void main(String[] args) throws Exception {
    // create a script engine manager:
    RenjinScriptEngineFactory factory 
        = new RenjinScriptEngineFactory();
    // create a Renjin engine:
    ScriptEngine engine = factory.getScriptEngine();

    engine.eval("df <- data.frame(x=1:10, y=(1:10)+rnorm(n=10))");
    engine.eval("print(df)");
    engine.eval("print(lm(y ~ x, df))");

  }
}

Mixing Java & R code

Raw Logic in Processing

public class PApplet {

    public void draw() {
        // Insert the logic to the template and compile it.
    }
}

Mixing Java & R code

Trade-off in Processing.R

public class PApplet {

    public void draw() {
        rInterpreterInJVM.eval(code.getFunction("draw"))
    }
}

Mixing Java & R code

Pros & Cons

  • 实现简单
  • 与 Java 部分解耦

 

  • 不能实现 Java 回调 R 中函数

Future Work

  • 实现对更多 R Packages 和 Processing Libraries 的支持
  • 支持 R style API (Matrix 等等)

Thanks

Q&A

Processing.R

By gaocegege

Processing.R

Introduction to Processing.R

  • 614
Loading comments...

More from gaocegege