在ShinyR中显示png文件

时间:2018-07-21 15:12:05

标签: r shiny

我正在尝试使用Shiny显示png文件。文件已上传,但显示不正确。我同时包含了ui和服务器代码

ProcessEvent

1 个答案:

答案 0 :(得分:1)

您对输入不做任何操作(如@RolandASc所述)。而是在服务器中生成一个新的png文件。

作为来源,您需要添加input$file1$datapath才能使用通过UI上传的文件,如this答案中所述。

ui <- fluidPage(
      titlePanel("Upload Slide Image"),
      sidebarLayout(
        sidebarPanel(fileInput("file1", "Choose png File", multiple = TRUE, 
                               accept = c(".png")) ), # Input: Select a file ----
        mainPanel(imageOutput("myImage")) 
      )
    )

    server <- function(input, output, session){
      observe({
        if (is.null(input$file1)) return()
        output$myImage <- renderImage({
          ## Following three lines CREATE a NEW image. You do not need them
          #outfile <- tempfile(fileext = '.png')
          #png(outfile, width = 400, height = 300) # Generate the PNG
          #dev.off()

          list(src = input$file1$datapath, contentType = 'image/png',width = 400, height = 300,
               alt = "This is alternate text")
        }, deleteFile = TRUE)
      })
    }

    # Create Shiny app ----
    shinyApp(ui, server)

编辑:我在observe中添加了一个检查,以解决应用程序首次运行时的错误。