Отображение выбранного изображения из загрузки в Shiny UI - PullRequest
0 голосов
/ 09 июля 2019

Я хочу иметь возможность загружать несколько изображений с помощью ввода файлов и отображать одно изображение, выбранное в пользовательском интерфейсе

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

При таком решении отпечатки пути к данным и имени показывают мне правильный ответ, но я продолжаю получать ту же ошибку после попытки рендеринга изображения: «Предупреждение:Ошибка в базовом имени: ожидается символьный векторный аргумент ".

1 Ответ

0 голосов
/ 09 июля 2019

Вот решение с использованием кодировки 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)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...