1

So I have a shiny app and a text input that receives a single word which serves as input for a function, it looks like this in the UI:

textInputAddon(inputId="taxa",addon=icon("search"), 
                       label=tags$h4(tags$strong("Enter the name of the taxonomic group:")),placeholder="Example: Cetacea")

Then in the server the word submited in the input is used to download a tsv file, and I render it like this:

taxaInput <- reactive({grades(input$taxa)})
  output$downloadData <- downloadHandler(
    filename = function() {
      paste(input$taxa, ".tsv")
    },
    content = function(file) {
      shiny::withProgress(
        message=paste0("Downloading and annotating dataset for  ",input$taxa), detail='This may take several minutes',
        value=0,
        {
          shiny::incProgress(1/10)
          Sys.sleep(1)
          shiny::incProgress(4/10)
          write_tsv(taxaInput(), file)
        }
      )
    }
  )

The "grades()" function is my user made function and I can onlye use it to download the file searching for one single word. What I want is to be able to search to put something like this in the input: Word1,Word2,Word3

In a simples R script I used to replace the word for a vector:

group<-c(Word1,Word2,Word3)

But in the shiny app I'm not being able to

Thanks for any response in advance

tadeufontes
  • 443
  • 1
  • 3
  • 12
  • 2
    Have you tried `strsplit`? – yusuzech Aug 21 '19 at 23:27
  • I haven't, thanks, I'll give it a try. but will it still work if someone uses only one word for the input? – tadeufontes Aug 21 '19 at 23:41
  • I’m not clear what is needed. Are you trying to do ‘group<-paste(word1,word2,word3, sep=“,”)’? – IRTFM Aug 21 '19 at 23:42
  • yes, I think either strplit or paste will work, thanks – tadeufontes Aug 21 '19 at 23:47
  • so normally if I were in the R script I would just separate the words inside a vector like c(word1,word2,word3) for my "grades" function to work, so I basically want the best way for that to be done through the text input in my shiny app if a user wants to search for "word1,word2,word3" instead of just "word1" for example – tadeufontes Aug 21 '19 at 23:52

1 Answers1

3

You can use unlist(strsplit(input$taxa, ",")). However you are better off using selectInput() with multiple = T if the taxa choices are exhaustive. That way you'll won't need strsplit and also you have complete control on what words are entered.

Anyways, here's the way with strsplit -

library(shiny)

shinyApp(
  ui = fluidPage(
    textInput("words", "Enter Words"),
    verbatimTextOutput("test")
  ),
  server = function(input, output, session) {
    output$test <- renderPrint({
      words <- unlist(strsplit(input$words, ",")) # get words separated by ','
      paste0(words, "_", seq_along(words)) # some random function
    })
  }
)

App snapshot -

enter image description here

Shree
  • 10,835
  • 1
  • 14
  • 36