Hi,
There are several ways to do this in Shiny. The one closest to your structure would be this:
library(shiny)
library(ggplot2)
ui <- fluidPage(
sidebarPanel(
actionButton("myButton", "click")
),
mainPanel(
plotOutput("myPlot"),
tableOutput("myTable")
)
)
server <- function(input, output, session) {
myPlot = reactiveVal()
myData = reactive({
input$myButton
data = data.frame(x = 1:10, y = runif(10))
myPlot(ggplot(data, aes(x = x, y = y)) + geom_point())
data
})
output$myPlot = renderPlot({
myPlot()
})
output$myTable = renderTable({
myData()
})
}
shinyApp(ui, server)
But you can also do this:
library(shiny)
library(ggplot2)
ui <- fluidPage(
sidebarPanel(
actionButton("myButton", "click")
),
mainPanel(
plotOutput("myPlot"),
tableOutput("myTable")
)
)
server <- function(input, output, session) {
myPlot = reactiveVal()
myData = reactiveVal()
observeEvent(input$myButton, {
data = data.frame(x = 1:10, y = runif(10))
myData(data)
myPlot(ggplot(data, aes(x = x, y = y)) + geom_point())
}, ignoreNULL = F)
output$myPlot = renderPlot({
myPlot()
})
output$myTable = renderTable({
myData()
})
}
shinyApp(ui, server)
It all depends on what triggers the code (I choose a button) and what the structure of your reactive environment is.
Hope this helps,
PJ