闪亮应用:ggplot2在子集化数据后不会显示图,错误:inccorect length(),expecting()

时间:2016-03-21 06:42:53

标签: r shiny

我正在尝试构建一个显示自定义散点图的R Shiny应用程序。我让用户选择数据集中的哪些变量放在x和y轴上,并带有选择框。此外(这是麻烦开始的地方)我希望能够通过每个选定变量的自定义滑块限制图表中绘制的每个变量的值范围。因此,用户基本上为每个变量指定最小值和最大值。我使用uiOutputrenderUi函数完成此操作。到目前为止,滑块似乎工作正常。

现在,问题是:一旦我根据输入值对数据进行了子集并尝试绘制结果,我就会收到错误:

  

"长度不正确(32),期待29"。

这里可能出现什么问题?

我打算运行它的数据集相当大,但是对于示例代码,我使用了mtcars数据集(同样的问题)。

library(shiny);library(ggplot2);library(dplyr)

#load data
data(mtcars)

ui <- shinyUI(fluidPage(

   # Application title
   titlePanel("Mtcars Data"),

   # Sidebar with a slider input for number of bins 
   sidebarLayout(
      sidebarPanel(
         selectInput(inputId = "x",
                     label = "x variable:",
                     choices = names(mtcars),
                     selected = names(mtcars)[1]),
         uiOutput("xslider"),
         selectInput(inputId = "y",
                     label = "y variable:",
                     choices = names(mtcars),
                     selected = names(mtcars)[4]),
         uiOutput("yslider")
      ),

      mainPanel(
         plotOutput("scatterPlot")
      )
   )
))

# Define server logic required to draw scatterplot
server <- shinyServer(function(input, output) {
   #Get summary stats for x and y variables,
   #this is used to set the parameters for the sliders in renderUI
   summary.x <- reactive({
           summary(mtcars[,input$x])
   })
   summary.y <- reactive({
           summary(mtcars[,input$y])
   })

   output$xslider <- renderUI({
           sliderInput(inputId = "xlim",
                       label = "limit x axis:",
                       min = summary.x()[1],
                       max = summary.x()[6],
                       value = c(summary.x()[1], summary.x()[6]),
                       step = .01)
   })

   output$yslider <- renderUI({
           sliderInput(inputId = "ylim",
                       label = "limit y axis:",
                       min = summary.y()[1],
                       max = summary.y()[6],
                       value = c(summary.y()[1], summary.y()[6]),
                       step = .01)
   })

   output$scatterPlot <- renderPlot({
           #Now subset data so values of x variable 
           #are within the range specified by input$xlim
           #and values of y are within range of input$ylim
           subdata <- filter(mtcars, mtcars[,input$x] < input$xlim[2],
                          mtcars[,input$x] > input$xlim[1])
           subdata <- filter(subdata, mtcars[,input$y] < input$ylim[2],
                             mtcars[,input$y] > input$ylim[1])
           #display the scatterplot        
           ggplot(subdata, aes_string(input$x,input$y)) + geom_point()

   })
})

# Run the application 
shinyApp(ui = ui, server = server)

1 个答案:

答案 0 :(得分:0)

您的第二个subdata <-指的是subdatamtcars中不同的长度结构。做一个表达:

subdata <- filter(mtcars,
                  mtcars[,input$x] < input$xlim[2],
                  mtcars[,input$x] > input$xlim[1],
                  mtcars[,input$y] < input$ylim[2],
                  mtcars[,input$y] > input$ylim[1])