【发布时间】:2017-09-28 16:56:06
【问题描述】:
我几乎完成了我的代码,但我还有一个小问题。我需要根据下拉菜单中的输入更改 x 和 y 轴。例如,如果 x = 销售额,任何 y = 研发,则 x 轴应为“销售额(百万美元)”,y 轴应为“研发(百万美元)”,依此类推。但是y轴可以是销售,x轴也可以是研发,这让我很困惑。到目前为止,这是我的代码:
用户界面:
ui = dashboardPage(
dashboardHeader(title = "Apple Financials"),
dashboardSidebar(
fileInput("file1", label = "Upload SAS Data:", accept = ".sas7bdat"),
selectInput("x", label = "X-Axis Variable", choices = c("Sales" = "SALEQ", "Cash" = "CHEQ", "Assets" = "ATQ", "Profits" = "OIADPQ", "R&D" = "XRDQ", "SG&A" = "XSGAQ")),
selectInput("y", label = "Y-Axis Variable", choices = c("Sales" = "SALEQ", "Cash" = "CHEQ", "Assets" = "ATQ", "Profits" = "OIADPQ", "R&D" = "XRDQ", "SG&A" = "XSGAQ"), selected = "XRDQ"),
selectInput("scale", label = "Choose the Scale:", choices = c("Levels" = "identity", "Log 10" = "log10")),
radioButtons("model", label = "Choose the Model:", choices = c("Linear Model" = "lm", "LOESS" = "loess", "Robust Linear" = "rlm", "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) {
output$plots = renderPlot({
data = input$file1
if(is.null(data))
return(NULL)
df = read_sas(data$datapath)
ggplot(df, aes_string(x = input$x, y = input$y)) +
geom_point(size = 2) +
geom_smooth(method = input$model, span = input$span, se =
input$ribbon) +
scale_x_continuous(trans = input$scale) +
scale_y_continuous(trans = input$scale) +
theme_minimal() +
validate(
need(input$x != input$y,
paste("X and Y variables have to be different"))
)
})
}
shinyApp(ui, server)
【问题讨论】: