【问题标题】:How can I browse and upload an image in a shiny application?如何在闪亮的应用程序中浏览和上传图像?
【发布时间】:2019-10-27 02:18:15
【问题描述】:

我想创建一个闪亮的应用程序,让用户能够浏览和加载图像,然后显示它。我的问题是闪亮是否支持这一点。

#ui.r
pageWithSidebar(
  headerPanel('Image Recognition'),
  sidebarPanel(
    fileInput("file1", "Choose Image",
              accept = c(
                ".jpg")
    ))
   ,
  mainPanel(
    imageOutput("file1")
  )
)
#server.r
library(shiny)

function(input, output, session) {
(shiny.maxRequestSize=30*1024^2) 

  output$myImage <- renderImage({
    # A temp file to save the output.
    # This file will be removed later by renderImage
    file1 <- tempfile(fileext = '.png')

    # Generate the PNG
    png(file1, width = 400, height = 300)
    dev.off()

    # Return a list containing the filename
    list(src = file1,
         contentType = 'image/png',
         width = 400,
         height = 300,
         alt = "This is alternate text")
  }, deleteFile = TRUE)



}

【问题讨论】:

  • 当心所选文件的路径:它通常来自一个格式错误的文件路径,它来自一个非工作块;)此外,当您调用 png() 时没有定义 outfile 但是只有file1
  • 我使用 (shiny.maxRequestSize=30*1024^2) 作为尺寸,但它不起作用。我也改变了outfile
  • 请问回溯日志呢?
  • shiny.maxRequestSize 是您需要设置的选项,而不是您需要创建的变量。删除该行并将 options(shiny.maxRequestSize = 30*1024^2) 放在 server.R 文件中的 library(shiny) 调用之后。
  • 编辑问题以删除文件大小特定的错误/问题是有意义的。但也许这样的事情已经回答了另一个问题:community.rstudio.com/t/…

标签: r shiny


【解决方案1】:

这是一个使用base64编码的上传文件的解决方案。

library(shiny)
library(base64enc)

options(shiny.maxRequestSize = 30*1024^2)

ui <- fluidPage(
  fileInput("upload", "Upload image", accept = "image/png"),
  uiOutput("image")
)

server <- function(input, output){

  base64 <- reactive({
    inFile <- input[["upload"]]
    if(!is.null(inFile)){
      dataURI(file = inFile$datapath, mime = "image/png")
    }
  })

  output[["image"]] <- renderUI({
    if(!is.null(base64())){
      tags$div(
        tags$img(src= base64(), width="100%"),
        style = "width: 400px;"
      )
    }
  })
}

shinyApp(ui, server)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-03
    • 2013-07-04
    • 1970-01-01
    • 1970-01-01
    • 2014-03-26
    • 2018-11-09
    • 2020-05-09
    • 1970-01-01
    相关资源
    最近更新 更多