我正在嘗試從 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)
'''
任何幫助將不勝感激。謝謝你,比爾。
uj5u.com熱心網友回復:
您收到錯誤訊息的原因是因為renderText回傳的是反應函式而不是影像 HTML 標記。reactive不應出現在任何render...函式中。
正如@MrFlick 所提到的,renderText只會向用戶界面回傳一個字串。renderUI和的替代方案uiOutput是renderImage和imageOutput。這些將以一種方便的方式將上傳的影像添加到 UI,因為渲染功能只需要一個屬性串列來給出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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/461135.html
上一篇:使用OutlinedTextBox樣式時如何更改標簽背景?
下一篇:如何制作技能共享中的線路?
