1

I want to get the items that were selected from selectInput(). However, it seems only the first item could be transferred to server(). The UI:

      sidebarLayout(
        sidebarPanel(width = 4,
          selectInput("cell_marker_density_surv", "Cell type", choices = cell_list, 
                      selected = colnames(density)[1:3], multiple = TRUE),
          textOutput("warning_density_roc_1"),
        ),
        mainPanel(width = 8,
        )
      )

The server()

warning_density_roc_1_output <- reactive({
  a = input$cell_marker_density_surv
  paste0(input$cell_marker_density_surv, collapse = ",")
})

output$warning_density_roc_1 <- renderText(warning_density_roc_1_output())

As we can see, only the first item showed, even in the default situation.

enter image description here

I have realized that there are many questions related to these problems, but I do not know how to solve it. Is it caused by the selectInput() function itself? In fact, I want to give back a warning when the selected inputs are more than five, so I need to know how many items were selected. Could you help me? Thank you!

The following is the code modified based on the first answers:

library(shiny)

mpg <- ggplot2::mpg
library(shinyFeedback)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      width = 4,
      textOutput("warning_density_roc_1"),
      selectInput("cell_marker_density_surv", "Cell type",
                  choices = names(mpg),
                  selected = names(mpg)[1:6], multiple = TRUE
      )
    ),
    mainPanel(width = 8, )
  )
)

server <- function(input, output, session) {
  warning_density_roc_1_output <- reactive({
    print(length(input$cell_marker_density_surv))
    num_input_density_roc <- if(length(input$cell_marker_density_surv) > 5) {
      print("TRUE")
      TRUE
    } else {
      print("FALSE")
      FALSE
    }
    num_input_density_roc
    feedbackWarning("cell_marker_density_surv", num_input_density_roc, "Warning, more than five items selected.")

  })
  
  output$warning_density_roc_1 <- renderText(warning_density_roc_1_output())
}

shinyApp(ui, server)

However, the feedbackWarning() could not work correctly.

tuanzi
  • 27
  • 5

1 Answers1

1

Edit: Using shinyFeedback::feedbackWarning().

library(shiny)
library(shinyFeedback)

mpg <- ggplot2::mpg

ui <- fluidPage(
  shinyFeedback::useShinyFeedback(),
  sidebarLayout(
    sidebarPanel(
      width = 4,
      # textOutput("warning_density_roc_1"),
      selectInput("cell_marker_density_surv", "Cell type",
        choices = names(mpg),
        selected = names(mpg)[1:6], multiple = TRUE
      )
    ),
    mainPanel(width = 8, )
  )
)

server <- function(input, output, session) {
  observeEvent(input$cell_marker_density_surv, {
    shinyFeedback::feedbackWarning(
      "cell_marker_density_surv",
      length(input$cell_marker_density_surv) > 5,
      "Warning, more than five items selected."
    )
  })
}

shinyApp(ui, server)

enter image description here

Old answer:

Maybe this can help, I used mpg dataset as dummy data.

library(shiny)

mpg <- ggplot2::mpg

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      width = 4,
      textOutput("warning_density_roc_1"),
      selectInput("cell_marker_density_surv", "Cell type",
        choices = names(mpg),
        selected = names(mpg)[1:6], multiple = TRUE
      )
    ),
    mainPanel(width = 8, )
  )
)

server <- function(input, output, session) {
  warning_density_roc_1_output <- reactive({
    if (length(input$cell_marker_density_surv) > 5) {
      "Warning, more than five items selected."
    }
  })

  output$warning_density_roc_1 <- renderText(warning_density_roc_1_output())
}

shinyApp(ui, server)
jpdugo17
  • 6,816
  • 2
  • 11
  • 23
  • Thank you very much for your concern! I have solved the problem with your help! I want to use feedbackWarning() to give back a warning. However, the code modified based on yours does not work. I have tested and found the if() could work correctly. I am not sure whether the problem was caused by feedbackWarning(). Could you help me test it? Thank you! – tuanzi Dec 22 '21 at 17:32
  • The code modified based on yours has been updated in the question. – tuanzi Dec 22 '21 at 17:38
  • 1
    @tuanzi I updated my answer to add `feedbackWarning()` case. – jpdugo17 Dec 22 '21 at 22:15
  • 1
    Thanks for your help! The problem has been solved! – tuanzi Dec 23 '21 at 04:55