2017-12-28 17 views
0

私は、他の入力によってフィルタリングされたサブセットに基づいて、選択された変数のヒストグラムを描画する単純なアプリケーションを構築しようとしています。私はという行にエラーがあり、それはdataX$mpgを返すはずです。どうすれば修正できますか? フルコード:非機能を適用しようとしました

library(shiny) 
u <- shinyUI(pageWithSidebar(
    headerPanel("Staz w bezrobociu"), 
    sidebarPanel(

    selectInput("variable", "Variable:", 
       list("Milles/gallon", 
        "Horse power") 
    ), 
    textInput("nc","Number of cylinders",value = 6) 
), 

    mainPanel(
    plotOutput("Plot") 
) 

)) 

s <- shinyServer(function(input, output) 
{ 
    dataX <- reactive({mtcars[mtcars$cyl==input$nc,,drop = FALSE]}) 

    datasetInput <- reactive({ 
    switch(input$variable, 
      "Milles/gallon" = mpg, 
      "Horse power" = hp) 
    }) 

    output$Plot <- renderPlot({ 

    hist(dataX()$datasetInput()) 
    }) 

}) 
shinyApp(u,s) 

答えて

0

シンプルなアプリを複雑にしました。

  1. selectInputにすべての列をリストする必要はありません。サーバー側からレンダリングするだけです。
  2. 円筒にも同じです
  3. uとのようなショートカットは使用できますが、命名規則に従ってください。それはあなたの人生を楽にします。以下は

完全な作業アプリ


library(shiny) 
ui <- shinyUI(pageWithSidebar(
    headerPanel("Staz w bezrobociu"), 
    sidebarPanel(uiOutput("SelectColname"), 
       uiOutput("Cylinders")), 
    mainPanel(plotOutput("Plot")) 
)) 

server <- shinyServer(function(input, output){ 
    # Create a reactive dataset 
    dataX <- reactive({ 
    mtcars 
    }) 

    # Output number cylinders as select box 
    output$Cylinders <- renderUI({ 
    selectInput("cylinders", "cylinders:", unique(dataX()$cyl)) 
    }) 

    # Output column names as selectbox 
    output$SelectColname <- renderUI({ 
    selectInput("variable", "Variable:", colnames(dataX()[,c(1,4)])) 
    }) 

    # Based on the selection by user, create an eventreactive plotdata object 
    plotdata <- eventReactive(input$cylinders, { 
    plotdata = dataX()[dataX()$cyl == input$cylinders, , drop = FALSE] 
    }) 

    # Render the plot, the plot changes when new cylinder is selected 
    output$Plot <- renderPlot({ 
    if (is.null(plotdata())) 
     return(NULL) 
    hist(
     plotdata()[, input$variable], 
     xlab = input$variable, 
     main = paste(
     "Histogram of" , 
     input$variable 
    ) 
    ) 
    }) 

}) 
shinyApp(ui, server) 
です
関連する問題