0

I have built a shiny application which is located at the URL below.

https://hdoran.shinyapps.io/openAnalysis/

On the tab called "Choose CrossFit Open Data" I have a textInput() function that calls a function that uses grep(), which is used to find names in a data frame.

The first time the program loads and a name is entered, the search seemingly occurs quickly and names are returned. However, when I delete the name and type a second name, the search is seemingly very slow.

Is there something I can do to optimize this so that is performs quickly always?

I'm still quite new at shiny and am not sure if somehow making this a reactive expression would help. If so, I'm not quite sure how.

Thanks in advance.

The relevant portion of code in the ui.R file

textInput("name", label = 'Enter an athlete name and find your scores', 
value = "Enter Name Here")

and the relevant portion of code in the server.R file is

output$myScores <- renderPrint({
df <- filedata()
df[grep(input$name, df$Competitor),]
})

And this portion is also in the ui.R file (though I'm not sure it is relevant to the problem)

verbatimTextOutput("myScores"),
dhc
  • 625
  • 1
  • 6
  • 14
  • 1
    I'm always surprised when people who are having problems think they actually know where the "relevant parts" really are. Seems that it would be more efficient to build a test case with both the ui and server portions. My experience suggests that often identifies where the problem actually resides. Maybe you already have done this but reading the question doesn't really tell me what to do to provoke the unwanted behavior. – IRTFM Mar 06 '15 at 02:58

1 Answers1

0

If I understand your goal correctly, you want to give the user the ability to select an input variable based on searching a the competitor column of the dataframe called by filedata()? If so, selectizeInput() is what you are looking for, using server-side selection as outlined here.

Adapted to the code you provided:

ui.r

selectizeInput("name", choices = NULL, multiple = FALSE)

server.r

updateSelectizeInput(session, "name", choices = filedata()$competitor, server = TRUE)

output$myScores <- renderPrint({
    df <- filedata()
    subset(df, competitor==input$name)
})
mdlincoln
  • 274
  • 3
  • 12
  • And FWIW, if you want to display the contents of a data.frame in Shiny, a nicer option than straight text is to use [`tableOutput()`](http://shiny.rstudio.com/reference/shiny/latest/tableOutput.html)/[`renderTable()`](http://shiny.rstudio.com/reference/shiny/latest/renderTable.html) – mdlincoln Mar 06 '15 at 17:24
  • Thank you for this tip. In implementing this, I get the error noted below and am not sure of the root cause. – dhc Mar 09 '15 at 19:57
  • > runApp('openAnalysis') Listening on http://127.0.0.1:5346 Error in .getReactiveEnvironment()$currentContext() : Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.) – dhc Mar 09 '15 at 19:57
  • Also the culprit seems to be this portion of code, updateSelectizeInput(session, "athleteName", choices = filedata()$Competitor, server = TRUE) which sits in the server.r file – dhc Mar 09 '15 at 19:59
  • Hard to check without having a reproducible example, but try wrapping `output$myScores <- renderPrint...` within an `observe({...})` [per this post](http://stackoverflow.com/a/21467399/3547541). – mdlincoln Mar 10 '15 at 02:55