[英]R Shiny - display image in UI
我正在嘗試從 www 文件夾加載圖像(這部分有效),然后使用圖像名稱在 UI 中顯示它。 當我嘗試這樣做時,出現以下錯誤:警告:cat 中的錯誤:參數 1(類型“closure”)無法由“cat”處理
這是相當簡單的代碼'''
library(shiny)
library(imager)
setwd("E:/CIS590-03I Practical Research Project/Project")
# ui object
ui <- fluidPage(
titlePanel(p("Dog Breed Classification", style = "color:#3474A7")),
sidebarLayout(
sidebarPanel(
fileInput("image",
"Select your image:", placeholder = "No file selected"),
tags$head(
tags$style("body .sidebar {background-color: white; }",
".well {background-color: white ;}"),
),
p("Image to categorize"),
),
mainPanel(htmlOutput("testHTML"),
)
)
)
# server()
server <- shinyServer(function(input, output) {
output$testHTML <- renderText({
paste("<b>Selected image file is: ", input$image$name, "<br>")
reactive(img(
src = input$image$name,
width = "250px", height = "190px"
))
})
})
# shinyApp()
shinyApp(ui = ui, server = server)
'''
任何幫助將不勝感激。 謝謝你,比爾。
您收到錯誤消息的原因是renderText
返回一個反應性 function 而不是圖像 HTML 標簽。 reactive
不應出現在任何render...
function。
正如@MrFlick 所提到的, renderText
只會向 UI 返回一個字符串。 renderUI
和uiOutput
的替代方案是renderImage
和imageOutput
。 這些將以方便的方式將上傳的圖像添加到 UI,因為渲染 function 只需要一個屬性列表來提供img
標簽。 這也允許輕松包含不在www
目錄中的圖像。
在下面的解決方案中,我將req
包含在渲染函數中,以便在沒有上傳圖像時不會出現錯誤消息。
library(shiny)
ui <- fluidPage(
tags$head(
tags$style(
".sidebar {background-color: white;}",
".well {background-color: white;}",
".title-text {color: #3474A7;}"
)
),
h2(
class = "title-text",
"Dog Breed Classification"
),
sidebarLayout(
sidebarPanel(
fileInput(
"image",
"Select your image:",
placeholder = "No file selected"
),
p("Image to categorize")
),
mainPanel(
tags$p(textOutput("filename", container = tags$b)),
imageOutput("testHTML")
)
)
)
server <- function(input, output) {
output$filename <- renderText({
req(input$image)
paste("Selected image file is:", input$image$name)
})
output$testHTML <- renderImage({
req(input$image)
list(
src = input$image$datapath,
width = "250px",
height = "190px"
)
}, deleteFile = TRUE)
}
shinyApp(ui = ui, server = server)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.