当Multiple = TRUE时,Shiny仅使用selectInput的第一项 [英] Shiny only uses the first item of selectInput when multiple = TRUE

查看:26
本文介绍了当Multiple = TRUE时,Shiny仅使用selectInput的第一项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用绘图中 input $ levels 的值作为标题,但是 selectInput 仅在选择多个时显示第一个值.情节本身以正确的方式更改,这意味着闪亮知道选择了多个级别.

I'm trying to use the value(s) of input$levels in the plot as the title but selectInput is only displaying the first value when multiple are selected. The plot itself changes in the correct way, which means shiny knows that multiple levels are selected.

library(tidyverse)
library(shiny)

test_data <- data.frame(x = seq(1, 100, 10),
                        y = seq(1, 100, 10),
                        level = rep(c(1, 2), 5))

ui <- fluidPage(

  titlePanel("Example"),

  sidebarLayout(
    sidebarPanel(
      selectInput("levels",
                  "Include level(s):",
                  selected=1,
                  choices=c(1, 2),
                  multiple=TRUE)
    ),

    mainPanel(
      plotOutput("plot")
    )
  )
)


server <- function(input, output) {

  output$plot <- renderPlot({
    ggplot(test_data %>% 
             filter(level %in% input$levels), aes(x, y)) + 
      geom_point() +
      ggtitle(paste("Including level(s):", input$levels))
  })

}

shinyApp(ui, server)

选择多个后,如何访问 selectInput 的所有值?

How does one access all values of selectInput when multiple are selected?

推荐答案

input $ levels 包含所选项目的向量.要使它们显示在图形标题中,您可以执行以下操作:

input$levels contains a vector of your selected items. To make them appear in the title of your graph, you can do:

  ggtitle(paste("Including level(s):", paste(input$levels,collapse=', ')))

希望这会有所帮助!

这篇关于当Multiple = TRUE时,Shiny仅使用selectInput的第一项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆