Dan_yef Dan_yef - 15 days ago 6
R Question

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.

Answer

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