Like functions, observers create their own variable scope. By default, variable writes are local to each function/observer.
If you want to make x available to both the observer and the renderPrint, you'd need to declare "x <- NULL" outside of the observer (but still inside the shiny server function) and then assign to it from inside the observer with the <<- operator, e.g. "x <<- 1".
But simple variables do not trigger reactivity--assigning to x will not, by default, cause renderPrint to re-run. You need to specifically tell Shiny to treat x as a reactive variable, by calling makeReactiveBinding.
I haven't tried this but it should work:
server <- function (input,output) {
x <- NULL
makeReactiveBinding("x")
observeEvent(input$button,{
x <<- 1
})
observe({
output$data <- renderPrint(x)
})
}
Further reading:
http://adv-r.had.co.nz/Functions.html#lexical-scopinghttp://shiny.rstudio.com/articles/scoping.html