首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >renderImage()和.svg在闪亮的应用程序中

renderImage()和.svg在闪亮的应用程序中
EN

Stack Overflow用户
提问于 2016-08-23 06:24:25
回答 2查看 5.6K关注 0票数 6

我不让renderImage()处理.svg文件。我的最小示例(改编自相应的RStudio教程):

ui.R

代码语言:javascript
复制
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

代码语言:javascript
复制
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)
  
})

有什么问题吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-09-01 05:28:06

在R_User123456789s解决方案(这里)的启发下,我在下面的ggplot2中获得了基本图形的解决方案

ui.r

代码语言:javascript
复制
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

代码语言:javascript
复制
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")
  })

})
票数 4
EN

Stack Overflow用户

发布于 2016-08-30 16:09:30

我为同样的问题而奋斗,在网络上我发现没有任何东西能解决这个令人沮丧的问题。以下是对我有效的解决方案:

ui.R:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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 (base)包中的svg设备。我还规范了源代码中的路径,因为我在Windows机器上(我相信这会将我的源从正斜杠改为反斜杠,但可能有人会对此发表评论)。

此外,我还创建了四个新变量来容纳svg宽度和高度以及图像宽度和高度,使页面仍然保持流畅性。我相信有一个比这更简单的解决办法,但这就是我发现的效果。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39093777

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档