首页 > 解决方案 > 图像在 Rshiny 中不显示

问题描述

示例图片:https ://ibb.co/Bt6v6W9

输出的样子: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

)

标签: imageshinyrstudiopng

解决方案


您应该在服务器端抛出创建图像的代码,将其封装在您命名的 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

)

...并显示:

在此处输入图像描述


推荐阅读