Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript datatable存在复杂的R输入绑定问题_Javascript_R_Datatables_Shiny - Fatal编程技术网

Javascript datatable存在复杂的R输入绑定问题

Javascript datatable存在复杂的R输入绑定问题,javascript,r,datatables,shiny,Javascript,R,Datatables,Shiny,我正在尝试做一些有点棘手的事情,我希望有人能帮助我 我想在数据表中添加selectInput。 如果启动应用程序,我会看到输入col_1,col_2。。与数据表连接良好(可以切换到a、b或c) 但是 如果我更新数据集(从iris到mtcars),则输入和数据表之间的连接将丢失。现在,如果更改选择input,则日志不会显示修改。我如何保持链接 我使用shinny.bindAll()和shinny.unbindAll()进行了一些测试,但没有成功 有什么想法吗 请查看应用程序: library(sh

我正在尝试做一些有点棘手的事情,我希望有人能帮助我

我想在数据表中添加
selectInput
。 如果启动应用程序,我会看到输入
col_1
col_2
。。与数据表连接良好(可以切换到a、b或c)

但是 如果我更新数据集(从
iris
mtcars
),则输入和数据表之间的连接将丢失。现在,如果更改
选择input
,则日志不会显示修改。我如何保持链接

我使用
shinny.bindAll()
shinny.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)
库(闪亮)
图书馆(DT)
图书馆(shinyjs)
图书馆(purrr)

ui了解您的挑战:

为了确定你面临的挑战,你必须知道两件事

  • 如果数据表被刷新,它将被“删除”并从中生成 刮擦(这里不是100%确定,我想我在什么地方读过)
  • 请记住,您实际上是在构建一个html页面
  • selectInput()
    只是html代码的包装。如果在控制台中键入
    选择输入(“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>
    
    使用类似于:
    inputId=paste0(“col_”,1:nc,“-”,sample(1:9999,nc))

    但这对你以后来说是很难使用的

    更长的路:

    所以你可以使用一些记忆

  • 您已经使用了哪些ID
  • 哪些是您当前使用的ID
  • 你可以用

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

    global好的,最后一部分我可以再解释一点,但它的睡眠时间从一个小时开始;)如果需要的话,让我在接下来的几天回来补充一些解释……谢谢你的建议,以及这些非常清晰的解释。事实上,我已经在创建输入的名称上使用了
    rnorm
    技巧。这是一个能很快找到极限的把戏。例如,您入侵了日志系统以模拟正确的行为。保留这些技巧会迫使我对我试图构建的所有内容进行大量的绕过:(“事实上,我已经在创建的输入的名称上使用了rnorm技巧。”。我们在问题中看不到这一点,…(?)。恐怕它只适用于解决方法,…过去有一些类似的问题,…祝你好运,。。。。
      global <- reactiveValues(oldId = c(), currentId = c())
    
        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)