0

I have a function that highlights the text in quotes corresponding to what a user entered into two textInput widgets. If there is no text input in the second textInput filed, the results returned highlights the first space, which is undesirable.

I have tried to use if else statement inside my function without success.

highlight <- function(text, search1, search2) if (search2 != ""){

  x <- unlist(strsplit(text, split = " ", fixed = T))
  x[tolower(x) %in% tolower(c(search1, search2))] <- paste0("<mark>", 
 x[tolower(x) %in% tolower(c(search1, search2))], "</mark>")
paste(x, collapse = " ")

} else 
 x <- unlist(strsplit(text, split = " ", fixed = T))
 x[tolower(x) %in% tolower(c(search1))] <- paste0("<mark>", x[tolower(x) %in% tolower(c(search1))], "</mark>")
 paste(x, collapse = " ")

  library(shiny)
  library(shinydashboard)

       highlight <- function(text, search1, search2) {

       x <- unlist(strsplit(text, split = " ", fixed = T))
       x[tolower(x) %in% tolower(c(search1, search2))] <- paste0("<mark>", 
       x[tolower(x) %in% tolower(c(search1, search2))], "</mark>")
       paste(x, collapse = " ")

       } 

   ui <- dashboardPage(
        dashboardHeader(),
         dashboardSidebar(
            sidebarMenu(
              menuItem("TexSearch", tabName = "Tabs", icon = icon("object-ungroup"))

            )
          ),
     dashboardBody(
        tabItem(tabName = "Tabs",
                fluidRow(
                column(width=3, 
                 box(
                   title="Search ",
                   solidHeader=TRUE,
                   collapsible=TRUE,
                   width=NULL,
                   textInput("quoteSearch1", " Search ",  '', placeholder = "Type keyword/statement"),
                   textInput("quoteSearch2", " Search ",  '', placeholder = "Type keyword/statement"),
                   submitButton("Search")
                 )
          ),

          column( width=9,
                  tabBox(
                    width="100%",
                    tabPanel("tab1", 
                             htmlOutput("quotesearchdetails")
                    )))))))

server <- function(input, output) {
      output$quotesearchdetails <-renderUI({
           outputed=""
       author <- c('John Cage','Thomas Carlyle','Elbert Hubbard', 'Albert Einstein')
          quote <- c('I cant understand why people are frightened of new ideas. Im frightened of the old ones.','The tragedy of life is not so much what men suffer, but rather what they miss.','The greatest mistake you can make in life is to be continually fearing you will make one.', 'Anyone who has never made a mistake has never tried anything new.')

    quotes <- data.frame(author, quote)

   if(input$quoteSearch1!="" | input$quoteSearch2!=""){
      words<-strsplit(input$quoteSearch1,input$quoteSearch2,",")
      words<-as.character(words[[1]])
      words<-tolower(words)
      for(i in 1:length(words)){
       quotes<-quotes[
      grepl(words[i],quotes$quote),]

      }
  if (dim(quotes)[1]>0){
    for(i in seq(from=1,to=dim(quotes)[1])){ 

      outputed<-paste(outputed,
                      paste("Author: ",quotes[i,"author"]),
                      sep="<br/><br/>")
      outputed<-paste(outputed,
                      highlight(   paste("Quote: ",quotes[i,"quote"]),  input$quoteSearch1, input$quoteSearch2),
                      sep="<br/><br/>")

    }

  } else {outputed- "No quotes found."}
}

     HTML(outputed)
})


   }
 shinyApp(ui, server)

The ideal case would be if a user searches only from input$quoteSearch1, the results displayed will not have the first space of the quote highlighted and only the quotes with the searched word are displayed.

Any insight on how to write my function to make it work even when there is no text in input$quoteSearch2 ?

1 Answer 1

1

I think you could simply overwrite search2 with NA if it contains an empty string:

highlight <- function(text, search1, search2) {
  if (search2 == "") search2 <- NA

  x <- unlist(strsplit(text, split = " ", fixed = T))
  x[tolower(x) %in% tolower(c(search1, search2))] <-
    paste0("_", x[tolower(x) %in% tolower(c(search1, search2))], "_")

  paste(x, collapse = " ")
}

Note that I padded the selected words with underscores to check whether the function works. You can change this to whatever you like, of course.

> highlight("I like cats", "cats", "")
[1] "I like _cats_"
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you ...with a little tweaking your suggestion worked.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.