3

downloadHandlerリアクティブ関数で作成されたプロットを、もう一度定義せずに呼び出すにはどうすればよいですか?

動かない例:

# Part of server.R

output$tgPlot <- renderPlot({
 plot1 <-ggplot(iris[iris$Species==input$species,])+geom_point(aes(Sepal.Length ,Sepal.Width))
 print(plot1)

 } ) 



  output$plotsave <- downloadHandler(
    filename = 'plot.pdf',
    content = function(file){
      pdf(file = file, width=12, height=4)
      tgPlot()
      dev.off()
    }
  )

で tgPlot() を呼び出せないのはなぜdownloadHandlerですか? 別の方法はありますか?

4

1 に答える 1

5

tgPlot()関数は別の場所で定義されていますか? 私はあなたがそれを定義するのを見たことがありません。

おそらく、両方の関数から参照する通常の (非反応性) 関数でプロット コードを定義する必要があります。

tgPlot <- function(inputSpecies){
 plot1 <-ggplot(iris[iris$Species==inputSpecies,])+geom_point(aes(Sepal.Length ,Sepal.Width))
 print(plot1)
}

output$tgPlot <- renderPlot({
    tgPlot(input$species)
}) 

output$plotsave <- downloadHandler(
  filename = 'plot.pdf',
  content = function(file){
    pdf(file = file, width=12, height=4)
    tgPlot(input$species)
    dev.off()
  }
)

これにより、プロットを生成できる関数が得られます。その後、この関数をリアクティブrenderPlotコンテキスト内で参照して、リアクティブ プロットを生成したり、PDF を生成したりできます。

于 2013-10-11T14:41:38.183 に答える