2017-09-28 1 views
2

Ich habe heute für mehr als 6 Stunden daran gearbeitet, und ich weiß, es ist ein einfaches Problem, aber ich kann es nicht herausfinden. Ich möchte eine Datei eingeben und dann mehrere Dropdown-Menüs selectInput verwenden, um die Ausgabe auf dem ggplot zu ändern. Hier ist, was ich bisher:Wie verbinde ich FileInput mit ggplot in Shiny?

UI:

ui = dashboardPage(
    dashboardHeader(title = "Apple Financials"), 
    dashboardSidebar(
    fileInput("file1", label = "Upload SAS Data:", accept = ".sas7bdat"), 
    selectInput("x", label = "X-Axis Variable", choices = c("Sales", "Cash", "Assets", "Profits", "R&D", "SG&A")), 
    selectInput("y", label = "Y-Axis Variable", choices = c("Sales", "Cash", "Assets", "Profits", "R&D", "SG&A"), selected = "R&D"), 
    selectInput("scale", label = "Choose the Scale:", choices = c("Levels", "Log 10")), 
    radioButtons("model", label = "Choose the Model:", choices = c("Linear Model", "LOESS", "Robust Linear", "None"), selected = "LOESS"), 
    checkboxInput("ribbon", label = "Standard Error Ribbon", value = TRUE), 
    conditionalPanel(
     condition = "input.model == 'LOESS'", 
     sliderInput("span", label = "Span for LOESS", min = 0, max = 1, value = .75) 
    ) 
), 
    dashboardBody(
    box(width = NULL, height = 415, plotOutput("plots")) 
) 
) 

Server:

server = function(input, output) { 


    observe({ 
    data = input$file1 
     if(is.null(data)) 
     return(NULL) 

    df = read_sas(data$datapath) 
    output$plots = renderPlot({ 
    ggplot(df, aes(x = input$x, y = input$y)) + 
     geom_line() 
    }) 
}) 
} 

Antwort

2

Da die Eingabe eine Zeichenfolge ist, brauchen wir aes_string in ggplot

server = function(input, output) { 


    observe({ 
    data = input$file1 
    if(is.null(data)) 
     return(NULL) 

    df = read_sas(data$datapath) 
    output$plots = renderPlot({ 
     ggplot(df, aes_string(x = input$x, y = input$y)) + 
     geom_line() 
    }) 
    }) 
} 
shinyApp(ui, server) 

enter image description here

HINWEIS: Zur Demonstration laden wir eine CSV-Datei anstelle von SAS-Datei

+0

Vielen Dank für Ihre Antwort! Wenn ich aes_string verwende, sagt es mir, dass das Objekt 'Sales' nicht existiert. In der eigentlichen SAS-Datei wird es als SALEQ bezeichnet, also muss ich SALEQ irgendwie als "Sales?" – Tarzan

+1

UPDATE: Ich konnte den Graphen rendern. Danke nochmal! – Tarzan

Verwandte Themen