Shiny Multi values text

I have a multi line text box. Its working when I entered a single value

      h4("Multi-line text input test"),
      tags$style(type="text/css", "textarea {width:100%}"),
      tags$textarea(id = 'input_text',placeholder = 'Type here', rows = 8, ""),
      verbatimTextOutput("output_text"),

data2<-subset(data,data$team %in% input$input_text)



I want to make it working when multiple values entered. please find attached file, with (fig 1)single value working. (Fig 2) two values and throws error.

What error???????????

I am trying to plot a chart, whenever I enter two values it gives null dataset because the selection is not taken and it gives Asthetic error.

A textual description of an error is not as useful as a coding example. To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this resources, to see how to create one for a shiny app

2 Likes

I am able to create Shiny App and produce the output. I have drop down and multiline text box in my UI

shinyUI(
  pageWithSidebar(
    headerPanel("The Team analysis"),
    sidebarPanel(
     
      selectInput("SFAM","Select Team",choices = c("A","B"),selected = FALSE),
     
        h4("Multi-line text input test"),
      tags$style(type="text/css", "textarea {width:100%}"),
      tags$textarea(id = 'input_text',placeholder = 'Type here', rows = 8, ""),
      verbatimTextOutput("output_text"),
      actionButton("Run","click me")

      
    ),
I am connecting the text from multiline text box to subset a R dataframe.
when I am typing a single value its giving the result with graph in place.(a bar or line chart), R code is as follows;
Server:
```R
shinyServer(
  
function(input,output){
output$MyPlot<-renderPlot({
data2<-subset(data,data$team %in% input$input_text)
ggplot(data2, aes(x=data2$team)) +
 geom_bar(aes(y=data2$Count), fill='deepskyblue4', stat="identity") 
}
})
```R
When I am entering multiple value in multiline text box, am getting empty dataframe which result in no graph.
1. I want to know is there any other text box which  can accommodate multiple entry and pass it to subset using "in"
**"data2<-subset(data,data$team %in% input$input_text)"**
2. How we can enter value in multi line text box whether comma separated or quotes separated,
example: Team1, Team2 or "Team1" "Team2".

Please help me with this..

data$team %in% input$input_text

%in% compares two vectors. You have a vector on the left, but on the right you have a single string with one or more team names. You need to split the right hand side into a vector. Maybe str_trim(str_split(input$input_text, ",")) would do it. These functions are from the stringr package

Also you don't need/shouldn't put data2$... in the aesthetic.

ggplot(data2, aes(x = team)) +
 geom_bar(aes(y = Count), fill = 'deepskyblue4', stat = "identity")
1 Like

Thanks for the reply, I tried it but the input is not taken to provide a valid Data frame. I am getting an empty dataset. I want to know any Shiny Text box or any other menu that can provide option for user to enter multiple value and with any specific format(so that, it can easily pass in R code).

I provide a reprex

library(shiny)

ui <- fluidPage(
  h4("Multi-line text input test"),
  tags$style(type="text/css", "textarea {width:100%}"),
  tags$textarea(id = 'input_text',placeholder = 'Type here', rows = 8, ""),
  tableOutput("tout")
)

server <- function(input, output, session) {
  mycars <- mtcars %>% as_tibble(rownames = "carnames") %>% 
    select(carnames,mpg)

  output$tout <- renderTable({
    if(isTruthy(input$input_text)){
    
  filter(
    mycars,
    str_detect(string =  carnames,
               pattern = str_split(input$input_text, ",") %>%
                 unlist %>% str_trim() %>% paste0(collapse="|")) 
  )
    } else {
      mycars
    }
  })
}

shinyApp(ui, server)
2 Likes

Thanks for the reply,I tried the above code and got below error. I am attaching the input box from UI,

I am giving the user input values as Team1,Team2. Is this ok that the value entered by user is separated by "Comma" and that "Input_text" in the filter but got error as "MISSING VALUES IN 'FILTER'"

An image shows only that you have a problem, it doesn't show what you are trying . Provide a reprex if you want more help

I have UI as below

If user selects Team1 its associated value should pop up in "Select Reference".
If user doesn't want to select values from "Select Reference", he can manually enter value in "Multi-line text Input test".

My issue is, how user can enter value in multiple text box, whether values needs to be separated by "comma"? or in which format. Once user entered value how it can be passed in R code.

Doesn't my example show that?
Type :
Maz, Merc

Into it

Yeah, I tried it but getting the same. To be specific, I need to know how user inputs multiple values in a text box or any other input box that can be used to pass in a sql query or subset in R.

Example:
Multiple text box: Team2, Team4

SQL query:( select * from Team where teams in ("Multiple text box Values")

or

R Subset<-(subset(Team, Team$teams "%in%" ("Multiple text box Values")

Thanks.

Filtering mycars by input$input_text

1 Like

Thank you [nirgrahamuk]: Yeah filter option sounds good. But in multiple text box if I enter single value its working, but multiple or even extra ,(comma) its not taking input.

Example: Team1 -- working
rather: Team1, -- Not working or Team1,Team2 also not working

Any other text box or option available in UI. Thanks in advance

But my example shows that commas work. So if your own different code doesn't work, you should change your code to follow the structure of my example?

Thank you [nirgrahamuk] its working yeah....... Thank you very much for your help.

I just found that its bringing all string with matching text in it.

after using the code:

filter(
    mycars,
    str_detect(string =  carnames,
               pattern = str_split(input$input_text, ",") %>%
                 unlist %>% str_trim() %>% paste0(collapse="|")) 
```R
Example: if input is "Team1,Team2",
it is bringing all the values which has "Team1,Team2". i.e.. "Green/Team1, RedTeam2,TTeam2"

How can I get exactly matching string, i.e.. only "Team1" or "Team2" etc..
Any help on this.
 str_detect(string =  carnames,
                   pattern = paste0("^",str_split(input$input_text, ",") %>%
                     unlist %>% str_trim(),"$") %>% paste0(collapse="|")) 

going back to my example.
Test the above change by typing, or pasting
Mazda RX4, Duster 360
for example

1 Like

Great nirgrahamuk, that gives a perfect solution to my problem. Thank you so much, that helps a lot.