【问题标题】:renderImage() and .svg in shiny app闪亮应用程序中的 renderImage() 和 .svg
【发布时间】:2016-12-29 20:17:51
【问题描述】:

我没有让renderImage()使用 .svg 文件。 我的最小示例(adapted from the corresponding RStudio-tutorial):

ui.R

shinyUI(pageWithSidebar(
  headerPanel("renderSVG example"),
  sidebarPanel(
    sliderInput("obs", "Number of observations:",
                min = 0, max = 1000,  value = 500),
    actionButton("savePlot", "savePlot")
  ),
  mainPanel(
    # Use imageOutput to place the image on the page
    imageOutput("plot"),
    imageOutput("plot_as_svg")
  )
))

服务器.R

require("ggplot2")
library(svglite)

shinyServer(function(input, output, session) {
  
  ## create plot with renderPlot() 
  

      output$plot<- renderPlot({
        hist(rnorm(input$obs), main="Generated in renderPlot()")
        })

  
  ## create .svg file of the plot and render it with renderImage() 
  
  output$plot_as_svg <- renderImage({
    # A temp file to save the output.
    # This file will be removed later by renderImage
    outfile <- tempfile(fileext='.svg')
    
    # Generate the svg
    svglite(outfile, width=400, height=300)
    hist(rnorm(input$obs), main="Generated in renderImage()")
    dev.off()
    
    # Return a list containing the filename
    list(src = outfile,
         contentType = 'text/svg+xml',
         width = 400,
         height = 300,
         alt = "This is alternate text")
  }, deleteFile = TRUE)
  
})

有什么想法吗?

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    我在同样的问题上苦苦挣扎,我发现网络上没有任何东西可以解决这个令人沮丧的问题。这是对我有用的解决方案:

    ui.R:

    shinyUI(pageWithSidebar(
      headerPanel("renderSVG example"),
      sidebarPanel(
        sliderInput("obs", "Number of observations:",
                    min = 0, max = 1000,  value = 500),
        actionButton("savePlot", "savePlot")
      ),
      mainPanel(
        # Use imageOutput to place the image on the page
        imageOutput("plot"),
        imageOutput("plot_as_svg")
      )
    ))
    

    server.R:

    require("ggplot2")
    
    shinyServer(function(input, output, session) {
    
      ## create plot with renderPlot()
    
    
          output$plot<- renderPlot({
            hist(rnorm(input$obs), main="Generated in renderPlot()")
            })
    
    
      ## create .svg file of the plot and render it with renderImage()
    
      output$plot_as_svg <- renderImage({
    
          width  <- session$clientData$output_plot_width
          height <- session$clientData$output_plot_height
          mysvgwidth <- width/96
          mysvgheight <- height/96
    
        # A temp file to save the output.
        # This file will be removed later by renderImage
    
        outfile <- tempfile(fileext='.svg')
    
        # Generate the svg
        svg(outfile, width=mysvgwidth, height=mysvgheight)
        hist(rnorm(input$obs), main="Generated in renderImage()")
        dev.off()
    
        # Return a list containing the filename
        list(src = normalizePath(outfile),
             contentType = 'image/svg+xml',
             width = width,
             height = height,
             alt = "My Histogram")
      })
    
    })
    

    请注意,我没有使用 svglite 包,只使用了 grDevices(基本)包中的 svg 设备。我还对源中的路径进行了规范化,因为我在 Windows 机器上(我相信这会将我的源从正斜杠更改为反斜杠,但也许有人会对此发表评论)。

    另外,我创建了四个新变量来容纳 svg 的宽度和高度以及图像的宽度和高度,以便仍然与页面保持流畅。我确信有比这更简单的解决方法,但这是我发现的有效方法。

    【讨论】:

    • 好的。所以在我看来,问题在于路径的规范化。下面是 ggplot 的简单解决方案。谢谢!
    【解决方案2】:

    受上述基本图形的 R_User123456789s 解决方案(此处)的启发,我使用 ggplot2 通过以下方式得到它

    ui.r

    shinyUI(pageWithSidebar(
      headerPanel("renderSVG example"),
      sidebarPanel(
        sliderInput("obs", "Number of observations:",
                    min = 0, max = 1000,  value = 500)
      ),
      mainPanel(
        # Use imageOutput to place the image on the page
        imageOutput("plot"),
        imageOutput("plot_as_svg")
      )
    ))
    

    server.r

    require("ggplot2")
    
    shinyServer(function(input, output, session) {
    
      ## create plot with renderPlot()
    
    
      output$plot<- renderPlot({
        hist(rnorm(input$obs), main="Generated in renderPlot() as png")
      })
    
    
      ## create .svg file of the plot and render it with renderImage()
    
      output$plot_as_svg <- renderImage({
    
        width  <- session$clientData$output_plot_width
        height <- session$clientData$output_plot_height
        mysvgwidth <- width/96
        mysvgheight <- height/96
    
        # A temp file to save the output.
        # This file will be removed later by renderImage
    
        outfile <- tempfile(fileext='.svg')
    
        # Generate the svg
        #to see actually what will be plotted and compare 
        qplot(clarity, data=diamonds, fill=cut, geom="bar")
        #save the plot in a variable image to be able to export to svg
        image=qplot(clarity, data=diamonds[1:input$obs,], fill=cut, geom="bar", main = "ggplot as svg")
        #This actually save the plot in a image
        ggsave(file=outfile, plot=image, width=mysvgwidth, height=mysvgheight)
    
        # Return a list containing the filename
        list(src = normalizePath(outfile),
             contentType = 'image/svg+xml',
             width = width,
             height = height,
             alt = "My svg Histogram")
      })
    
    })
    

    【讨论】:

    • 这对我不起作用。也许值得通过 GitHub 询问易辉的见解。
    猜你喜欢
    • 1970-01-01
    • 2017-09-10
    • 1970-01-01
    • 2016-05-23
    • 2013-07-08
    • 2016-05-27
    • 2022-01-18
    • 1970-01-01
    • 2016-06-02
    相关资源
    最近更新 更多