ashleych
ashleych

Reputation: 1054

Reactive values - what am I doing wrong

The app is intended to display summarized_mod when the action button is clicked. But I keep getting a summarized_mod missing error.

summarized <- data.frame(id = 1:20, group = letters[1:4], TY_COMP = runif(20), LY_COMP = runif(20))

    library(shiny)

    ui <- fluidPage(
      verbatimTextOutput("text"),
      actionButton("btn", "Show the summarized")
    )

    server <- function(input, output){
              summarized <- reactive({summarized})
                observeEvent(input$btn,{ 
        summarized_mod <-summarized()$TY_COMP / summarized()$LY_COMP-1 } 
               })

      output$text <- renderPrint(summarized_mod())
    }

    shinyApp(ui, server)

Upvotes: 0

Views: 32

Answers (1)

St&#233;phane Laurent
St&#233;phane Laurent

Reputation: 84529

dat <- data.frame(id = 1:20, 
                  group = letters[1:4], 
                  TY_COMP = runif(20), 
                  LY_COMP = runif(20))

library(shiny)

ui <- fluidPage(
  verbatimTextOutput("text"),
  actionButton("btn", "Show the summarized")
)

server <- function(input, output){
  # summarized <- reactive({summarized}) useless !
  summarized_mod <- eventReactive(input$btn, {
    dat$TY_COMP / dat$LY_COMP-1
  })
  output$text <- renderPrint(summarized_mod())
}

shinyApp(ui, server)

Upvotes: 1

Related Questions