繁体   English   中英

复杂的R Shiny输入绑定问题与数据表

[英]Complex R Shiny input binding issue with datatable

我想做一些有点棘手的事情,我希望有人可以帮助我。

我想在数据表中添加selectInput 如果我启动应用程序,我会看到输入col_1col_2 ..已连接到数据表(您可以切换到a,b或c)

但是如果我更新数据集(从irismtcars ),输入和数据表之间的连接就会丢失。 现在,如果更改selectinput则日志不会显示修改。 我该如何保留链接?

我使用shiny.bindAll()shiny.unbindAll()做了一些测试但没有成功。

有任何想法吗?

请看看该应用程序:

library(shiny)
library(DT)
library(shinyjs)
library(purrr)

    ui <- fluidPage(
      selectInput("data","choose data",choices = c("iris","mtcars")),
      DT::DTOutput("tableau"),
      verbatimTextOutput("log")
    )

    server <- function(input, output, session) {
      dataset <- reactive({
        switch (input$data,
          "iris" = iris,
          "mtcars" = mtcars
        )
      })

      output$tableau <- DT::renderDT({
        col_names<-
          seq_along(dataset()) %>% 
        map(~selectInput(
          inputId = paste0("col_",.x),
          label = NULL, 
          choices = c("a","b","c"))) %>% 
          map(as.character)

        DT::datatable(dataset(),
                  options = list(ordering = FALSE, 
                          preDrawCallback = JS("function() {
                                               Shiny.unbindAll(this.api().table().node()); }"),
                         drawCallback = JS("function() { Shiny.bindAll(this.api().table().node());
                         }")
          ),
          colnames = col_names, 
          escape = FALSE         
        )

      })
      output$log <- renderPrint({
        lst <- reactiveValuesToList(input)
        lst[order(names(lst))]
      })

    }

    shinyApp(ui, server)

了解您的挑战:

为了确定您手头的挑战,您必须了解两件事。

  1. 如果数据表被刷新,它将被“删除”并从头开始构建(不是100%肯定在这里,我想我在某处阅读)。
  2. 请记住,您正在构建一个html页面。

selectInput()只是html代码的包装器。 如果在控制台中键入selectInput("a", "b", "c") ,它将返回:

<div class="form-group shiny-input-container">
  <label class="control-label" for="a">b</label>
  <div>
    <select id="a"><option value="c" selected>c</option></select>
    <script type="application/json" data-for="a" data-nonempty="">{}</script>
  </div>
</div>

请注意,您正在构建<select id="a"> ,一个id="a"的选择。 因此,如果我们假设1)在刷新后是正确的,则尝试构建另一个html元素: <select id="a">具有现有id。 这不应该工作: 如果多个不同的HTML元素是不同的元素,它们可以具有相同的ID吗? (假设我的假设1)成立;))

解决您的挑战:

乍一看很简单:只需确保您使用的ID在创建的html文档中是唯一的。

非常快速和肮脏的方式将取代:

inputId = paste0("col_",.x)

例如: inputId = paste0("col_", 1:nc, "-", sample(1:9999, nc))

但是之后很难用到你身上。

更长的方式:

所以你可以使用某种记忆

  1. 你已经使用过哪些ID。
  2. 您目前正在使用哪些ID。

您可以使用

  global <- reactiveValues(oldId = c(), currentId = c())

为了那个原因。

过滤掉旧的使用ID并提取当前ID的想法可能是这样的:

    lst <- reactiveValuesToList(input)
    lst <- lst[setdiff(names(lst), global$oldId)]
    inp <- grepl("col_", names(lst))
    names(lst)[inp] <- sapply(sapply(names(lst)[inp], strsplit, "-"), "[", 1)

可重复的示例如下:

library(shiny)
library(DT)
library(shinyjs)
library(purrr)

ui <- fluidPage(
  selectInput("data","choose data",choices = c("iris","mtcars")),
  dataTableOutput("tableau"),
  verbatimTextOutput("log")
)

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

  global <- reactiveValues(oldId = c(), currentId = c())

  dataset <- reactive({
    switch (input$data,
            "iris" = iris,
            "mtcars" = mtcars
    )
  })

  output$tableau <- renderDataTable({
    isolate({
      global$oldId <- c(global$oldId, global$currentId)
      nc <- ncol(dataset())
      global$currentId <- paste0("col_", 1:nc, "-", sample(setdiff(1:9999, global$oldId), nc))

      col_names <-
        seq_along(dataset()) %>% 
        map(~selectInput(
          inputId = global$currentId[.x],
          label = NULL, 
          choices = c("a","b","c"))) %>% 
        map(as.character)
    })    
    DT::datatable(dataset(),
                  options = list(ordering = FALSE, 
                                 preDrawCallback = JS("function() {
                                                      Shiny.unbindAll(this.api().table().node()); }"),
                                 drawCallback = JS("function() { Shiny.bindAll(this.api().table().node());
}")
          ),
          colnames = col_names, 
          escape = FALSE         
    )

})
  output$log <- renderPrint({
    lst <- reactiveValuesToList(input)
    lst <- lst[setdiff(names(lst), global$oldId)]
    inp <- grepl("col_", names(lst))
    names(lst)[inp] <- sapply(sapply(names(lst)[inp], strsplit, "-"), "[", 1)
    lst[order(names(lst))]
  })

}

shinyApp(ui, server)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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