繁体   English   中英

显示在Shiny UI中上传的选定图像

[英]Display a selected image from upload in Shiny UI

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

用户界面

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 <- 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])
      }
    }
  })
}

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

这是使用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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM