简体   繁体   中英

reactive + serverside Shiny datatable with extensions?

Update:

I'm having trouble combining server-side datatables and a reactive dataframe

If I run this code with server = FALSE , the table is rendered, with an alert

在此处输入图片说明

If I run it with server = TRUE , I get "$ operator is invalid for atomic vectors" where the table should be.

library(shiny)
library(DT)

input4details <- expand.grid(bmo.spec = c("dog", "cat", "bird"),
                       ic50 = seq(from = 0.0, to = 10.0, by = 0.5),
                       stringsAsFactors = F)

bsSelections   <- c("<ALL>", sort(unique(input4details$bmo.spec)))

ui <- fluidPage(
  tags$head(
    tags$style(type="text/css", ".dataTables_filter {display: none;    }"
    )),

  titlePanel('ServerSideExtensions'),

  sidebarPanel(
    selectInput(inputId = "bmospec", label = "Choose the BMO species:",
      choices = bsSelections),
    sliderInput("maxIC50", "Maximum IC50 (uM):",
                min = 0.01, max = 10.00, step = 0.01, value = 10.00)
  ),

   mainPanel(
    tabsetPanel(
      tabPanel("Tab2",  DT::dataTableOutput("tbl2"))
    )
  )
)

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

  dataSel4details <- reactive({
    if(input$bmospec != '<ALL>') {
      bsFilter <- input4details$bmo.spec == input$bmospec
    } else {
      bsFilter <- rep(TRUE, length(input4details$bmo.spec))
    }
    input4details[ input4details$ic50 < input$maxIC50 & bsFilter ,  ]
  })

  output$tbl2 <- DT::renderDataTable({
    action1 <- dataTableAjax(session, dataSel4details())
    datatable(
    dataSel4details(),
    server = TRUE,
    extensions = c(
      'TableTools',
      'ColVis',
      'Scroller'
    ),
    options = list(
      ajax = list(url = action1),
      searching = FALSE,
      dom = 'CT<"clear">lrtip',
      tableTools = list(
        sSwfPath = copySWF('www'),
        aButtons = c('csv', 'copy')
      ),
      deferRender = TRUE,
      scrollY = 500,
      scrollCollapse = TRUE,
      lengthMenu = list(c(5, 10, 15), c('5', '10', '15')),
      pageLength = 10
    ),
    rownames=FALSE
  ) %>%
    formatRound(c('ic50'), 4)
  })
}

shinyApp(ui = ui, server = server)

Original:

I have made some (functional) shiny interfaces in which a reactive data object was used as the data argument inside of renderDataTable.

dataSel4counts <- reactive({
  if(input$targspec != '<ALL>') {
    tsFilter <- lendata$targ.spec == input$targspec
  } else {
    tsFilter <- rep(TRUE, length(lendata$targ.spec))
  }
  lendata[ lendata$adjp <= input$maxp & tsFilter ,  ]
})

output$countTable = renderDataTable({
  data <- dataSel4counts()
  datatable(
    data
  )
})

Recently I added several of the extensions documented on https://rstudio.github.io/DT/extensions.html

I get the sense that (by default) client-side processing is a consequence of adding DT extensions.

I would like to switch back to server side processing, following http://rstudio.github.io/DT/server.html

But it seems like the data arguments (iris in the example) can't be reactive. When I try to do something like

action = dataTableAjax(session, dataSel4counts())
widget = datatable(dataSel4counts(), server = TRUE, options = list(
  ajax = list(url = action)
))

I get a variety of errors, including

Warning in run(timeoutMs) : You are using the server-side processing mode, but did not specify the 'ajax' option. See http://rstudio.github.io/DT/server.html

Error in .getReactiveEnvironment()$currentContext() : Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)

When the data argument of DT::datatable() comes from a reactive expression, the datatable() must be put inside renderDataTable() .

renderDataTable({
  action = dataTableAjax(session, dataSel4counts())
  datatable(dataSel4counts(), server = TRUE, options = list(
    ajax = list(url = action)
  ))
})

That will fix the error. Regarding the warning message, I have no idea before you post a minimal reproducible example (along with your library(DT); library(shiny); sesssionInfo() ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM