【问题标题】:Use selectInput to select vectors使用 selectInput 选择向量
【发布时间】:2020-04-19 10:20:02
【问题描述】:

我一直在尝试使用闪亮的 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)

【问题讨论】:

    标签: r shiny selectinput


    【解决方案1】:

    input$input1 是一个字符串。

    代替:

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

    试试:

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

    【讨论】:

    • 它有效,它们只是给出相同的结果。检查lapply(c('blue','grey','orange'), function(x) df[which(df[[x]] != 'big'),]$big) 两个错误,每个为真
    猜你喜欢
    • 2019-08-03
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 2019-03-28
    • 2020-07-28
    • 2021-03-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多