0

I want to be able to add or remove new words every time I click the "Add words"/"Remove words" buttons. But I want the app to save the previously added/removed words. For example, if I add 'hello' as my first word, and then I add 'bye', I want my vector of words to be ['hello', 'bye']. If I then remove 'hello', my vector should be ['bye']

This is what I have been able to achieve so far

# Define UI ----------
ui <- fluidPage(

  # Sidebar panel
  sidebarLayout(
    sidebarPanel(

      selectInput('addrm',
                  label = h3('Remove or add words'),
                  choices = list('Remove words' = 1,
                                 'Add words' = 2)),
      textInput('words',
                label = 'You can introduce multiple words separated by comma',
                placeholder = 'Enter words...'),

      uiOutput('button')

    ),

    # Main panel
    mainPanel(

      textOutput('removeWords')
    )
  )
)

# Define server logic required ------------
server <- function(input, output){

  output$button <- renderUI({

    if (input$addrm == 1){
      actionButton('button', label = 'Remove words')
    } else{
      actionButton('button', label = 'Add words')
    }
  })

  stopwords <- c()

  output$removeWords <- renderText({

    input$button
    isolate({ # Only runs when the button is clicked
      stopwords <- unique(c(stopwords, unlist(strsplit(gsub(' ', '', input$words), ',')))) 
    })
  })
}

# Run the application 
shinyApp(ui = ui, server = server)
piblo95
  • 123
  • 1
  • 2
  • 10

1 Answers1

0

Nevermind, I was able to find what I was looking for in the following post

Just in case anyone wants the answer:


# Define UI ----------
ui <- fluidPage(

  # Sidebar panel
  sidebarLayout(
    sidebarPanel(

      selectInput('addrm',
                  label = h3('Remove or add words'),
                  choices = list('Remove words' = 1,
                                 'Add words' = 2)),
      textInput('words',
                label = 'You can introduce multiple words separated by comma',
                placeholder = 'Enter words...'),
      uiOutput('button')

    ),

    # Main panel
    mainPanel(

      textOutput('removeWords')
    )
  )
)

# Define server logic required ------------
server <- function(input, output){

  output$button <- renderUI({

    if (input$addrm == 1){
      actionButton('button', label = 'Remove words')
    } else{
      actionButton('button', label = 'Add words')
    }
  })

  values <- reactiveValues()
  values$stopwords <- c()

  output$removeWords <- renderText({

    input$button
    isolate({ # Only runs when the button is clicked
      values$stopwords <- unique(c(values$stopwords, unlist(strsplit(gsub(' ', '', input$words), ',')))) 
    })
  })
}

# Run the application 
shinyApp(ui = ui, server = server)
piblo95
  • 123
  • 1
  • 2
  • 10