1

Is there any possibility to inialize a reactive in shiny? I want to a have a reactive data.table. Part of this data.table does not change, so I would not need to reinitialize it every time input changes, but would only need to update columns "original_values" and "working_values".

Is this possible? Or is this a case for better using reactiveValues() with observe(), even though I calculate values here and do not have sideeffects?

library(shiny)
library(data.table)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      sliderInput("slider1", "Slider 1",min = 0.1, max = 1, value = 0.4, step = 0.05),
      sliderInput("slider2", "Slider 2",min = 0.1, max = 1, value = 0.4, step = 0.05),                               
      sliderInput("slider3", "Slider 3",min = 100, max = 20000, value = 5000, step= 200)
    ), 
    mainPanel(
      tableOutput("tableOut")

    )

  ))

  server <- function(input, output, session){
    rv_dtWeights <- reactive({

      #initalization - can this be put somewhere else?
      ret <- data.table(slider_names= c("slider1","slider2", "slider3"), 
                        some_grouping_information=c("A", "A", "B") )

      #actual reactive
      ret[,original_values :=c(input$slider1,input$slider2,input$slider3)]
      ret[,working_values:=sum(original_values), by=some_grouping_information]

    })

    output$tableOut<- renderTable(rv_dtWeights())

  }
  shinyApp(ui = ui, server=server)
Julian
  • 741
  • 8
  • 19
  • 1
    Possible duplicate of [Are there global variables in R Shiny?](https://stackoverflow.com/questions/20333399/are-there-global-variables-in-r-shiny) – Julien Navarre Feb 22 '18 at 14:41
  • @JulienNavarre: No, it is not. The similarity is the answer: I can use a data.table as a global variable, and can extend it with a reactive. But that is only the answer. – Julian Feb 22 '18 at 14:58
  • Just in case someone comes here wondering why reactivity does not work with data.table (As I did when posting this question): See https://github.com/rstudio/shiny/issues/1017 or https://stackoverflow.com/questions/32536940/shiny-reactivity-fails-with-data-tables – Julian Nov 27 '18 at 14:17
  • The := in the data.table package is out-dated and can be very confusing to R beginners when it is presented together with the complexity of Shiny. – SJ9 Feb 27 '20 at 02:04

1 Answers1

4

You can just put it outside of the reactive, and in the server (or outside of the server, for some more info on the differences, see here). An example is given below, where the data.table is only initialized once when the user opens the app.

Hope this helps!

library(shiny)
library(data.table)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      sliderInput("slider1", "Slider 1",min = 0.1, max = 1, value = 0.4, step = 0.05),
      sliderInput("slider2", "Slider 2",min = 0.1, max = 1, value = 0.4, step = 0.05),                               
      sliderInput("slider3", "Slider 3",min = 100, max = 20000, value = 5000, step= 200)
    ), 
    mainPanel(
      tableOutput("tableOut")

    )

  ))

server <- function(input, output, session){

  #initalization - can this be put somewhere else?
  ret <- data.table(slider_names= c("slider1","slider2", "slider3"), 
                    some_grouping_information=c("A", "A", "B") )


  rv_dtWeights <- reactive({
    #actual reactive
    ret[,original_values :=c(input$slider1,input$slider2,input$slider3)]
    ret[,working_values:=sum(original_values), by=some_grouping_information]

  })

  output$tableOut<- renderTable(rv_dtWeights())

}
shinyApp(ui = ui, server=server)
Florian
  • 24,425
  • 4
  • 49
  • 80