2015-10-16 16 views
0

如何在plot1中使用反應值並在plot2中使用X的對象。換句話說,我想獲得x的值並將其傳遞到plot1之外的另一個函數中。 在Server.R的代碼如下:如何獲取對象的反應值並將其傳遞給另一個閃亮的函數R

output$plot1<-renderPlot({ 
x<-reactiveValue(o="hello") 


)} 
outpu$plot2<-renderplot({ 
print(input$x$o) 

)} 

當我運行它,它不會顯示在控制檯RStudio什麼。

+0

如果您需要共享值「x」,您應該在'renderPlot()'之外定義它。那是你在說什麼?或者你也有一個名爲'x'的輸入?一個更完整的[可重現的例子](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)在這裏會很有幫助。 – MrFlick

回答

2

在服務器中定義renderPlot之外的反應值。此外,它不是input的一部分,因此請將其簡稱爲x$o

library(shiny) 

shinyApp(
    shinyUI(
     fluidPage(
      wellPanel(
       checkboxInput('p1', 'Trigger plot1'), 
       checkboxInput('p2', 'Trigger plot2') 
      ), 
      plotOutput('plot2'), 
      plotOutput('plot1') 
     ) 
    ), 
    shinyServer(function(input, output){ 
     x <- reactiveValues(o = 'havent done anything yet', color=1) 

     output$plot1 <- renderPlot({ 
      ## Add dependency on 'p1' 
      if (input$p1) 
       x$o <- 'did something' 
     }) 

     output$plot2<-renderPlot({ 
      input$p2 
      print(x$o) # it is not in 'input' 

      ## Change text color when plot2 is triggered 
      x$color <- isolate((x$color + 1)%%length(palette()) + 1) 

      plot(1, type="n") 
      text(1, label=x$o, col=x$color) 
     }) 
    }) 
) 
+0

非常感謝,是的,你是對的我必須在Server.R的開頭定義reactiveValue。有用。 – user

相關問題