Shiny image upload

吃可爱长大的小学妹 提交于 2020-01-03 05:47:04

问题


Not certain why this simple code is not working. The goal is to upload an image from a device and view as shiny output. I keep getting an error, *****Error: invalid filename argument*****

library(jpeg)
library(shiny)
library(magick)
library(magrittr)




  ui <- fluidPage(

#-------------------------------------Header Panel--------------------------------------------------#

titlePanel('Invoice Recognition & Interpretation -IRI'),

#--------------------------------Sidebar : Image Upload---------------------------------------------#
sidebarLayout(

  sidebarPanel(
    fileInput(inputId = "file1", 
              label = "Upload Invoice",
             accept = c('image/png', 'image/jpeg','image/jpg')
    ),

    tags$hr()

  ),


  mainPanel( 

  imageOutput(outputId = "Invoice")

    )
)
)


server <- function(input, output) {

re1<-reactive({ input$file1})

output$Invoice<-renderImage({re1()})

 }
shinyApp(ui, server)

回答1:


You are most of the way there, however, you need to understand that your fileInput returns a data frame and not just the path. Also, the renderImage is similar to an <img> and you need to assign the src.

library(shiny)

ui <- fluidPage(

  titlePanel('Invoice Recognition & Interpretation -IRI'),

  sidebarLayout(
    sidebarPanel(
      fileInput(
        inputId = "file1",
        label = "Upload Invoice",
        accept = c('image/png', 'image/jpeg','image/jpg')
      ),
      tags$hr()
    ),
    mainPanel(
      textOutput("filename"),
      imageOutput(outputId = "Invoice")
    )
  )
)

server <- function(input, output) {

  re1 <- reactive({gsub("\\\\", "/", input$file1$datapath)})

  output$Invoice <- renderImage({list(src = re1())})

}

shinyApp(ui, server)


来源:https://stackoverflow.com/questions/47683197/shiny-image-upload

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!