image - 图像在 Rshiny 中不显示
问题描述
- 图像在 R 闪亮中无法正确显示(空图像显示),内部作为外部(app.R)运行
过去在本网站上发布的类似问题没有提供足够的答案:通过 app.R 运行应用程序的解决方案对我来说没有解决问题,图像仍然不显示
在代码中:图像被添加到主面板,两者都尝试从文档和网络(将图像转换为网络链接)。
- 我尝试通过将图像转换为 Web 链接来通过文档(本地工作目录)包含图像。
输出的样子:https ://cdn1.imggmi.com/uploads/2019/4/24/d65ae8a21decc6adb1d14db9a3e9bf75-full.png
任何人的解决方案的想法?如前所述,通过 app.R 运行应用程序不起作用,输出保持不变。
library(shiny)
library(png)
# See above for the definitions of ui and server
library(shiny)
library(png)
# Define UI for app that draws a histogram ----
ui <- fluidPage(
# App title ----
titlePanel("Hello Shiny!"),
# Sidebar layout with input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
# Input: Slider for the number of bins ----
sliderInput(inputId = "bins",
label = "Number of bins:",
min = 1,
max = 50,
value = 30)
),
# Main panel for displaying outputs ----
mainPanel(
# Output: Histogram ----
plotOutput(outputId = "distPlot"),
img(src='DataVIS1.png', align = "right",height=168,width=70)
##output: png image
)
)
)
server <- function(input, output) {
# Histogram of the Old Faithful Geyser Data ----
# with requested number of bins
# This expression that generates a histogram is wrapped in a call
# to renderPlot to indicate that:
#
# 1. It is "reactive" and therefore should be automatically
# re-executed when inputs (input$bins) change
# 2. Its output type is a plot
output$distPlot <- renderPlot({
x <- faithful$waiting
bins <- seq(min(x), max(x), length.out = input$bins + 1)
hist(x, breaks = bins, col = "#75AADB", border = "white",
xlab = "Waiting time to next eruption (in mins)",
main = "Histogram of waiting times")
})
}
shinyApp(ui = ui, server = server
)
解决方案
您应该在服务器端抛出创建图像的代码,将其封装在您命名的 renderPlot({}) 函数中,然后在“distPlot”plotOutput 之后绘制输出。我无法让你的 img(src=...) 代码工作,所以我使用了一个功能相同的光栅图。一个缩小的例子如下:
library(shiny)
library(png)
ui <- fluidPage(
mainPanel(
plotOutput(outputId = "png")
)
)
server <- function(input, output) {
output$png <- renderPlot({
pic = readPNG('path/to/image.png')
plot.new()
grid::grid.raster(pic)
})
}
shinyApp(ui = ui, server = server)
将其放入您的代码中会产生:
library(shiny)
library(png)
# See above for the definitions of ui and server
library(shiny)
library(png)
# Define UI for app that draws a histogram ----
ui <- fluidPage(
# App title ----
titlePanel("Hello Shiny!"),
# Sidebar layout with input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
# Input: Slider for the number of bins ----
sliderInput(inputId = "bins",
label = "Number of bins:",
min = 1,
max = 50,
value = 30)
),
# Main panel for displaying outputs ----
mainPanel(
# Output: Histogram ----
plotOutput(outputId = "distPlot"),
###Changed code here
plotOutput(outputId = "png")
##output: png image
)
)
)
server <- function(input, output) {
# Histogram of the Old Faithful Geyser Data ----
# with requested number of bins
# This expression that generates a histogram is wrapped in a call
# to renderPlot to indicate that:
#
# 1. It is "reactive" and therefore should be automatically
# re-executed when inputs (input$bins) change
# 2. Its output type is a plot
output$distPlot <- renderPlot({
x <- faithful$waiting
bins <- seq(min(x), max(x), length.out = input$bins + 1)
hist(x, breaks = bins, col = "#75AADB", border = "white",
xlab = "Waiting time to next eruption (in mins)",
main = "Histogram of waiting times")
})
###New function
output$png <- renderPlot({
pic = readPNG('path/to/image.png')
plot.new()
grid::grid.raster(pic)
})
}
shinyApp(ui = ui, server = server
)
...并显示:
推荐阅读
- ios - 如何在 AVAudioRecorder 中控制 Opus 比特率
- openlayers - Here Satellite Map WMS 或 WMTS 服务
- c++ - 为什么 Qt SIGNAL(clicked(bool) 与真正的鼠标点击行为不同?
- c - 在 QEMU 中调试早期内核启动代码
- .net - Elasticserach NEST 客户端不会在搜索结果中绑定 _score 或 _id
- flutter - 如何为 Flutter 应用创建纯 Dart 包?
- c++14 - 动态编程问题中的错误。“运行时错误:退出代码为 -1073741571”是什么意思?
- html - BEM html 命名约定
- opencv - 使用 OpenCv 重叠图像的问题
- node.js - 在 Node 库中添加类型引用