Searching user input text in files

Hi,
I want to lookup user defined text input in .SAS files placed in folder and I want output will be the name of .SAS and True or False if it contain the text input.
I have R code to do that but i am facing problem in making shiny app ..i tried reactive etc.
Please help
My code

library(stringr)

results = data.frame(matrix(ncol = 2, nrow = 0))
colnames(results) = c("rule_name", "containDB_Core")

rule_list = list.files("C:\\Users\\singhp25\\Desktop\\Tactical rules")
rule_list = rule_list[grepl('.sas', rule_list)] 

for (i in rule_list){
  con <- file(paste("C:\\Users\\singhp25\\Desktop\\Tactical rules\\", i, sep = ""))
  rawContent <- readLines(con) # empty
  rawScript = str_c(rawContent,collapse=' ')    
  close(con)  # close the connection to the file, to keep things tidy
  results[nrow(results)+1, ] = c(i, grepl('DB_CORE', rawScript, ignore.case = T))
}

results[results$containDB_Core == 'TRUE', ]


rule_list

Could you provide a reproducible example of shiny? it would be easier to check what is going wrong.

1 Like

I am trying this

library(shiny)
library(stringr)

results = data.frame(matrix(ncol = 2, nrow = 0))
colnames(results) = c("rule_name", "containDB_Core")

rule_list = list.files("C:\\Users\\singhp25\\Desktop\\Tactical rules")
rule_list = rule_list[grepl('.sas', rule_list)] 

for (i in rule_list){
  con <- file(paste("C:\\Users\\singhp25\\Desktop\\Tactical rules\\", i, sep = ""))
  rawContent <- readLines(con) # empty
  rawScript = str_c(rawContent,collapse=' ')    
  close(con)  # close the connection to the file, to keep things tidy
  #results[nrow(results)+1, ] = c(i, grepl('DB_CORE', rawScript, ignore.case = T))
}

#results[results$containDB_Core == 'TRUE', ]

input$data = load("U:/ModelResult.RData") 

ui = shinyUI(pageWithSidebar(
  headerPanel("Test App"),
  sidebarPanel(
    input$text = textInput("test", "Enter text"),
    submitButton(text = "Submit")
  ),
  mainPanel(
    
    tableOutput("Variable")
    
  )
))


server = shinyServer(function(input, output){
  
  search.criteria <- reactive({

     if(grepl(test,data(rawscript),ignore.case = TRUE)==TRUE)
    {
      results = results[nrow(results)+1, ]
      results <- c("rule_name", "FALSE")
     
    } 
    
    else if (grepl(test,data(rawscript),ignore.case = FALSE)==FALSE)
    {
      results = results[nrow(results)+1, ]
      
      results <- c("rule_name", "FALSE")
      
    } 
    
  }
  )
  
  output$Variable = renderTable({
    print(search.criteria())
    results[search.criteria(), ]    #use the search.critera() reactive to determine rows to display
  })
  
})
  

This is a stab in the dark, but the ideas should transfer to your final solution.

Changes made...

  1. Move the calculation of rawScript to the inside of the server function, so if you refresh the page, it will recalculate rawScript. In the current state, if a new file is added, the shiny app will have to be restarted for it to be found.
  2. Remove the setting of input$text = textInput("test", "Enter text") in the ui object. When the server function runs, input$text will be available with the current text value.
  3. Load the data to my_data instead of input$data. Outside the server function, input does not exist.
  4. Since you will be checking all files dynamically at server run time, we need the file contents.
  5. Inside a reactive, make sure to retrieve the input value. Ex: test <- input$test
  6. Calculate the grep on all content values

Formatting changes:

  • readLines reads until a linebreak. When collapsing back, collapse with a \n to preserve the linebreaks.
  • all = assignments were upgraded to <- assignments
  • readLines can take a file path directly, no need to fuss with connection objects directly.
library(shiny)
library(stringr)

### #1 Move the calculation of `rawScript` inside the server function

my_data <- load("U:/ModelResult.RData") 

ui <- shinyUI(pageWithSidebar(
  headerPanel("Test App"),
  sidebarPanel(
    ### #2 Remove the setting of `input$text`
    textInput("test", "Enter text"),
    submitButton(text = "Submit")
  ),
  mainPanel(  
    tableOutput("Variable")  
  )
))

server <- shinyServer(function(input, output) {

  ### retrieve the file information
  rule_list <- list.files("C:\\Users\\singhp25\\Desktop\\Tactical rules")
  rule_list <- rule_list[grepl('.sas', rule_list)] 

  raw_scripts <- unlist(lapply(rule_list, function(i) {
    rawContent <- readLines(paste("C:\\Users\\singhp25\\Desktop\\Tactical rules\\", i, sep = ""))
    rawScript <- str_c(rawContent, collapse = "\n") ### #4
    rawScript
  }))
  
  ###store the results in a data frame
  rule_info <- data.frame(rule_name = rule_list, content = raw_scripts)

  is_contained <- reactive({
    ### #5 make sure you retrieve the input value
    test <- input$test

    ### #6 calculate the grepl on all the file content vector
    ### I'm guessing that you want a vector of booleans that state if the file contains the text input
    grepl(test, rule_info$content, ignore.case = TRUE)
  })
  
  output$Variable = renderTable({
    print(is_contained())
    rule_info[is_contained(), 1, drop = FALSE]
  })
  
})