2017-09-28 23 views
1

私のコードはほぼ完成しましたが、もう少し小さな問題があります。ドロップダウンメニューからの入力に応じてx軸とy軸を変更する必要があります。たとえば、x = Salesの場合、y = R & Dの場合、x軸は「Sales(million)$」、y軸は「R & D(millions $)」などとなります。しかし、y軸はSalesで、x軸はR & Dでも構いません。これは私を混乱させます。ここに私のコードは、これまでです:ggplot2複数の軸のタイトル

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" = "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")) 
) 
) 

はサーバー:input$xinput$yは、両方の文字列

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) 

答えて

1
lab_choices = c("Sales" = "SALEQ", 
       "Cash" = "CHEQ", 
       "Assets" = "ATQ", 
       "Profits" = "OIADPQ", 
       "R&D" = "XRDQ", 
       "SG&A" = "XSGAQ") 

ui = dashboardPage(
    dashboardHeader(title = "Apple Financials"), 
    dashboardSidebar(
    fileInput("file1", label = "Upload SAS Data:", accept = ".sas7bdat"), 
    selectInput("x", label = "X-Axis Variable", choices = lab_choices), 
    selectInput("y", label = "Y-Axis Variable", choices = lab_choices, 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) + 
    labs(x = paste(names(lab_choices)[lab_choices == input$x], "(millions $)"), 
     y = paste(names(lab_choices)[lab_choices == input$y], "(millions $)")) + 
    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")) 
    ) 
    }) 
} 

をしているので、あなただけの

labs(x = paste(input$x, "(millions $)"), 
    y = paste(input$y, "(millions $)")) 
を追加することができます

編集:

OPは、変数名ではなく軸ラベルとしてユーザーに表示される名前を使用したいという点を強調しました。値と名前の両方が必要なので、ラベル選択肢のグローバル変数を作成し、shinyUIにそのまま供給し、input$xinput$yに一致するnamesをshinyServerに指定します。 choice =の変更点はselectInputlabsであり、ggplot

+0

ありがとうございます!だから、私がそれをすると、例えば "SALEQ(millions $)"が印刷されますが、実際の変数名の代わりに "Sales"と言いたいです。 – Tarzan

+0

@Tarzan私の編集を参照してください。あなたの質問は再現性がなく、shinyAppでは公平ではありませんので、私はそれをテストすることはできません。だからそれ以上の問題があれば教えてください。 – useR

関連する問題