使用selectInput选择向量

时间:2019-12-30 17:38:35

标签: r shiny selectinput

我一直在尝试在Shiny上使用selectInput来选择要绘制的向量的值。

在此示例中,我想使用selectInput在要子集的向量之间进行选择。如果我选择“灰色”,则df应该只选择不大的灰色矢量观测值。

我无法正常工作。我该怎么办?

library(shiny)

ui <- fluidPage(
   titlePanel("Test"),
   sidebarLayout(
      sidebarPanel(
         selectInput("input1",
                     "Input",
                     choices = c(
                       "Blue" = "blue",
                       "Grey" = "grey",
                       "Orange" = "orange"
                     ))
      ),

      mainPanel(
         plotOutput("plot")
      )
   )
)


server <- function(input, output) {

  blue <- c("big", "small", "big", "small", "medium")
  grey <- c("small", "big", "big", "medium", "medium")
  orange <- c("big", "big", "medium", "medium", "medium")
  big <- c("true", "true", "true", "false", "false")
  df<- data.frame(blue, grey, orange, big)


   output$plot <- renderPlot({
      df <- df[input$input1 != "big",]

      plot <- ggplot(df, aes(x=big))+geom_bar()
      return(plot)
   })
}


shinyApp(ui = ui, server = server)

1 个答案:

答案 0 :(得分:0)

input$input1是一个字符串。

代替:

df <- df[input$input1 != "big",]

尝试:

df <- df[which(df[[input$input1]] != "big"),]