0

How can I use a reactivevalue in the plot1 and use object of X in plot2. In the other words I want to get the value of x and pass it into another function outside of plot1. The code in the Server.R is as following:

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


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

)}

when I run it it does not show anything in the RStudio console.

user
  • 592
  • 6
  • 26
  • If you need the value `x` to be shared, you should define it outside the `renderPlot()`. Is that what you are talking about? Or do you have an input named `x` as well? A more complete [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) would be helpful here. – MrFlick Oct 16 '15 at 22:53

1 Answers1

2

Define the reactive value outside of renderPlot in the server. Also, it is not part of input, so refer to it as simply 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)
        })
    })
)
Rorschach
  • 31,301
  • 5
  • 78
  • 129
  • Thank you so much, Yes you are right I have to define reactiveValue in the beginning of Server.R. It works. – user Oct 16 '15 at 23:36