【问题标题】:Shiny error: attempt to select less than one element from selectInput闪亮的错误:尝试从 selectInput 中选择少于一个元素
【发布时间】:2016-01-24 22:07:04
【问题描述】:

如何在 Shiny 的 selectInput 调用中添加要从中选择的类变量,以从 ggplot 生成条形图?在我的数据集总数中,我想选择 Class 变量以生成不同的 ggplot 条形图。我收到错误:“尝试选择少于一个元素”。

    totals<-data.frame(Class=c("A","A","A","B","B","B","c","C","D","D","D","D"),
               Type=c("Type1","Type2","Type3","Type1","Type2","Type3","Type1",
                      "Type2","Type1","Type2","Type3","Type4"),
               Acres=c(45,543,434,434,434,455,683,345,567,77,52,86))

server.r 代码如下:

    library(shiny)
    library(ggplot2)

    shinyServer(function(input, output, session) {

    output$acresPlot <- reactivePlot(function() {

    acresData <- data.frame(acres = totals$Acres, var = factor(totals[[input$variable]]))

    p <- ggplot(acresData, aes(var, acres)) + 
    geom_bar(stat="identity") 
    print(p)
    })
    })

ui.r 代码如下:

    shinyUI(pageWithSidebar(
    headerPanel("My types by class"),

    sidebarPanel(
    selectInput("Class", "Class:",
            c("A" = "A", 
                 "B" = "B", 
                 "C" = "C",
                 "D"="D"))),
    mainPanel(
    plotOutput("acresPlot")
    )
     ))

【问题讨论】:

    标签: r ggplot2 shiny


    【解决方案1】:

    我不完全确定你想要什么,但如果你只想一次选择一门课,你可以这样做:

    library(shiny)
    library(ggplot2)
    
    totals<-data.frame(Class=c("A","A","A","B","B","B","c","C","D","D","D","D"),
                       Type=c("Type1","Type2","Type3","Type1","Type2","Type3","Type1",
                              "Type2","Type1","Type2","Type3","Type4"),
                       Acres=c(45,543,434,434,434,455,683,345,567,77,52,86))
    
    server <- shinyServer(function(input, output, session) {
    
      output$acresPlot <- renderPlot(function() {
        inds <- totals$Class == input$Class
        acresData <- data.frame(acres = totals$Acres[inds], var = totals$Class[inds])
    
        p <- ggplot(acresData, aes(var, acres)) + 
          geom_bar(stat="identity") 
        print(p)
      })
    
      output$acresPlot2 <- renderPlot(function() {
        inds <- totals$Class == input$Class
        acresData <- data.frame(acres = totals$Acres, var = totals$Class, col=inds)
    
        p <- ggplot(acresData, aes(var, acres, fill =col)) + 
          geom_bar(stat="identity") 
        print(p)
      })
    })
    
    ui <- shinyUI(pageWithSidebar(
      headerPanel("My types by class"),
    
      sidebarPanel(
        selectInput("Class", "Class:",
                    c("A" = "A", 
                      "B" = "B", 
                      "C" = "C",
                      "D"="D"))),
      mainPanel(
        plotOutput("acresPlot"),
        plotOutput("acresPlot2")
      )
    ))
    
    shinyApp(ui=ui,server = server)
    

    acresPlot 显示所选栏,acresPlot2 显示所选栏以及所有其他栏。

    【讨论】:

    • 谢谢@Oscar。指定 inds[] 对象效果很好。
    猜你喜欢
    • 2015-07-13
    • 2023-04-04
    • 2016-06-10
    • 2020-07-28
    • 1970-01-01
    • 1970-01-01
    • 2018-10-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多