Trying to do just some very basic things w/ shiny in R markdown - my question is, can I create an object in one output section and reference it in another output section to avoid re-writing code? I tried doing something like the below, but got the error: 'object 'op1' not found' which I take to mean that objects created in one output block can't be referenced in another?
--Tried this in the server section and didn't work
server <- function(input, output){
output$data <- renderTable({
op1 <- mtcars %>% filter(cyl == input$cyl)
})
output$data2 <- renderTable({
op1 %>% summarise(mean_wt = mean(wt))
})
--Actual code
library(shiny)
#> Warning: package 'shiny' was built under R version 3.3.3
library(dplyr)
#> Warning: package 'dplyr' was built under R version 3.3.3
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
mtcars <- mtcars
ui <- fluidPage(
selectInput(inputId = "cyl",
label = "Select Cyl",
choices = unique(mtcars$cyl)),
tableOutput("data"),
tableOutput("data2")
)
server <- function(input, output){
output$data <- renderTable({
mtcars %>% filter(cyl == input$cyl)
})
output$data2 <- renderTable({
mtcars %>% filter(cyl == input$cyl) %>% summarise(mean_wt = mean(wt))
})
}
shinyApp(ui = ui, server = server)