如何在闪亮的应用程序中浏览和上传图像?

时间:2019-06-12 14:18:17

标签: r shiny

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

#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)



}

1 个答案:

答案 0 :(得分: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)

enter image description here