user
user

Reputation: 592

how do I get reactive value of an object and pass it to another function in shiny R

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.

Upvotes: 0

Views: 862

Answers (1)

Rorschach
Rorschach

Reputation: 32416

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)
        })
    })
)

Upvotes: 2

Related Questions