Searches through text for a particular term and displays a table showing the cases that contain that search term. This is useful when you try to understand the context of a specific word or phrase in your text. The input text can be a Text variable, a character vector in an R Calculation Output, or the processed text from a variable created by How to Extract Keywords and Phrases from Text. If you use processed text to search, the table will display both the original text input as well as the processed text.
- Method - Create the search analysis
- Extract processed text
- Make the search widget interactive in View Mode
- Formatting text data
Requirements
- Requires one of the following:
- A Setup Text Analysis object
- A Text variable, see Formatting text data below if you have more than one text variable.
- Or a character vector in a Calculation
Method - Create the search analysis
- From the Report tree, select + > Advanced Analysis > Text Analysis > Advanced > Search.
- Under Data > Input, select a Setup Text Analysis object, a Text variable, or a Calculation listing text.
- Enter a Search Term (in the example below, wine) and hit Enter. The output will display a table with results that include the search term.
Worked example:
In the example above, we have first processed the text from a survey question that asked people the first thought that comes to mind about France, using Extract Keywords and Phrases from Text. Since the text has been pre-processed, the table shows both the original text and the results of the processing. If you use a text variable or character vector under Data > Input, then the result will instead be a single column containing the text only. The search term that is used is the word wine, and it appears in bold when it is identified in the processed text.
Extract processed text
To get an output that contains only the processed text (i.e., the right column of keywords above), take these steps:
-
Click + in the Report pane, or from the toolbar while on a page, select Calculation
> Custom Code.
- Click on the page to add the calculation.
-
In the Code
panel, paste the code below, where text.search is the name of your Text Search output:
text.search$Processed.TextClick Calculate.
Make the search widget interactive in View Mode
If you want viewers of your dashboard to be able to search text responses, you can create a Text Control and edit the R code of the Text Analysis - Search output to reference that instead of the field in the object inspector. To do so:
- First, create the Text Control. While on a Page, from the toolbar, click Control
> Text.
- Click on the Page to place your Text Control.
- Optional - in Properties
, you can use the Tooltip or Placeholder fields to add in helping or default text.
- Now edit the Text Analysis - Search R code to reference the text control. Select the search output and access the R code via Properties
> Attributes > Code.
- In the Code
panel, press Ctrl+F and search for
formSearchin the code. - Highlight
formSearchand then click on the name that pops up on your Text Control to swap in the Text Control reference in the code. - Now you can type in your search term into the Text Control for searching.
Formatting text data
The feature is built to search for a single variable/list of data, but sometimes you may have multiple variables you want to search at once. You can reformat your text into one variable/list to use in the tool. There are a couple of ways to do this, depending on what you need.
To create a new variable in the data set that concatenates the text together
Create a new JavaScript text variable, and then in the Code panel, paste and modify the following code:
//change textVar1, textVar2 etc to the Names of your text variables
//you can additionally change " - " below to whatever you want to use to separate the responses
textVar1 + " - " + textVar2 + " - " + textVar3To stack the text variables together in an R Calculation
You can reformat your variables into a "long" format where you stack all of the text variable data on top of one another.
- Optional: If the text variables to search are in individual sets, you need to first combine them into a single Variable Set:
- From the Data Sources tree, select the variables, right-click, and select Duplicate.
- With the duplicates selected, right-click, and select Combine > As a Variable Set. The Structure of the new variable set should be recognized as a Text-Multi.
-
From the toolbar or + in the Report tree, go to Calculation
> Custom Code.
- If using the toolbar, click on the Page to position the output.
-
In the Code
panel, paste in the following code and modify as described
#specify the Text-Multi variable set to search #put the label of the set within backticks below textvar=`My Text Variable Set` #make a vector texts=unlist(textvar) #remove names to make one column names(texts) <- NULL #show final text texts - Click Calculate.
- Use the output created above in the search output in Properties
> Data > Input field.