R闪亮的下拉列表用于呈现表格中的列

huangapple go评论165阅读模式
英文:

R shiny Drop down list for columns in rendered table

问题

我正在尝试创建一个下拉列表,以查看每一列中的值,类似于Excel中的方式。但我无法创建下拉列表。我不知道在哪里进行更改以创建这个列表。任何建议都将不胜感激。

代码:

Server.R

  1. library(shiny)
  2. library(DT)
  3. shinyServer(function(input, output, session) {
  4. mtcars2 = data.frame(
  5. name = rownames(mtcars), mtcars[, c('mpg', 'hp')],
  6. stringsAsFactors = FALSE
  7. )
  8. output$tbl = DT::renderDataTable(
  9. mtcars2, filter = 'top', server = TRUE, rownames = FALSE,
  10. options = list(autoWidth = TRUE)
  11. )
  12. })

ui.r

  1. library(shiny)
  2. shinyUI(fluidPage(
  3. title = 'Column Filters on the Server Side',
  4. fluidRow(
  5. DT::dataTableOutput('tbl')
  6. )
  7. ))

注意:以上为您提供的代码部分的翻译。

英文:

I am trying to create a drop down list to view the values within each column, similar to in excel. But I am unable to create the dropdown list. I am unable to understand where to make changes to create this list. Any suggestions, highly appreciated.

code:

Server.R

  1. library(shiny)
  2. library(DT)
  3. shinyServer(function(input, output, session) {
  4. mtcars2 = data.frame(
  5. name = rownames(mtcars), mtcars[, c('mpg', 'hp')],
  6. stringsAsFactors = FALSE
  7. )
  8. output$tbl = DT::renderDataTable(
  9. mtcars2, filter = 'top', server = TRUE, rownames = FALSE,
  10. options = list(autoWidth = TRUE)
  11. )
  12. })

ui.r

  1. library(shiny)
  2. shinyUI(fluidPage(
  3. title = 'Column Filters on the Server Side',
  4. fluidRow(
  5. DT::dataTableOutput('tbl')
  6. )
  7. ))

答案1

得分: 1

以下是翻译好的代码部分:

  1. library(shiny)
  2. library(DT)
  3. dat <- mtcars
  4. sketch <- htmltools::tags$table(
  5. tableHeader(c("", names(dat))),
  6. tableFooter(rep("", 1+ncol(dat)))
  7. )
  8. js <- c(
  9. "function(){",
  10. " this.api().columns().every(function(i){",
  11. " var column = this;",
  12. " var select = $('<select><option value=\"\"></option></select>')",
  13. " .appendTo( $(column.footer()).empty() )",
  14. " .on('change', function(){",
  15. " select.val(null);",
  16. " });",
  17. " var data = column.data();",
  18. " if(i == 0){",
  19. " data.each(function(d, j){",
  20. " select.append('<option value=\"'+d+'\">'+d+'</option>');",
  21. " });",
  22. " }else{",
  23. " data.unique().sort().each(function(d, j){",
  24. " select.append('<option value=\"'+d+'\">'+d+'</option>');",
  25. " });",
  26. " }",
  27. " select.select2({width: '100%'});",
  28. " });",
  29. "}"
  30. )
  31. ui <- fluidPage(
  32. tags$head(
  33. tags$link(rel = "stylesheet", href = "https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/css/select2.min.css"),
  34. tags$script(src = "https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/js/select2.min.js")
  35. ),
  36. br(),
  37. DTOutput("dtable")
  38. )
  39. server <- function(input, output, session){
  40. output[["dtable"]] <- renderDT({
  41. datatable(
  42. dat, container=sketch,
  43. options = list(
  44. initComplete = JS(js),
  45. columnDefs = list(
  46. list(targets = "_all", className = "dt-center")
  47. )
  48. )
  49. )
  50. }, server = FALSE)
  51. }
  52. shinyApp(ui, server)
  1. library(shiny)
  2. library(DT)
  3. library(htmltools)
  4. dat <- mtcars
  5. sketch <- tags$table(
  6. tags$thead(
  7. tags$tr(
  8. tags$th(),
  9. lapply(names(dat), tags$th)
  10. ),
  11. tags$tr(
  12. tags$th(id = "th0"),
  13. tags$th(id = "th1"),
  14. tags$th(id = "th2"),
  15. tags$th(id = "th3"),
  16. tags$th(id = "th4"),
  17. tags$th(id = "th5"),
  18. tags$th(id = "th6"),
  19. tags$th(id = "th7"),
  20. tags$th(id = "th8"),
  21. tags$th(id = "th9"),
  22. tags$th(id = "th10"),
  23. tags$th(id = "th11")
  24. )
  25. )
  26. )
  27. js <- c(
  28. "function(){",
  29. " this.api().columns().every(function(i){",
  30. " var column = this;",
  31. " var select = $('<select><option value=\"\"></option></select>')",
  32. " .appendTo( $('#th'+i).empty() )",
  33. " .on('change', function(){",
  34. " select.val(null);",
  35. " });",
  36. " var data = column.data();",
  37. " if(i == 0){",
  38. " data.each(function(d, j){",
  39. " select.append('<option value=\"'+d+'\">'+d+'</option>');",
  40. " });",
  41. " }else{",
  42. " data.unique().sort().each(function(d, j){",
  43. " select.append('<option value=\"'+d+'\">'+d+'</option>');",
  44. " });",
  45. " }",
  46. " select.select2({width: '100%'});",
  47. " });",
  48. "}"
  49. )
  50. ui <- fluidPage(
  51. tags$head(
  52. tags$link(rel = "stylesheet", href = "https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/css/select2.min.css"),
  53. tags$script(src = "https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/js/select2.min.js")
  54. ),
  55. br(),
  56. DTOutput("dtable")
  57. )
  58. server <- function(input, output, session) {
  59. output[["dtable"]] <- renderDT({
  60. datatable(
  61. dat, container=sketch,
  62. options = list(
  63. orderCellsTop = TRUE,
  64. initComplete = JS(js),
  65. columnDefs = list(
  66. list(targets = "_all", className = "dt-center")
  67. )
  68. )
  69. )
  70. }, server = FALSE)
  71. }
  72. shinyApp(ui, server)
英文:

Try this.

  1. library(shiny)
  2. library(DT)
  3. dat &lt;- mtcars
  4. sketch &lt;- htmltools::tags$table(
  5. tableHeader(c(&quot;&quot;, names(dat))),
  6. tableFooter(rep(&quot;&quot;, 1+ncol(dat)))
  7. )
  8. js &lt;- c(
  9. &quot;function(){&quot;,
  10. &quot; this.api().columns().every(function(i){&quot;,
  11. &quot; var column = this;&quot;,
  12. &quot; var select = $(&#39;&lt;select&gt;&lt;option value=\&quot;\&quot;&gt;&lt;/option&gt;&lt;/select&gt;&#39;)&quot;,
  13. &quot; .appendTo( $(column.footer()).empty() )&quot;,
  14. &quot; .on(&#39;change&#39;, function(){&quot;,
  15. &quot; select.val(null);&quot;,
  16. &quot; });&quot;,
  17. &quot; var data = column.data();&quot;,
  18. &quot; if(i == 0){&quot;,
  19. &quot; data.each(function(d, j){&quot;,
  20. &quot; select.append(&#39;&lt;option value=\&quot;&#39;+d+&#39;\&quot;&gt;&#39;+d+&#39;&lt;/option&gt;&#39;);&quot;,
  21. &quot; });&quot;,
  22. &quot; }else{&quot;,
  23. &quot; data.unique().sort().each(function(d, j){&quot;,
  24. &quot; select.append(&#39;&lt;option value=\&quot;&#39;+d+&#39;\&quot;&gt;&#39;+d+&#39;&lt;/option&gt;&#39;);&quot;,
  25. &quot; });&quot;,
  26. &quot; }&quot;,
  27. &quot; select.select2({width: &#39;100%&#39;});&quot;,
  28. &quot; });&quot;,
  29. &quot;}&quot;)
  30. ui &lt;- fluidPage(
  31. tags$head(
  32. tags$link(rel = &quot;stylesheet&quot;, href = &quot;https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/css/select2.min.css&quot;),
  33. tags$script(src = &quot;https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/js/select2.min.js&quot;)
  34. ),
  35. br(),
  36. DTOutput(&quot;dtable&quot;)
  37. )
  38. server &lt;- function(input, output, session){
  39. output[[&quot;dtable&quot;]] &lt;- renderDT({
  40. datatable(
  41. dat, container=sketch,
  42. options = list(
  43. initComplete = JS(js),
  44. columnDefs = list(
  45. list(targets = &quot;_all&quot;, className = &quot;dt-center&quot;)
  46. )
  47. )
  48. )
  49. }, server = FALSE)
  50. }
  51. shinyApp(ui, server)

R闪亮的下拉列表用于呈现表格中的列


Edit: dropdowns at the top of the table

  1. library(shiny)
  2. library(DT)
  3. library(htmltools)
  4. dat &lt;- mtcars
  5. sketch &lt;- tags$table(
  6. tags$thead(
  7. tags$tr(
  8. tags$th(),
  9. lapply(names(dat), tags$th)
  10. ),
  11. tags$tr(
  12. tags$th(id = &quot;th0&quot;),
  13. tags$th(id = &quot;th1&quot;),
  14. tags$th(id = &quot;th2&quot;),
  15. tags$th(id = &quot;th3&quot;),
  16. tags$th(id = &quot;th4&quot;),
  17. tags$th(id = &quot;th5&quot;),
  18. tags$th(id = &quot;th6&quot;),
  19. tags$th(id = &quot;th7&quot;),
  20. tags$th(id = &quot;th8&quot;),
  21. tags$th(id = &quot;th9&quot;),
  22. tags$th(id = &quot;th10&quot;),
  23. tags$th(id = &quot;th11&quot;)
  24. )
  25. )
  26. )
  27. js &lt;- c(
  28. &quot;function(){&quot;,
  29. &quot; this.api().columns().every(function(i){&quot;,
  30. &quot; var column = this;&quot;,
  31. &quot; var select = $(&#39;&lt;select&gt;&lt;option value=\&quot;\&quot;&gt;&lt;/option&gt;&lt;/select&gt;&#39;)&quot;,
  32. &quot; .appendTo( $(&#39;#th&#39;+i).empty() )&quot;,
  33. &quot; .on(&#39;change&#39;, function(){&quot;,
  34. &quot; select.val(null);&quot;,
  35. &quot; });&quot;,
  36. &quot; var data = column.data();&quot;,
  37. &quot; if(i == 0){&quot;,
  38. &quot; data.each(function(d, j){&quot;,
  39. &quot; select.append(&#39;&lt;option value=\&quot;&#39;+d+&#39;\&quot;&gt;&#39;+d+&#39;&lt;/option&gt;&#39;);&quot;,
  40. &quot; });&quot;,
  41. &quot; }else{&quot;,
  42. &quot; data.unique().sort().each(function(d, j){&quot;,
  43. &quot; select.append(&#39;&lt;option value=\&quot;&#39;+d+&#39;\&quot;&gt;&#39;+d+&#39;&lt;/option&gt;&#39;);&quot;,
  44. &quot; });&quot;,
  45. &quot; }&quot;,
  46. &quot; select.select2({width: &#39;100%&#39;});&quot;,
  47. &quot; });&quot;,
  48. &quot;}&quot;)
  49. ui &lt;- fluidPage(
  50. tags$head(
  51. tags$link(rel = &quot;stylesheet&quot;, href = &quot;https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/css/select2.min.css&quot;),
  52. tags$script(src = &quot;https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/js/select2.min.js&quot;)
  53. ),
  54. br(),
  55. DTOutput(&quot;dtable&quot;)
  56. )
  57. server &lt;- function(input, output, session) {
  58. output[[&quot;dtable&quot;]] &lt;- renderDT({
  59. datatable(
  60. dat, container=sketch,
  61. options = list(
  62. orderCellsTop = TRUE,
  63. initComplete = JS(js),
  64. columnDefs = list(
  65. list(targets = &quot;_all&quot;, className = &quot;dt-center&quot;)
  66. )
  67. )
  68. )
  69. }, server = FALSE)
  70. }
  71. shinyApp(ui, server)

huangapple
  • 本文由 发表于 2023年6月22日 18:58:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76531193.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定