Mal_a
Mal_a

Reputation: 3760

R Mechanism behind input$table_rows_all from DT

i have a question about input$table_rows_all from DT package. I have actually noticed that while using it my code/table is rendered more then one time(?). At first the table with 0 rows and then accordingly to filtered rows --> my actual App is very complex, therefore i loose on time during this operation.

Here is an easy example of what i mean:

library(shiny)
library(DT)

ui <- bootstrapPage(
dataTableOutput("table1"),
dataTableOutput("table2"))

server <- function(input, output) {

output$table1 <- renderDataTable({
datatable(iris,filter="top")})

output$table2 <- renderDataTable({
data <- iris[input$table1_rows_all,]
str(data)
datatable(data)})
}
shinyApp(ui = ui, server = server)

After running this code we can see in the console this output:

'data.frame':   0 obs. of  5 variables:
 $ Sepal.Length: num 
 $ Sepal.Width : num 
 $ Petal.Length: num 
 $ Petal.Width : num 
 $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 
'data.frame':   0 obs. of  5 variables:
 $ Sepal.Length: num 
 $ Sepal.Width : num 
 $ Petal.Length: num 
 $ Petal.Width : num 
 $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 
'data.frame':   150 obs. of  5 variables:
 $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
 $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
 $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
 $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
 $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...

I have tried to look for any documentation but could not find anything. Any explanation would be helpfull!

Upvotes: 1

Views: 438

Answers (2)

pieca
pieca

Reputation: 2563

While @thothal proposes a solution, here I'd like to answer the question directly. When you initialize your app, DataTables are just empty <divs> which are filled with content only when user opens the app. To verify this behavior, run your example, open it in browser and then (in firefox) right-click -> View Page Source. For explanation, see here.

Let's see this in your app -- I have added some 'debug prints' and a js alert that will help:

library(shiny)
library(DT)

ui <- bootstrapPage(
  shiny::tags$script(
    "$(document).on('shiny:sessioninitialized', function(event) {
      alert('Connected to the server');
    });"
  ),
  dataTableOutput("table1"),
  dataTableOutput("table2"))

server <- function(input, output) {

  #  this ignores input$table1_rows_all being NULL and runs only when there is a value
  observeEvent(
    input$table1_rows_all,
    {
      print("observeEvent called")
      print(input$table1_rows_all)
    }
  )

  output$table1 <- renderDataTable({
    print("renderDataTable -- table 1 called")
    datatable(iris,filter="top")
  })

  output$table2 <- renderDataTable({
    print("renderDataTable -- table 2 called")
    if (is.null(input$table1_rows_all)) {
      print("input$table1_rows_all is NULL")
    }
    data <- iris[input$table1_rows_all,]
    datatable(data)
  })
}

shinyApp(ui = ui, server = server, options = list(launch.browser = FALSE))

Run the app and open it in browser. You see that session is initialized and each renderDataTable has run once. As mentioned earlier, they are just empty placeholders by now and variable input$table1_rows_all just does not exist at this point -- that's why you have 0 rows.

[1] "renderDataTable -- table 1 called"
[1] "renderDataTable -- table 2 called"
[1] "input$table1_rows_all is NULL"

Click ok to proceed. The following lines are printed subsequently:

[1] "renderDataTable -- table 2 called"
[1] "input$table1_rows_all is NULL"
[1] "observeEvent called"
  [1]   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28
 [29]  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56
 [57]  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84
 [85]  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111 112
[113] 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
[141] 141 142 143 144 145 146 147 148 149 150
[1] "renderDataTable -- table 2 called"

My understanding of what is going on here is the following:

1) for some reason renderDataTable for table2 function is called. However, input$table1_rows_all is still NULL.

2) table1 actually gets rendered (i.e., rows are loaded) so input$table1_rows_all becomes available. This results in print from observeEvent and another call from renderDataTable for table2.

Upvotes: 1

thothal
thothal

Reputation: 20399

A simple req prevents this behaviour:

library(shiny)
library(DT)

ui <- bootstrapPage(
  dataTableOutput("table1"),
  dataTableOutput("table2"))

server <- function(input, output) {

  output$table1 <- renderDataTable({
    datatable(iris,filter="top")
  })

  output$table2 <- renderDataTable({
    req(input$table1_rows_all) ## run the following code only if table1_rows_all is truthy
    data <- iris[input$table1_rows_all,]
    str(data)
    datatable(data)
  })
}
shinyApp(ui = ui, server = server)

# 'data.frame':   150 obs. of  5 variables:
#  $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
#  $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
#  $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
#  $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
#  $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...

My understanding of reactives is that whenever something changes the oberserver/render* functions are invoked. I assume this is also the case at the startup.

For observeEvent you have even a parameter ignoreInit which prevents the observer to fire on init.

Upvotes: 1

Related Questions