【问题标题】:Save base64 JPG to disk in R - Shiny在 R 中将 base64 JPG 保存到磁盘 - 闪亮
【发布时间】:2016-10-09 10:12:25
【问题描述】:

我有以下数据框,可以从here 下载。 image_path 列包含 base64 格式的 jpg 文件。我想提取图像并将其存储在本地文件夹中。我尝试使用herehere 给出的代码。

虽然第二个完美地在浏览器中打开了图像,但我不知道如何在本地保存文件。我尝试了以下代码:

library(shiny)
for (i in 1:length(df)){
file <- paste(df$id[i])
png(paste0(~images/file, '.png'))
tags$img(src = df$image_path[i])
dev.off()
}

以下只是运行,但不创建任何图像文件,也没有显示错误。当我尝试运行tags$img(src = df$image_path[1]) 以查看它是否生成图像时,它不会。我知道 tags$img 是一个闪亮的函数,当我在 ui 中传递它时可以工作(正如@daatali 所建议的那样),但不知道如何在本地保存文件。

我想要的是从闪亮的服务器环境中运行一个 for 循环,并将图像本地保存为 jpg,使用 id 编号作为文件名,可以使用调查中捕获的各种其他详细信息进行渲染。

我从未使用过图像,如果这完全是新手,请多多包涵。

【问题讨论】:

    标签: r image shiny jpeg


    【解决方案1】:

    这会从 base64 字符串创建您的图像并将文件保存到您当前的工作目录,子文件夹“/images/”This article describes pretty well how to save files locally in Shiny.

    library(shiny)
    library(base64enc)
    filepath <- "images/"
    dir.create(file.path(filepath), showWarnings = FALSE)
    df <- read.csv("imagefiletest.csv", header=T, stringsAsFactors = F)
    for (i in 1:nrow(df)){
      if(df[i,"image_path"] == "NULL"){
        next
      }
      testObj <- strsplit(df[i,"image_path"],",")[[1]][2]
      inconn <- testObj
      outconn <- file(paste0(filepath,"image_id",df[i,"id"],".png"),"wb")
      base64decode(what=inconn, output=outconn)
      close(outconn)
    }
    

    【讨论】:

    • 非常感谢....非常感谢....过去两天让我头疼...这真的很有帮助....谢谢。
    猜你喜欢
    • 1970-01-01
    • 2011-10-19
    • 1970-01-01
    • 1970-01-01
    • 2012-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-21
    相关资源
    最近更新 更多