Daniil Yefimov
Daniil Yefimov

Reputation: 1012

Is it possible to use reactive function into another reactive function in r shiny?

For example:

        data_cbs <- reactive({ 
        "code"
        })

        model <- reactive({
                data <- data_cbs()
                + "code"    
        })

Is it possible to use the following structure in R shiny?

Maybe it is important to know, that data_cbs() and model consists of 3-4 "else-if" statements.

Upvotes: 0

Views: 728

Answers (1)

Willem van Doesburg
Willem van Doesburg

Reputation: 534

Here's a single-script example to show that this indeed works and to play around with:

# Global variables can go here
n <- 200


# Define the UI
ui <- bootstrapPage(
  checkboxInput('random', 'randomize'),
  plotOutput('plot')
)


# Define the server code
server <- function(input, output) {

  checkRandom <- reactive({
    if( input$random ){
      data <- runif(n)
    }else {
      data <- seq(1, n)
    }
    return(data)
  })

  output$plot <- renderPlot({
    plot(checkRandom())
  })
}

# Return a Shiny app object
shinyApp(ui = ui, server = server)

Upvotes: 2

Related Questions