2016-03-21 6 views
0

Ich versuche eine R Shiny App zu erstellen, die benutzerdefinierte Streudiagramme anzeigt. Ich lasse den Benutzer auswählen, welche Variablen aus dem Datensatz mit Auswahlfeldern auf der x- und y-Achse platziert werden sollen. Darüber hinaus (hier beginnt der Fehler) möchte ich den Bereich der Werte von jeder Variablen, die im Diagramm gezeichnet wird, mit Hilfe eines benutzerdefinierten Schiebereglers für jede ausgewählte Variable einschränken. Also gibt der Benutzer grundsätzlich einen Min- und Max-Wert für jede Variable an. Das erreiche ich mit den Funktionen uiOutput und renderUi. Bisher scheinen die Schieberegler gut zu funktionieren.Shiny App: ggplot2 wird das Plot nach der Teilmenge nicht anzeigen, Fehler: inccorect length(), expecting()

Nun das Problem: Sobald ich die Datenteilmenge auf der Grundlage der Eingangswerte und versuchen, das Ergebnis zu plotten, erhalte ich die Fehlermeldung:

"incorrect length(32), expecting 29".

Was ist hier falsch gehen könnte?

Der Datensatz, mit dem ich dies ausführen möchte, ist ziemlich groß, aber für den Beispielcode habe ich den mtcars-Datensatz verwendet (dasselbe Problem).

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) 

Antwort

0

Ihre zweite subdata <- bezieht sich auf verschiedene Länge Strukturen in subdata und in mtcars. Machen Sie es zu einem Ausdruck:

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]) 
Verwandte Themen