Basic shiny in R markdown question

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)

Hi @romanb333, what I've done is operate the filter outside of the "render" outputs. This is done in a reactive function:

server <- function(input, output){

  op1 <-  reactive({
    mtcars %>% filter(cyl == input$cyl)
  })

  output$data <- renderTable({
     op1()
  })
  
  
  output$data2 <- renderTable({
    op1()  
      %>%  summarise(mean_wt = mean(wt))
  })

Please notice that op1() becomes a function, so you have to call it as a function (using parenthesis), that was the biggest leap for me yesterday when I was trying to figure out how to accomplish this as well!

Edgar - this is perfect, thank you so much!!

Curious about a few more things:

--is it possible to 'freeze panes' like in excel? i.e. make it so that you can still see column headers when you scroll down a table?

--is it possible to disable the vertical scrollbar that renders with a table?

--how can I upload a reprex and allow the user to load the data I have if it's from a source outside of R? i.e. if I have a .csv stored on my local machine, R loads it from my working directory, but how can someone on this site access that file?

Thanks again for all your help.

Hi @romanb333, you're welcome!

There are several options for placing a table in a Shiny app, for what you're describing, I would encourage you to take a look at DataTables: http://rstudio.github.io/DT/ , I think this may cover your second question, please let me know if it doesn't.

For the file question, I think what you're asking about is how Shiny handles a file dependency that it's outside of the app. Where are you planning to publish to? shinyapps.io?

Hi @edgararuiz - thanks, I'll take a look at the datatable options. Re the file question, it wasn't necessarily a shiny specific question, just more related to how can I make sure that someone can reproduce my environment with all the objects I've created etc... if the data source isn't on their local machine? I know how to provide a reprex, but I'm a bit confused as to how someone would be able to run that and reproduce my environment if they don't have access to the .csv I loaded the data from, for example i.e. the file lives in my working directory so the code works on my machine, but the reference to that file in my code would error out on someone else's right? Hope that's a bit clearer, thanks again.

I see, check out these articles and let me know if they answer your question: http://shiny.rstudio.com/articles/deployment-local.html, http://shiny.rstudio.com/articles/#deployment

Yeah, this is what I was looking for. Thanks again!