如何上传图像文件,在Shiny r上显示和删除它



需要帮助,我想上传一个图像文件,显示在闪亮和图像可以删除。

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

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)

任何解决方案都非常感谢

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

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

最新更新