【问题标题】:Display a selected image from upload in Shiny UI在 Shiny UI 中显示从上传中选择的图像
【发布时间】:2019-07-09 00:46:55
【问题描述】:

我希望能够通过文件输入上传多张图片并显示在 UI 中选择的单张图片

ui.R

library(shiny)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      fileInput("file","Upload the file", multiple = TRUE), # fileinput() function is used to get the file upload contorl option
      uiOutput("selectfile")
    ),
    mainPanel(
      uiOutput('images')


    )

  )
)

server.R

server <- function(input,output) {


  ## Side bar select input widget coming through renderUI()
  # Following code displays the select input widget with the list of file loaded by the user
  output$selectfile <- renderUI({
    if(is.null(input$file)) {return()}
    list(hr(), 
         helpText("Select the files for which you need to see data and summary stats"),
         selectInput("Select", "Select", choices=input$file$name)
    )

  })

  output$images <- renderImage({
    if(is.null(input$file)) {return(NULL)}
    for (i in 1:nrow(input$file))
    {
      if(input$file$name[i] == input$Select){
        list(src=input$file$datapath[i],
             alt= "error")
        print(input$file$name[i])
        print(input$file$datapath[i])
      }
    }
  })
}

使用此解决方案,数据路径和名称的打印显示了正确的答案,但在尝试渲染图像后我仍然收到相同的错误:“警告:basename 中的错误:预期的字符向量参数”。

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    这是使用base64编码的解决方案。

    library(shiny)
    library(base64enc)
    
    ui <- fluidPage(
      sidebarLayout(
        sidebarPanel(
          fileInput("file", "Upload the file", multiple = TRUE), 
          uiOutput("selectfile")
        ),
        mainPanel(
          uiOutput('image')
        )
      )
    )
    
    server <- function(input,output) {
      output$selectfile <- renderUI({
        req(input$file)
        list(hr(), 
             helpText("Select the files for which you need to see data and summary stats"),
             selectInput("Select", "Select", choices=input$file$name)
        )
      })
    
      output$image <- renderUI({
        req(input$Select)
        i <- which(input$file$name == input$Select)
        if(length(i)){
          base64 <- dataURI(file = input$file$datapath[i], mime = input$file$type[i])
          tags$img(src = base64, alt= "error")
        }
      })
    }
    
    shinyApp(ui, server)
    

    【讨论】:

    • 非常感谢!但我仍然想知道我的解决方案有什么问题,有什么想法吗?
    猜你喜欢
    • 2016-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-07
    • 1970-01-01
    • 2023-01-04
    • 2020-11-20
    • 1970-01-01
    相关资源
    最近更新 更多