2015-10-16 15 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

相关问题