R DT用于具有多选项的逗号分隔文本的筛选。

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

R DT filter for comma-separated text with multi-select

问题

我有一个包含星期几列的数据框。该列中可以是一个星期几,也可以是多个星期几,以逗号分隔。
我想要在表格上显示一个筛选器,星期几列应该像因子筛选器一样运行,因此可以弹出包含7个可能星期几的多选框。

我尝试使用yadcf Datatables插件:https://github.com/vedmack/yadcf/

library(shiny)

ui <- fluidPage(
  tags$head(
    tags$link(href="jquery.dataTables.yadcf.css", rel = "stylesheet"),
    tags$script(src="jquery.dataTables.yadcf.js"),
  ),
  dataTableOutput("sometable")
)

jsc <- '
    function(settings, json) {
        var table = settings.oInstance.api();
        yadcf.init(table, [{
          column_number : 2,
          column_data_type: "text",
          data: [{"label": "Montag", "value": 1}, {"label": "Dienstag", "value": 2}, {"label": "Mittwoch", "value": 3},
                 {"label": "Donnerstag", "value": 4}, {"label": "Freitag", "value": 5}, {"label": "Samstag", "value": 6}, {"label": "Sonntag", "value": 7}],
          omit_default_label: true,
          text_data_delimiter: ",",
        }]);
    }
'

server <- function(input, output) {
  output$sometable <- renderDataTable({
    df <- data.frame(ID = 1:6,
                     Weekdays = c("Montag", "Dienstag,Mittwoch", "Mittwoch,Samstag",
                                  "Donnerstag", "Montag,Freitag", "Samstag,Sonntag"))

    datatable(df, filter = "top",
              options = list(
                initComplete = JS(jsc))
    )
  })
}

shinyApp(ui, server)

编辑:这个JS代码有效,但我希望多选框看起来更好,像普通的选择输入,并且我希望它替换默认的filter="top"

jsc <- '
    function(settings, json) {
        var table = settings.oInstance.api();
        yadcf.init(table, [{
          column_number : 2,
          column_data_type: "text",
          data: ["Montag","Dienstag","Mittwoch","Donnerstag","Freitag","Samstag","Sonntag"],
          sort_as: "num",
          omit_default_label: true,
          filter_type: "multi_select",
          text_data_delimiter: ",",
        }]);
    }
'

编辑2:理想情况下,我希望在Tags列中的示例中看到的行为,链接如下:https://yadcf-showcase.appspot.com/cumulative_filtering.html

英文:

I have a dataframe with a weekday column. In it can be one weekday or multiple weekdays, comma separated.
I want to show the table with the filter on top and the weekdays column should behave like a factor filter, so the multi-select pops-up with the 7 possible weekdays.

I tried with the yadcf Datatables plugin: https://github.com/vedmack/yadcf/

library(shiny)

ui &lt;- fluidPage(
  tags$head(
    tags$link(href=&quot;jquery.dataTables.yadcf.css&quot;, rel = &quot;stylesheet&quot;),
    tags$script(src=&quot;jquery.dataTables.yadcf.js&quot;),
  ),
  dataTableOutput(&quot;sometable&quot;)
)

jsc &lt;- &#39;
    function(settings, json) {
        var table = settings.oInstance.api();
        yadcf.init(table, [{
          column_number : 2,
          column_data_type: &quot;text&quot;,
          data: [{&quot;label&quot;: &quot;Montag&quot;, &quot;value&quot;: 1}, {&quot;label&quot;: &quot;Dienstag&quot;, &quot;value&quot;: 2}, {&quot;label&quot;: &quot;Mittwoch&quot;, &quot;value&quot;: 3},
                 {&quot;label&quot;: &quot;Donnerstag&quot;, &quot;value&quot;: 4}, {&quot;label&quot;: &quot;Freitag&quot;, &quot;value&quot;: 5}, {&quot;label&quot;: &quot;Samstag&quot;, &quot;value&quot;: 6}, {&quot;label&quot;: &quot;Sonntag&quot;, &quot;value&quot;: 7}],
          omit_default_label: true,
          //filter_type: &quot;multi_select&quot;,
          text_data_delimiter: &quot;,&quot;
        }]);
    }
&#39;

server &lt;- function(input, output) {
  output$sometable &lt;- renderDataTable({
    df &lt;- data.frame(ID = 1:6,
                     Weekdays = c(&quot;Montag&quot;, &quot;Dienstag,Mittwoch&quot;, &quot;Mittwoch,Samstag&quot;,
                                  &quot;Donnerstag&quot;, &quot;Montag,Freitag&quot;, &quot;Samstag,Sonntag&quot;))

    datatable(df, filter = &quot;top&quot;,
              options = list(
                initComplete = JS(jsc))
    )
  })
}

shinyApp(ui, server)

EDIT: This JS works, but I would like the multi-select to look nicer, like a normal select input and actually I would like that select instead of the default one from filter=&quot;top&quot;.

jsc &lt;- &#39;
    function(settings, json) {
        var table = settings.oInstance.api();
        yadcf.init(table, [{
          column_number : 2,
          column_data_type: &quot;text&quot;,
          data: [&quot;Montag&quot;,&quot;Dienstag&quot;,&quot;Mittwoch&quot;,&quot;Donnerstag&quot;,&quot;Freitag&quot;,&quot;Samstag&quot;,&quot;Sonntag&quot;],
          sort_as: &quot;num&quot;,
          omit_default_label: true,
          filter_type: &quot;multi_select&quot;,
          text_data_delimiter: &quot;,&quot;
        }]);
    }
&#39;

R DT用于具有多选项的逗号分隔文本的筛选。


EDIT 2:

Ideally I would like the behaviour in this example in the Tags column
https://yadcf-showcase.appspot.com/cumulative_filtering.html

答案1

得分: 2

I removed data and omit_default_label and it works fine. Is it what you want?

library(DT)

js &lt;- &#39;
      yadcf.init(table, [
        {
          column_number : 2,
          column_data_type: &quot;text&quot;,
          //data: [{&quot;label&quot;: &quot;Montag&quot;, &quot;value&quot;: 1}, {&quot;label&quot;: &quot;Dienstag&quot;, &quot;value&quot;: 2}, {&quot;label&quot;: &quot;Mittwoch&quot;, &quot;value&quot;: 3},
          //       {&quot;label&quot;: &quot;Donnerstag&quot;, &quot;value&quot;: 4}, {&quot;label&quot;: &quot;Freitag&quot;, &quot;value&quot;: 5}, {&quot;label&quot;: &quot;Samstag&quot;, &quot;value&quot;: 6}, {&quot;label&quot;: &quot;Sonntag&quot;, &quot;value&quot;: 7}],
          //omit_default_label: true,
          text_data_delimiter: &quot;,&quot;
        }
      ]);
&#39;

df &lt;- data.frame(ID = 1:6,
                 Weekdays = c(&quot;Montag&quot;, &quot;Dienstag,Mittwoch&quot;, &quot;Mittwoch,Samstag&quot;,
                              &quot;Donnerstag&quot;, &quot;Montag,Freitag&quot;, &quot;Samstag,Sonntag&quot;))

dtable &lt;- datatable(
  df, #filter = &quot;top&quot;,
  callback = JS(js)
)

dep &lt;- htmltools::htmlDependency(
  &quot;yadcf&quot;, &quot;0.9.3&quot;,
  c(href =  &quot;https://cdnjs.cloudflare.com/ajax/libs/yadcf/0.9.3/&quot;),
  script = &quot;jquery.dataTables.yadcf.min.js&quot;,
  stylesheet = &quot;jquery.dataTables.yadcf.min.css&quot;)
dtable$dependencies &lt;- c(dtable$dependencies, list(dep))
dep &lt;- htmltools::htmlDependency(
  &quot;jquery-ui&quot;, &quot;1.12.1&quot;,
  src = &quot;www/shared/jqueryui/&quot;,
  script = &quot;jquery-ui.js&quot;,
  stylesheet = &quot;jquery-ui.css&quot;,
  package = &quot;shiny&quot;)
dtable$dependencies &lt;- c(dtable$dependencies, list(dep))
dep &lt;- htmltools::htmlDependency(
  &quot;moment&quot;, &quot;2.27.0&quot;,
  c(href =  &quot;https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.27.0/&quot;),
  script = &quot;moment.min.js&quot;)
dtable$dependencies &lt;- c(dtable$dependencies, list(dep))

dtable

R DT用于具有多选项的逗号分隔文本的筛选。


英文:

I removed data and omit_default_label and it works fine. Is it what you want?

library(DT)

js &lt;- &#39;
      yadcf.init(table, [
        {
          column_number : 2,
          column_data_type: &quot;text&quot;,
          //data: [{&quot;label&quot;: &quot;Montag&quot;, &quot;value&quot;: 1}, {&quot;label&quot;: &quot;Dienstag&quot;, &quot;value&quot;: 2}, {&quot;label&quot;: &quot;Mittwoch&quot;, &quot;value&quot;: 3},
          //       {&quot;label&quot;: &quot;Donnerstag&quot;, &quot;value&quot;: 4}, {&quot;label&quot;: &quot;Freitag&quot;, &quot;value&quot;: 5}, {&quot;label&quot;: &quot;Samstag&quot;, &quot;value&quot;: 6}, {&quot;label&quot;: &quot;Sonntag&quot;, &quot;value&quot;: 7}],
          //omit_default_label: true,
          text_data_delimiter: &quot;,&quot;
        }
      ]);
&#39;

df &lt;- data.frame(ID = 1:6,
                 Weekdays = c(&quot;Montag&quot;, &quot;Dienstag,Mittwoch&quot;, &quot;Mittwoch,Samstag&quot;,
                              &quot;Donnerstag&quot;, &quot;Montag,Freitag&quot;, &quot;Samstag,Sonntag&quot;))

dtable &lt;- datatable(
  df, #filter = &quot;top&quot;,
  callback = JS(js)
)

dep &lt;- htmltools::htmlDependency(
  &quot;yadcf&quot;, &quot;0.9.3&quot;,
  c(href =  &quot;https://cdnjs.cloudflare.com/ajax/libs/yadcf/0.9.3/&quot;),
  script = &quot;jquery.dataTables.yadcf.min.js&quot;,
  stylesheet = &quot;jquery.dataTables.yadcf.min.css&quot;)
dtable$dependencies &lt;- c(dtable$dependencies, list(dep))
dep &lt;- htmltools::htmlDependency(
  &quot;jquery-ui&quot;, &quot;1.12.1&quot;,
  src = &quot;www/shared/jqueryui/&quot;,
  script = &quot;jquery-ui.js&quot;,
  stylesheet = &quot;jquery-ui.css&quot;,
  package = &quot;shiny&quot;)
dtable$dependencies &lt;- c(dtable$dependencies, list(dep))
dep &lt;- htmltools::htmlDependency(
  &quot;moment&quot;, &quot;2.27.0&quot;,
  c(href =  &quot;https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.27.0/&quot;),
  script = &quot;moment.min.js&quot;)
dtable$dependencies &lt;- c(dtable$dependencies, list(dep))

dtable

R DT用于具有多选项的逗号分隔文本的筛选。


Edit

To get the desired order:

library(DT)

js &lt;- &#39;
      yadcf.init(table, [
        {
          column_number : 2,
          column_data_type: &quot;text&quot;,
          data: [
            {&quot;label&quot;: &quot;Montag&quot;, &quot;value&quot;: &quot;Montag&quot;}, 
            {&quot;label&quot;: &quot;Dienstag&quot;, &quot;value&quot;: &quot;Dienstag&quot;}, 
            {&quot;label&quot;: &quot;Mittwoch&quot;, &quot;value&quot;: &quot;Mittwoch&quot;},
            {&quot;label&quot;: &quot;Donnerstag&quot;, &quot;value&quot;: &quot;Donnerstag&quot;}, 
            {&quot;label&quot;: &quot;Freitag&quot;, &quot;value&quot;: &quot;Freitag&quot;}, 
            {&quot;label&quot;: &quot;Samstag&quot;, &quot;value&quot;: &quot;Samstag&quot;}, 
            {&quot;label&quot;: &quot;Sonntag&quot;, &quot;value&quot;: &quot;Sonntag&quot;}
          ],
          //omit_default_label: true,
          filter_type: &quot;select&quot;,
          select_type: &quot;jquery-ui&quot;,
          text_data_delimiter: /,/
        }
      ]);
&#39;

df &lt;- data.frame(ID = 1:6,
                 Weekdays = c(&quot;Montag&quot;, &quot;Dienstag,Mittwoch&quot;, &quot;Mittwoch,Samstag&quot;,
                              &quot;Donnerstag&quot;, &quot;Montag,Freitag&quot;, &quot;Samstag,Sonntag&quot;))

dtable &lt;- datatable(
  df, #filter = &quot;top&quot;,
  callback = JS(js)
)

dep &lt;- htmltools::htmlDependency(
  &quot;yadcf&quot;, &quot;0.9.3&quot;,
  c(href =  &quot;https://cdnjs.cloudflare.com/ajax/libs/yadcf/0.9.3/&quot;),
  script = &quot;jquery.dataTables.yadcf.min.js&quot;,
  stylesheet = &quot;jquery.dataTables.yadcf.min.css&quot;)
dtable$dependencies &lt;- c(dtable$dependencies, list(dep))
dep &lt;- htmltools::htmlDependency(
  &quot;jquery-ui&quot;, &quot;1.12.1&quot;,
  src = &quot;www/shared/jqueryui/&quot;,
  script = &quot;jquery-ui.js&quot;,
  stylesheet = &quot;jquery-ui.css&quot;,
  package = &quot;shiny&quot;)
dtable$dependencies &lt;- c(dtable$dependencies, list(dep))
dep &lt;- htmltools::htmlDependency(
  &quot;moment&quot;, &quot;2.27.0&quot;,
  c(href =  &quot;https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.27.0/&quot;),
  script = &quot;moment.min.js&quot;)
dtable$dependencies &lt;- c(dtable$dependencies, list(dep))

dtable

R DT用于具有多选项的逗号分隔文本的筛选。


Edit: nice multi select

If you want to have a nice multi select, you have to set the option select_type=select2 but you also have to include the select2 library.

library(DT)

js &lt;- &#39;
      yadcf.init(table, [
        {
          column_number : 2,
          column_data_type: &quot;text&quot;,
          data: [
            {&quot;label&quot;: &quot;Montag&quot;, &quot;value&quot;: &quot;Montag&quot;}, 
            {&quot;label&quot;: &quot;Dienstag&quot;, &quot;value&quot;: &quot;Dienstag&quot;}, 
            {&quot;label&quot;: &quot;Mittwoch&quot;, &quot;value&quot;: &quot;Mittwoch&quot;},
            {&quot;label&quot;: &quot;Donnerstag&quot;, &quot;value&quot;: &quot;Donnerstag&quot;}, 
            {&quot;label&quot;: &quot;Freitag&quot;, &quot;value&quot;: &quot;Freitag&quot;}, 
            {&quot;label&quot;: &quot;Samstag&quot;, &quot;value&quot;: &quot;Samstag&quot;}, 
            {&quot;label&quot;: &quot;Sonntag&quot;, &quot;value&quot;: &quot;Sonntag&quot;}
          ],
          //omit_default_label: true,
          filter_type: &quot;multi_select&quot;,
          select_type: &quot;select2&quot;,
          text_data_delimiter: /,/
        }
      ]);
&#39;

df &lt;- data.frame(ID = 1:6,
                 Weekdays = c(&quot;Montag&quot;, &quot;Dienstag,Mittwoch&quot;, &quot;Mittwoch,Samstag&quot;,
                              &quot;Donnerstag&quot;, &quot;Montag,Freitag&quot;, &quot;Samstag,Sonntag&quot;))

dtable &lt;- datatable(
  df, #filter = &quot;top&quot;,
  callback = JS(js)
)

dep &lt;- htmltools::htmlDependency(
  &quot;yadcf&quot;, &quot;0.9.3&quot;,
  c(href =  &quot;https://cdnjs.cloudflare.com/ajax/libs/yadcf/0.9.3/&quot;),
  script = &quot;jquery.dataTables.yadcf.min.js&quot;,
  stylesheet = &quot;jquery.dataTables.yadcf.min.css&quot;)
dtable$dependencies &lt;- c(dtable$dependencies, list(dep))
dep &lt;- htmltools::htmlDependency(
  &quot;jquery-ui&quot;, &quot;1.12.1&quot;,
  src = &quot;www/shared/jqueryui/&quot;,
  script = &quot;jquery-ui.js&quot;,
  stylesheet = &quot;jquery-ui.css&quot;,
  package = &quot;shiny&quot;)
dep &lt;- htmltools::htmlDependency(
  &quot;select2_js&quot;, &quot;4.0.13&quot;,
  c(href =  &quot;https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/&quot;),
  script = &quot;js/select2.min.js&quot;,
  stylesheet = &quot;css/select2.min.css&quot;)
dtable$dependencies &lt;- c(dtable$dependencies, list(dep))
dep &lt;- htmltools::htmlDependency(
  &quot;moment&quot;, &quot;2.27.0&quot;,
  c(href =  &quot;https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.27.0/&quot;),
  script = &quot;moment.min.js&quot;)
dtable$dependencies &lt;- c(dtable$dependencies, list(dep))

dtable

R DT用于具有多选项的逗号分隔文本的筛选。


Edit: in Shiny

library(DT)
library(shiny)

js &lt;- &#39;
      yadcf.init(table, [
        {
          column_number : 2,
          column_data_type: &quot;text&quot;,
          data: [
            {&quot;label&quot;: &quot;Montag&quot;, &quot;value&quot;: &quot;Montag&quot;}, 
            {&quot;label&quot;: &quot;Dienstag&quot;, &quot;value&quot;: &quot;Dienstag&quot;}, 
            {&quot;label&quot;: &quot;Mittwoch&quot;, &quot;value&quot;: &quot;Mittwoch&quot;},
            {&quot;label&quot;: &quot;Donnerstag&quot;, &quot;value&quot;: &quot;Donnerstag&quot;}, 
            {&quot;label&quot;: &quot;Freitag&quot;, &quot;value&quot;: &quot;Freitag&quot;}, 
            {&quot;label&quot;: &quot;Samstag&quot;, &quot;value&quot;: &quot;Samstag&quot;}, 
            {&quot;label&quot;: &quot;Sonntag&quot;, &quot;value&quot;: &quot;Sonntag&quot;}
          ],
          //omit_default_label: true,
          filter_type: &quot;multi_select&quot;,
          select_type: &quot;select2&quot;,
          text_data_delimiter: /,/
        }
      ]);
&#39;

df &lt;- data.frame(ID = 1:6,
                 Weekdays = c(&quot;Montag&quot;, &quot;Dienstag,Mittwoch&quot;, &quot;Mittwoch,Samstag&quot;,
                              &quot;Donnerstag&quot;, &quot;Montag,Freitag&quot;, &quot;Samstag,Sonntag&quot;))

dtable &lt;- datatable(
  df, #filter = &quot;top&quot;,
  callback = JS(js)
)

ui &lt;- fluidPage(
  tagList(
    htmltools::htmlDependency(
      &quot;yadcf&quot;, &quot;0.9.3&quot;,
      c(href =  &quot;https://cdnjs.cloudflare.com/ajax/libs/yadcf/0.9.3/&quot;),
      script = &quot;jquery.dataTables.yadcf.min.js&quot;,
      stylesheet = &quot;jquery.dataTables.yadcf.min.css&quot;),
    htmltools::htmlDependency(
      &quot;select2_js&quot;, &quot;4.0.13&quot;,
      c(href =  &quot;https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/&quot;),
      script = &quot;js/select2.min.js&quot;,
      stylesheet = &quot;css/select2.min.css&quot;),
    htmltools::htmlDependency(
      &quot;moment&quot;, &quot;2.27.0&quot;,
      c(href =  &quot;https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.27.0/&quot;),
      script = &quot;moment.min.js&quot;)
  ),
  DTOutput(&quot;dtable&quot;)
)

server &lt;- function(input, output, session) {
  output[[&quot;dtable&quot;]] &lt;- renderDT({
    dtable
  }, server = FALSE)
}

shinyApp(ui, server)

huangapple
  • 本文由 发表于 2023年7月3日 18:04:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76603725.html
匿名

发表评论

匿名网友

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

确定