Skip to content

Instantly share code, notes, and snippets.

@lujiacn
Created March 2, 2015 10:03
Show Gist options
  • Save lujiacn/aa4bdbdbe6eca056b1c2 to your computer and use it in GitHub Desktop.
Save lujiacn/aa4bdbdbe6eca056b1c2 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
// "github.com/nchern/go-R/R"
"io/ioutil"
"os"
"os/exec"
"strings"
"time"
)
func test() {
//tmp files for r and png
r_file, _ := ioutil.TempFile(os.TempDir(), "r_")
img_file, _ := ioutil.TempFile(os.TempDir(), "img_")
r_script := `
library("ggplot2")
# Define the cars vector with 5 values
cars <- c(1, 3, 6, 4, 9)
# Graph the cars vector with all defaults
qplot(cars)
ggsave("&&img_file_name", device=svg)
dev.off()
`
r_script = strings.Replace(r_script, "&&img_file_name", img_file.Name(), -1)
ioutil.WriteFile(r_file.Name(), []byte(r_script), 0x777)
fmt.Println("R file name:", r_file.Name())
fmt.Println("svg file name:", img_file.Name())
output, err := exec.Command("Rscript",
"--vanilla",
r_file.Name()).Output()
if err != nil {
fmt.Printf("%v", err)
}
fmt.Println(string(output))
time.Sleep(time.Second * 30)
defer os.Remove(r_file.Name())
defer os.Remove(img_file.Name())
}
func main() {
// R.Init()
// for i := 0; i < 10; i++ {
// fmt.Println(i)
// test()
// }
test()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment