在数据表的单元格中显示多个字符串,可以通过单击将其删除

firmo23

我下面有一个闪亮的应用程序,在该程序中,我将d数据框转换为一个数据框,其中items将基于汇总唯一性name并添加一个新列count然后,我使用DT包显示此数据框。不知DTshinywidgets或也许另一方法可以以像显示在下面,其中,用户将能够显示在不同的串屏幕截图中的表中使用items列分隔单词,他将能够移除。这是第二列中的示例。

在此处输入图片说明

library(shiny)
library(DT)
library(jsonlite)

selector <- function(id, values, items = values){
  options <- HTML(paste0(mapply(
    function(value, item){
      as.character(tags$option(value = value, item))
    }, c("", values), c("", items)
  ), collapse = ""))
  as.character(
    tags$select(
      id = id, class = "form-control", multiple = "multiple", options
    )
  )
}

name<-c("Jack","Bob","Jack","Bob")
item<-c("apple","olive","banana","tomato")
d<-data.frame(name,item)

words<-tapply(d$item, d$name, I)


nrows <- length(words)

js <- c(
  "function(settings) {",
  sprintf("var nrows = %d;", nrows),
  sprintf("var words = %s;", toJSON(words)),
  "  var table = this.api().table();",
  "  function selectize(i) {",
  "    $('#slct' + i).selectize({",
  "      items: words[i-1],",
  "      onChange: function(value) {",
  "        table.cell(i-1, 2).data(value.length);",
  "      }",
  "    });",
  "  }",
  "  for(var i = 1; i <= nrows; i++) {",
  "    selectize(i);",
  "    Shiny.setInputValue('slct' + i, words[i-1]);",
  "  }",
  "}"
)

ui <- fluidPage(
  br(),
  DTOutput("table"),
  div( # this is a hidden selectize input whose role is to make
    # available 'selectize.js'
    style = "display: none;",
    selectInput("id", "label", c("x", "y"))
  )
)

server <- function(input, output, session) {
  
  output[["table"]] <- renderDT({
    dat <- data.frame(
      FOO = c(unique(d$name)),
      Words = vapply(
        1:nrows,
        function(i){
          selector(paste0("slct", i), words[[i]])
        },
        character(1)
      ),
      Count = lengths(words),
      stringsAsFactors = FALSE
    )
    
    datatable(
      data = dat,
      selection = "none",
      escape = FALSE,
      rownames = FALSE,
      options = list(
        initComplete = JS(js),
        preDrawCallback = JS(
          'function() { Shiny.unbindAll(this.api().table().node()); }'
        ),
        drawCallback = JS(
          'function() { Shiny.bindAll(this.api().table().node()); }'
        )
      )
    )
  }, server = FALSE)
  
  
}

shinyApp(ui, server)
斯蒂芬·洛朗(Stephane Laurent)

我们可以使用selectizeInput

在此处输入图片说明

library(shiny)
library(DT)

js <- c(
  "function(settings){",
  "  $('#mselect').selectize();",
  "}"
)

ui <- fluidPage(
  br(),
  DTOutput("table"),
  div(
    style = "display: none;",
    selectInput("id", "label", c("x", "y"))
  )
)

server <- function(input, output, session) {
  
  output[["table"]] <- renderDT({
    dat <- data.frame(
      FOO = "bar",
      BAZ = '<select id="mselect" class="form-control" multiple="multiple">
                       <option value=""></option>
                       <option value="A">Apple</option>
                       <option value="B">Banana</option>
                       <option value="C">Lemon</option>
                       </select>',
      stringsAsFactors = FALSE)
    
    datatable(
      data = dat,
      selection = "none",
      escape = FALSE,
      rownames = FALSE, 
      options = list(
        initComplete = JS(js)
      )
    )
  })
  
}

shinyApp(ui, server)

编辑

library(shiny)
library(DT)

selector <- function(id, values, items = values){
  options <- HTML(paste0(mapply(
    function(value, item){
      as.character(tags$option(value = value, item))
    }, c("",values), c("",items)
  ), collapse = ""))
  as.character(
    tags$select(
      id = id, class = "form-control", multiple = "multiple", options
    )
  )
}

words1 <- c("apple", "banana")
words2 <- c("olive", "tomato")

js <- c(
  "function(settings) {",
  sprintf("var words1 = [%s];", toString(shQuote(words1))),
  sprintf("var words2 = [%s];", toString(shQuote(words2))),
  "  $('#slct1').selectize({items: words1});",
  "  $('#slct2').selectize({items: words2});",
  "  Shiny.setInputValue('slct1', words1);",
  "  Shiny.setInputValue('slct2', words2);",
  "}"
)

ui <- fluidPage(
  br(),
  verbatimTextOutput("words1"),
  DTOutput("table"),
  div( # this is a hidden selectize input whose role is to make
       # available 'selectize.js'
    style = "display: none;",
    selectInput("id", "label", c("x", "y"))
  )
)

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

  output[["table"]] <- renderDT({
    dat <- data.frame(
      FOO = c("bar", "baz"),
      Words = c(
        selector("slct1", words1),
        selector("slct2", words2)
      ),
      stringsAsFactors = FALSE
    )

    datatable(
      data = dat,
      selection = "none",
      escape = FALSE,
      rownames = FALSE,
      options = list(
        initComplete = JS(js),
        preDrawCallback = JS(
          'function() { Shiny.unbindAll(this.api().table().node()); }'
        ),
        drawCallback = JS(
          'function() { Shiny.bindAll(this.api().table().node()); }'
        )
      )
    )
  }, server = FALSE)

  output[["words1"]] <- renderPrint({
    input[["slct1"]]
  })
}

shinyApp(ui, server)

编辑

计数:

library(shiny)
library(DT)

selector <- function(id, values, items = values){
  options <- HTML(paste0(mapply(
    function(value, item){
      as.character(tags$option(value = value, item))
    }, c("",values), c("",items)
  ), collapse = ""))
  as.character(
    tags$select(
      id = id, class = "form-control", multiple = "multiple", options
    )
  )
}

words1 <- c("apple", "banana")
words2 <- c("olive", "tomato")

js <- c(
  "function(settings) {",
  sprintf("var words1 = [%s];", toString(shQuote(words1))),
  sprintf("var words2 = [%s];", toString(shQuote(words2))),
  "  var table = this.api().table();",
  "  $('#slct1').selectize({",
  "    items: words1,",
  "    onChange: function(value) {",
  "      var count = value.length;",
  "      table.cell(0,2).data(count);",
  "    }",
  "  });",
  "  $('#slct2').selectize({",
  "    items: words2,",
  "    onChange: function(value) {",
  "      var count = value.length;",
  "      table.cell(1,2).data(count);",
  "    }",
  "  });",
  "  Shiny.setInputValue('slct1', words1);",
  "  Shiny.setInputValue('slct2', words2);",
  "}"
)

ui <- fluidPage(
  br(),
  verbatimTextOutput("words1"),
  DTOutput("table"),
  div( # this is a hidden selectize input whose role is to make
       # available 'selectize.js'
    style = "display: none;",
    selectInput("id", "label", c("x", "y"))
  )
)

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

  output[["table"]] <- renderDT({
    dat <- data.frame(
      FOO = c("bar", "baz"),
      Words = c(
        selector("slct1", words1),
        selector("slct2", words2)
      ),
      Count = c(length(words1), length(words2)),
      stringsAsFactors = FALSE
    )

    datatable(
      data = dat,
      selection = "none",
      escape = FALSE,
      rownames = FALSE,
      options = list(
        initComplete = JS(js),
        preDrawCallback = JS(
          'function() { Shiny.unbindAll(this.api().table().node()); }'
        ),
        drawCallback = JS(
          'function() { Shiny.bindAll(this.api().table().node()); }'
        )
      )
    )
  }, server = FALSE)

  output[["words1"]] <- renderPrint({
    input[["slct1"]]
  })
}

shinyApp(ui, server)

在此处输入图片说明


编辑

对于任意数量的行:

library(shiny)
library(DT)
library(jsonlite)

selector <- function(id, values, items = values){
  options <- HTML(paste0(mapply(
    function(value, item){
      as.character(tags$option(value = value, item))
    }, c("", values), c("", items)
  ), collapse = ""))
  as.character(
    tags$select(
      id = id, class = "form-control", multiple = "multiple", options
    )
  )
}

words <- list(
  c("apple", "banana"),
  c("olive", "tomato")
)

nrows <- length(words)

js <- c(
  "function(settings) {",
  sprintf("var nrows = %d;", nrows),
  sprintf("var words = %s;", toJSON(words)),
  "  var table = this.api().table();",
  "  function selectize(i) {",
  "    $('#slct' + i).selectize({",
  "      items: words[i-1],",
  "      onChange: function(value) {",
  "        table.cell(i-1, 2).data(value.length);",
  "      }",
  "    });",
  "  }",
  "  for(var i = 1; i <= nrows; i++) {",
  "    selectize(i);",
  "    Shiny.setInputValue('slct' + i, words[i-1]);",
  "  }",
  "}"
)

ui <- fluidPage(
  br(),
  verbatimTextOutput("words1"),
  DTOutput("table"),
  div( # this is a hidden selectize input whose role is to make
       # available 'selectize.js'
    style = "display: none;",
    selectInput("id", "label", c("x", "y"))
  )
)

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

  output[["table"]] <- renderDT({
    dat <- data.frame(
      FOO = c("bar", "baz"),
      Words = vapply(
        1:nrows,
        function(i){
          selector(paste0("slct", i), words[[i]])
        },
        character(1)
      ),
      Count = lengths(words),
      stringsAsFactors = FALSE
    )

    datatable(
      data = dat,
      selection = "none",
      escape = FALSE,
      rownames = FALSE,
      options = list(
        initComplete = JS(js),
        preDrawCallback = JS(
          'function() { Shiny.unbindAll(this.api().table().node()); }'
        ),
        drawCallback = JS(
          'function() { Shiny.bindAll(this.api().table().node()); }'
        )
      )
    )
  }, server = FALSE)

  output[["words1"]] <- renderPrint({
    input[["slct1"]]
  })
}

shinyApp(ui, server)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在字符串的单元格数组中查找多个字符串的位置

来自分类Dev

在单个单元格中处理多个字符串和相应的整数

来自分类Dev

在单个单元格中处理多个字符串和相应的整数

来自分类Dev

将每个单元格中的多个字符串列分解为无序的单个单元格字符串

来自分类Dev

如何编写VB脚本以在所有单元格中查找多个字符串并突出显示每一行?

来自分类Dev

如何使用VBA将多个字符串值添加到Excel工作表中的单元格

来自分类Dev

是否可以删除数据表单元格的第一个元素(字符串值)?

来自分类Dev

由于熊猫中单个单元格中有多个字符串,导致日期时间转换错误

来自分类Dev

如果单元格包含某个字符串,则删除表格行

来自分类Dev

当单个单元格包含多个字符串时,与字符串关联的值的总和

来自分类Dev

在单元格中包含一个字符串

来自分类Dev

从数据表中删除空白单元格

来自分类Dev

删除单元格中的第一个字符,并将其复制到Google表格中的相邻单元格中

来自分类Dev

通过vba在数据透视表中显示0而不是空单元格

来自分类Dev

通过vba在数据透视表中显示0而不是空单元格

来自分类Dev

在Excel中,是否有一种方法可以显示包含文本字符串的单元格的数据透视表计数?

来自分类Dev

在MATLAB中,如何在单元格数组中每个字符串的开头插入一个字符串?

来自分类Dev

如何从单元格的字符串中识别和删除单个字母?

来自分类Dev

EXCEL-如果一个单元格区域中有多个字符串

来自分类Dev

如何删除字符串中的多个字符

来自分类Dev

删除文件中的多个字符串/字符

来自分类Dev

删除文件中的多个字符串/字符

来自分类Dev

从数据帧单元格中的字符串中删除单词/字符?

来自分类Dev

如何使用vba写入xml字符串中超过12个字符的单元格

来自分类Dev

删除单个单元格中的可变字符串的多个实例

来自分类Dev

Excel通过多个单词在单元格中搜索字符串

来自分类Dev

包含88个字符的单元格的字符串的换行

来自分类Dev

通过替换或左字符串函数从范围中的单元格中删除子字符串

来自分类Dev

删除单元格中字符串的特定部分

Related 相关文章

  1. 1

    在字符串的单元格数组中查找多个字符串的位置

  2. 2

    在单个单元格中处理多个字符串和相应的整数

  3. 3

    在单个单元格中处理多个字符串和相应的整数

  4. 4

    将每个单元格中的多个字符串列分解为无序的单个单元格字符串

  5. 5

    如何编写VB脚本以在所有单元格中查找多个字符串并突出显示每一行?

  6. 6

    如何使用VBA将多个字符串值添加到Excel工作表中的单元格

  7. 7

    是否可以删除数据表单元格的第一个元素(字符串值)?

  8. 8

    由于熊猫中单个单元格中有多个字符串,导致日期时间转换错误

  9. 9

    如果单元格包含某个字符串,则删除表格行

  10. 10

    当单个单元格包含多个字符串时,与字符串关联的值的总和

  11. 11

    在单元格中包含一个字符串

  12. 12

    从数据表中删除空白单元格

  13. 13

    删除单元格中的第一个字符,并将其复制到Google表格中的相邻单元格中

  14. 14

    通过vba在数据透视表中显示0而不是空单元格

  15. 15

    通过vba在数据透视表中显示0而不是空单元格

  16. 16

    在Excel中,是否有一种方法可以显示包含文本字符串的单元格的数据透视表计数?

  17. 17

    在MATLAB中,如何在单元格数组中每个字符串的开头插入一个字符串?

  18. 18

    如何从单元格的字符串中识别和删除单个字母?

  19. 19

    EXCEL-如果一个单元格区域中有多个字符串

  20. 20

    如何删除字符串中的多个字符

  21. 21

    删除文件中的多个字符串/字符

  22. 22

    删除文件中的多个字符串/字符

  23. 23

    从数据帧单元格中的字符串中删除单词/字符?

  24. 24

    如何使用vba写入xml字符串中超过12个字符的单元格

  25. 25

    删除单个单元格中的可变字符串的多个实例

  26. 26

    Excel通过多个单词在单元格中搜索字符串

  27. 27

    包含88个字符的单元格的字符串的换行

  28. 28

    通过替换或左字符串函数从范围中的单元格中删除子字符串

  29. 29

    删除单元格中字符串的特定部分

热门标签

归档