【问题标题】:display png file in shinyR在闪亮的 R 中显示 png 文件
【发布时间】:2018-07-22 01:10:18
【问题描述】:

我正在尝试使用闪亮显示 png 文件。文件已上传但未正确显示。我已经包含了 ui 和服务器代码

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){
  output$myImage <- renderImage({
    outfile <- tempfile(fileext = '.png')
    png(outfile, width = 400, height = 300) # Generate the PNG
    dev.off()
    list(src = outfile,contentType = 'image/png',width = 400, height = 300,
         alt = "This is alternate text")
  }, deleteFile = TRUE)
}

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

【问题讨论】:

  • 您似乎没有对边栏中提供的fileInput 执行任何操作

标签: r shiny


【解决方案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 中添加了一个检查,以应对应用首次运行时出现的错误。

【讨论】:

  • 这个答案可以通过避免在应用程序首次运行时出现“错误:无效的文件名参数”消息来稍微改进
猜你喜欢
  • 2014-06-16
  • 2018-03-23
  • 1970-01-01
  • 2018-12-21
  • 1970-01-01
  • 1970-01-01
  • 2018-07-07
  • 1970-01-01
  • 2021-09-02
相关资源
最近更新 更多