【问题标题】:How to Upload Image File, Display and Remove it on Shiny R如何在 Shiny R 上上传、显示和删除图像文件
【发布时间】:2021-11-17 12:25:56
【问题描述】:

需要帮助,我想上传一个图片文件显示它闪亮并且可以删除图片。

我已经试过了,但问题是第一次尝试上传文件是好的,但第二次尝试是图像没有显示。

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)

非常感谢任何解决方案

【问题讨论】:

    标签: r shiny shinyapps


    【解决方案1】:

    使用您的removeUI,您正在移除容器。改为删除其内容:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-20
      • 2015-01-07
      • 2016-02-05
      • 2013-04-04
      • 2020-07-29
      • 2016-08-16
      • 1970-01-01
      相关资源
      最近更新 更多