2017-09-28 42 views
2

今天我一直在爲此工作超過6個小時,而且我知道這是一個簡單的問題,但我無法弄清楚。我想要的是輸入一個文件,然後可以使用多個selectInput下拉菜單來更改ggplot上的輸出。以下是我迄今爲止:如何將fileInput連接到閃存中的ggplot?

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 = 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() 
    }) 
}) 
} 

回答

2

由於輸入是一個字符串,我們需要aes_stringggplot

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

注意:爲了演示,我們上傳的是csv文件而不是SAS文件

+0

謝謝您的迴應!當我使用aes_string時,它會告訴我對象'Sales'不存在。在實際的SAS文件中,它表示爲SALEQ,所以我認爲我需要以某種方式讓SALEQ作爲「銷售」運行? – Tarzan

+1

更新:我能夠呈現圖表。再次感謝你! – Tarzan