2

I have the following app which allows for text to be entered and it is then saved as VALUE and printed on a panel.

enter image description here

Although it looks like I can only do this with one text input at a time - even if I click add (so I don't believe this button is working). On top of that I would like for the user to be able to add multiple inputs (like I have below).

enter image description here

And then my VALUE function should be list with multiple inputs.

code below

library(shiny)


ui <- fluidPage(
  headerPanel("R Package App"),
  
  sidebarPanel(
    #  selectInput("options", "options", choices=c('abc','def')),
    textInput("textbox", "Enter R Package Name", ""),
    actionButton("add","Add")
  ),
  
  mainPanel(
    textOutput("caption")
  )
)
server <- function(input, output, session) {
  observe({
    VALUE <- ''
    if(input$add>0) {
      isolate({
        VALUE <- input$textbox
      })
    }
    updateTextInput(session, inputId = "textbox", value = VALUE)
  })
  
  output$caption <- renderText({
    input$textbox
  })
}

shinyApp(ui = ui, server = server)

RL_Pug
  • 697
  • 7
  • 30

1 Answers1

4

Have you considered using selectizeInput with it's create option?

library(shiny)

packagesDF <- as.data.frame(installed.packages())

ui <- fluidPage(
  headerPanel("R Package App"),
  sidebarPanel(
    selectizeInput(
      inputId = "selectedPackages",
      label = "Enter R Package Name",
      choices = packagesDF$Package,
      selected = NULL,
      multiple = TRUE,
      width = "100%",
      options = list(
        'plugins' = list('remove_button'),
        'create' = TRUE,
        'persist' = TRUE
      )
    )
  ),
  mainPanel(textOutput("caption"))
)

server <- function(input, output, session) {
  output$caption <- renderText({
    paste0(input$selectedPackages, collapse = ", ")
  })
}

shinyApp(ui = ui, server = server)

result

ismirsehregal
  • 30,045
  • 5
  • 31
  • 78
  • This looks good but what if the package in not installed? – RL_Pug Feb 01 '22 at 18:55
  • You can add custom text and persist it, just as I did with "mypackage" (which isn't existing in the choices initially). – ismirsehregal Feb 01 '22 at 18:56
  • Also see this [answer](https://stackoverflow.com/a/11561793/9841389) if you want to populate the choices beyond the installed packages. – ismirsehregal Feb 01 '22 at 19:02
  • For some reason when I deploy the app, the output$captoin returns the rownumber. So for example askpass will show as 1, base will show as 2, and ect in the mainpanel. Any idea why this is? – RL_Pug Feb 04 '22 at 20:55
  • @RL_Pug sounds like you are passing a named list / named vector as the `selectizeInput` choices instead of a character vector (as I did in the above example). – ismirsehregal Feb 05 '22 at 07:08