0

I would like to use an eventReactive-function in a shiny module. However that does not work as expected. What is wrong with my code or what do I have to add?

I have already tried observers but I want to use eventReactive because I need the return-value.

mod_test_UI <- function(id) {

  ns <- NS(id)

  actionButton(ns("test"), "Test")

}


mod_test <- function(input, output, session) {

  ns <- session$ns

  observe({
    print(input$test)
  })

  result<- eventReactive(input$test, {
    print("ABC")
  })

}


ui <- tagList(
  mod_test_UI("test-mod")
)

server <- function(input, output, session) {
  callModule(mod_test, "test-mod") 
}

# app
shinyApp(ui = ui, server = server)

Jensxy
  • 119
  • 1
  • 14
  • Please give some more information as to what you want to achieve. Do you want to print to screen in which case you should use renderText and textOuptut. – Eli Berkow Jul 30 '19 at 07:59
  • I like to return a data frame which is created when the Test-button is clicked. The print in the eventReactive function should only show that the button does not work. – Jensxy Jul 30 '19 at 08:33
  • See below answer – Eli Berkow Jul 30 '19 at 08:48

1 Answers1

2

You need to return a value within eventReactive as below:

mod_test_UI <- function(id) {

    ns <- NS(id)

    actionButton(ns("test"), "Test")

}


mod_test <- function(input, output, session) {

    ns <- session$ns

    observe({
        print(input$test)
    })

    result<- eventReactive(input$test, {
        return("ABC")
    })

    observe({
        print(result())
    })
}


ui <- tagList(
    mod_test_UI("test-mod")
)

server <- function(input, output, session) {
    callModule(mod_test, "test-mod") 
}

# app
shinyApp(ui = ui, server = server)

The second observe just prints the value now contained in result() to the screen to prove that it works.

The return() in this case is not necessary and it could just be "ABC" as below:

result<- eventReactive(input$test, {
    "ABC"
})
Eli Berkow
  • 2,628
  • 1
  • 12
  • 22