R - 如何使用连接而不是文件?

R - How to use connection instead of file?

是否有一种通用的方法来提供一个函数,该函数期望将文本文件作为字符串而不是输入?

我想将 56000+ svg 'files' 转换为 raster/grid 格式(位图、png 或类似格式)

但是 svg 文件不是作为文件存储的,而是作为数据框中的字符串存储的

我以为我可以使用 textConnection,但我运气不好:

> pseudo_file <- textConnection(data[1,"svg"])
> bitmap <- rsvg(pseudo_file)
Error: is.raw(svg) is not TRUE

以下适合我。确保字符串被识别为 R 的正确 URL:\ 应替换为 \/

library(rsvg)
myfiles <- c("example.svg" , "example.svg")
for(i in myfiles) {
  test <- rsvg(i)
}
test

如果 SVG 是字符串,您可以使用 charToRaw 将 SVG 字符串转换为原始数据。