2016-05-05 10 views
0

選択した変数に応じてx軸の特定の値をプロットするginyplotボックスプロットを生成します。私はあなたの助けに非常に感謝します!else ifを使用して、反応性で選択された変数に応じて特定のx軸値を選択します。shiny boxplot(mtcars data)

ここでは、ユーザーがギアの数を選択し、さまざまなシリンダーのhpの出力を取得するmtcarsで例を挙げました。

シャイニーにx軸上の異なる順番の組み合わせを表示させるにはどうすればいいですか?&たとえば、ユーザーが3つのギアを選択すると、シリンダーオーダーは4,8,6になります。ユーザーが4つのギアを選択した場合、シリンダーオーダーは6、4になります。ユーザーが5つのギアを選択すると、 8、4(6を省略)とする。

助けてください! :)

はここでオンラインアプリです:ここでhttps://dwok.shinyapps.io/CarEg/

は私のコードです:

# using mtcars data set, select number of gears to reveal hp by cylinder 
    library(shiny) 
# Define UI for Shiny app 
    shinyUI(fluidPage(
    # Application title 
    titlePanel(title = h2("mtcars hp by cylinders", align = "center")), 
    # Sidebar layout 
    sidebarLayout(position = "left", 
    # Sidebar panel 
    sidebarPanel (h4("Select number of"), 
        selectInput("gear", "Gears?", c("3", "4", "5"), 
        selected = "3") 
      ), 
    # Main Panel 
    mainPanel  (h3("Car info:"), 
        textOutput("caption"), 
        plotOutput("gearPlot") 
       ) 
       ) 
       ) 
     ) 

ui.r

server.r

library(shiny) 
library(datasets) 
library(ggplot2) 

shinyServer(function(input, output) { 

    # Use selected variables to shrink size of data frame 
    selectedData <- reactive({ 
    mtcars[mtcars$gear == input$gear, ] 
    }) 
    # THIS BIT IS NOT WORKING. WHY NOT??? 
    # Determine order of cylinders to display on x axis 
    cylOrder <- reactive({ 
    if (input$gear == "3") { 
    cylOrder <- c("4","8","6") 
    } else if (input$gear == "4") { 
    cylOrder <- c("6","4") 
    } else if (input$gear == "5") { 
    cylOrder <- c("8","4")  # yes, I don't want 6 cyl to appear 
    }  
    }) 

    # Compute the forumla text in a reactive expression 
    formulaText <- reactive({ 
    paste("The number of gears you have selected is", input$gear) 
    }) 

    # Return the formula text for printing as a caption 
    output$caption <- renderText({ 
    formulaText() 
    }) 

    # Generate a plot of the requested variable against duration of phase 
    output$gearPlot <- renderPlot({ 
    boxplot(selectedData()$hp ~ selectedData()$cyl, 
      xlab = 'cylinders', ylab = 'hp', 
      main = 'hp by cylinder for gears  selected', 
#STUCK HERE! order (cylOrder()), 
      outline = FALSE 
     ) 
    }) 
    output$mygear <- renderText(input$gear) 
    } 
) 

ためhttps://dwok.shinyapps.io/CarEg/を参照してください。アプリ

答えて

0

変更

cylOrder <- reactive({ 
    if (input$gear == "3") { 
     c("4","8","6") 
    } else if (input$gear == "4") { 
     c("6","4") 
    } else if (input$gear == "5") { 
     c("8","4")  # yes, I don't want 6 cyl to appear 
    }  
}) 

そして

boxplot(selectedData()$hp ~ selectedData()$cyl, 
      xlab = 'cylinders', ylab = 'hp', 
      main = 'hp by cylinder for gears  selected', 
      subset = selectedData()$cyl %in% cylOrder(), 
      outline = FALSE 
    ) 
に箱ひげ図を変更する反応
関連する問題