2017-12-28 34 views
0

다른 입력으로 필터링 된 부분 집합을 기반으로 선택한 변수의 막대 그래프를 그리는 간단한 응용 프로그램을 작성하려고합니다. dataX$mpg을 반환해야하는 라인 hist(dataX()$datasetInput())에서 오류가 발생합니다. 어떻게 해결할 수 있습니까? 전체 코드 :비 기능을 적용하려고했습니다.

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) 
에게 있습니다