Home > Software engineering >  How to Upload Image File, Display and Remove it on Shiny R
How to Upload Image File, Display and Remove it on Shiny R

Time:09-24

Need Help, I would like to upload an image file display it on shiny and the image can be remove.

I already tried but the problem is first attemp upload file is OK but second attemp is the image are not displayed.

library(shiny)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      fluidRow( 
        fileInput("myFile", "Choose a file", accept = c('image/png', 'image/jpeg')),
        actionButton('reset', 'Reset Input')
      )
    ),
    mainPanel(
      div(id = "image-container", style = "display:flexbox")
    )
  )
)


server <- function(input, output) {
  
  
  observeEvent(input$myFile, {
    inFile <- input$myFile
    if (is.null(inFile))
      return()
    
    b64 <- base64enc::dataURI(file = inFile$datapath, mime = "image/png")
    insertUI(
      selector = "#image-container",
      where = "afterBegin",
      ui = img(src = b64, width = 600, height = 600)
    )
  })
  
  
  
  observeEvent(input$reset, {
    removeUI(
      selector = "#image-container",
      
      
    )
  })
    
  
  
} 
shinyApp(ui = ui, server = server)

Any solution is really appreciated

CodePudding user response:

With your removeUI you are removing the container. Remove its content instead:

  observeEvent(input$reset, {
    removeUI(
      selector = "#image-container > *"
    )
  })
  • Related