Multi-keywords field in a DT table in Shiny

时间:2016-02-12 21:14:00

标签: r shiny dt

I've a table with a keywords field linked to each entry, many with multi-keywords. The keywords are currently delimited by semicolon like in the example below.

url/registeruser/?username=username&password=password

Is it possible to treat them independently in a way that when searching for one in particular, in a table made with the DT package, every entries where it is present is shown?

For instance, I want the user to be able to search for either "Anticyclonic eddy" or "Arctic halocline". The default search box in author keywords Shaw Anticyclonic eddy; Arctic halocline Eddie Nonhydrostatic modeling treats this entry as one item: "Anticyclonic eddy; Arctic halocline", but I want it to be two separate, individually searchable items.

How should I do it?

1 个答案:

答案 0 :(得分:1)

您不希望如何搜索文本,或者想要从中返回文本,因此示例输出会很好。

然而,根据您所描述的内容,这听起来像是搜索' DT顶部的框为您执行此操作

但是,如果您希望自己控制它以生成单独的表,则可以执行以下操作:

<强> server.R

library(shiny)
library(shinydashboard)
library(DT)

dt <- data.frame(author = c("Shaw", "Eddie"),
                 keywords = c("Anticyclonic eddy; Arctic halocline", "Nonhydrostatic modeling"))


function(input, output, session) {

    output$dt_data <- renderDataTable({
        dt_data <- dt
    })

    ## search dt for text
    output$dt_found <- renderDataTable({

        if(input$txt_search==""){
            dt_found <- NULL
        }else{
            txt <- input$txt_search
            dt_data <- dt

            dt_found <- dt_data[ grep(txt, dt_data$keywords), ]
        }
        return(dt_found)
    })
}

<强> ui.R

library(shiny)
library(shinydashboard)
library(DT)

dashboardPage(
    dashboardHeader(),
    dashboardSidebar(),
    dashboardBody(
        dataTableOutput(outputId = "dt_data"),
        textInput(inputId = "txt_search", label = "Search"),
        dataTableOutput(outputId = "dt_found")
    )
)