代码之家  ›  专栏  ›  技术社区  ›  Mark

将绘图文本/二进制写入变量

  •  2
  • Mark  · 技术社区  · 15 年前

    有没有办法 R device(postscript很好)是否将输出写入变量而不是文件?

    例如,我知道:

    postscript(file="|cat")
    plot(1:10)
    dev.off()
    

    将PostScript文本发送到 STDOUT . 如何将该文本转换为 R ?

    4 回复  |  直到 9 年前
        1
  •  3
  •   stotastic    15 年前

    我已经成功地将一个图的二进制数据作为字符串转换成了一个r变量。它有一些读/写开销。在下面的代码片段中,R将绘图保存为一个临时文件,并将其重新读取。

    ## create a plot
    x <- rnorm(100,0,1)
    hist(x, col="light blue")
    
    ## save plot as temp file
    png(filename="temp.png", width=500, height=500)
    print(p)
    dev.off()
    
    ## read temp file as a binary string
    plot_binary <- paste(readBin("temp.png", what="raw", n=1e6), collapse="")
    

    也许这对你有帮助。

        2
  •  1
  •   Jeff    15 年前

    PostScript接受命令参数,因此PostScript(file=“”,command=“cat”)

        3
  •  1
  •   Harlan    15 年前

    你到底为什么要这么做?R对于操作PostScript文件不是一个很好的系统。如果没有其他内容,则可以使用tempfile()将图像写入文件,然后使用标准文件函数读取该文件。如果你想成为一名花花公子,你可以使用fifo()管道,但我怀疑它会更快。但我想你最好换个方法。

        4
  •  0
  •   Collin    15 年前

    您应该能够使用如下的textconnection。

    tc <- textConnection("string", "w")
    
    postscript(tc)
    plot(1:10)
    dev.off()
    

    但是 string 保持空白-可能是个虫子?

    推荐文章