2
votes

I have this question: In a Shiny App, I construct a varible with a reactive(). The thing is that, in the midle of this process (that is a long one) I construct other varibles that I need too. For example:

#---------------UI------------------
ui <- navbarPage(
  title = "example",
  tabPanel('panel',
           tableOutput("my_table"),
           tableOutput("colum_names"))
) 

#---------------SERVER------------------
server <- function(input, output) {
  a <- reactive({
    df_1 <- data.frame("fc"=c(1,2,3), "sc"=c(1,2,3), "tc"=c(1,2,3) )
    df_2 <- subset(df_1,select=-c(fc))
    column_names <- colnames(df_2)
    df_3 <- df_2*2
    df_3
  })
  output$my_table = renderTable({
    a()
  })
  output$colum_names = renderTable({
    df_column_names = data.frame(column_names())
    df_column_names
  })
}

#---------------APP------------------
shinyApp(ui = ui, server = server)

In this (very short) example, I would need the variable "a" (of course) and the variable "column_names". I can do something like create a new reactive that reproduce all the process until the line that contain "column_names" and finish it there. But the process is too long and I prefer to do it more "eficiently".

Any idea??

Thank you so much!

2

2 Answers

0
votes

The process you're describing is correct : instead of assigning variables, just assign reactives and Shiny will handle the depedencies between them.

Note that in the example you provided, reactives aren't needed because the content is up to now static.

library(shiny)
#---------------UI------------------
ui <- navbarPage(
  title = "example",
  tabPanel('panel',
           tableOutput("my_table"),
           tableOutput("column_names"))
) 

#---------------SERVER------------------
server <- function(input, output) {
  
  df_1 <- data.frame("fc"=c(1,2,3), "sc"=c(1,2,3), "tc"=c(1,2,3) )
  
  a <- reactive({subset(df_1,select=-c(fc))})
  
  column_names <- reactive({colnames(a())})

  output$my_table = renderTable({a()})
  
  output$column_names = renderTable({column_names()})
}

#---------------APP------------------
shinyApp(ui = ui, server = server)
0
votes

I found a interesting answer to my own question: if you want to do something like that, you can use "<<-" instead of "<-" and it save the variable when you are working insede a function (like reactive()). Let´s see:

#---------------UI------------------
ui <- navbarPage(
  title = "example",
  tabPanel('panel',
           tableOutput("my_table"),
           tableOutput("colum_names"))
) 

#---------------SERVER------------------
server <- function(input, output) {
  a <- reactive({
    df_1 <- data.frame("fc"=c(1,2,3), "sc"=c(1,2,3), "tc"=c(1,2,3) )
    df_2 <- subset(df_1,select=-c(fc))
    column_names <- colnames(df_2)
    # HERE THE SOLUTION!!
    column_names_saved <<- column_names
    df_3 <- df_2*2
    df_3
  })
  output$my_table = renderTable({
    a()
  })
  output$colum_names = renderTable({
    df_column_names = data.frame(column_names_saved)
    df_column_names
  })
}

#---------------APP------------------
shinyApp(ui = ui, server = server)

Then, into the funtion you must continues with the variable "column_names", but when you need to use it later, you can use "column_name_saved". (just be carefull with one thing: onece you save the variable into the funtion, you canot change it)

Thanks!!!