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

时间:2016-08-23 06:24:25

标签: r shiny

我没有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") ) )) 使用.svg文件。 我的最小例子(adapted from the corresponding RStudio-tutorial):

ui.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)

})

server.R

var uninvoiced = [];
function addCheckbox(el) {
  var value = el.value.split('|')[1];
  if (el.checked) {
    uninvoiced.push(value);
  } else {
    uninvoiced.splice(uninvoiced.indexOf(value), 1);
  }
  console.log(uninvoiced);
}

问题出在哪里?

2 个答案:

答案 0 :(得分:4)

我在同样的问题上苦苦挣扎,网上没有任何内容可以解决这个令人沮丧的问题。以下是适用于我的解决方案:

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

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

答案 1 :(得分: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")
  })

})