2016-07-08 1 views
0

나는 간단한 반짝 이는 애플 리케이션을 수행하도록 요청 받았습니다. 다음 코드를 가지고 있습니다 :tabpanel 반짝 이는 애플 리케이션 내의 R

library(shiny) 

ui <- fluidPage(

    tabsetPanel(
    tabPanel(title = "Central Limit Theorem", 
      sliderInput(inputId = "num", label = "Choose a sample size", 
         value = 5, min = 1, max = 50), 
       plotOutput(outputId = "hist") 
    ) 
    , 
     tabPanel(title = "BMI prediction", 
       pageWithSidebar(
       # Application title 
       headerPanel("BMI prediction"), 

       sidebarPanel(
       numericInput('wt', 'Weight in Pounds', 90, min = 50, max = 200), 
       submitButton('Submit'), 
       numericInput('ht', 'Height in Inches', 60, min = 50, max = 85), 
       submitButton('Submit') 
       ), 
       mainPanel(
       h3('Results of prediction'), 
       h4('Your weight is'), 
       verbatimTextOutput("inputValue1"), 
       h4('Your height is'), 
       verbatimTextOutput("inputValue2"), 
       h4('Which resulted in a BMI of '), 
       verbatimTextOutput("prediction") 

       ) 
      ) 

     ) 

     ) 
    ) 
# Define server logic required to draw a histogram 
server <- function(input, output) { 
    output$hist <- renderPlot({ 
    par(mfrow = c(1,2)) 
    r <- 10000 
    samp <- matrix(rexp(n=(input$num)*r, rate = 2), nrow = r) 
    sample.means <- apply(samp,1,mean) 
    hist(sample.means,col="purple", main="Sampling Distribution of the Sample Mean", 
     xlab = "Sample Mean") 
    qqnorm(sample.means, col = "purple") 
    }) 
    bmi_calc <- function(weight, height) (weight/height^2)*703 
    output$inputValue1 <- renderPrint({input$wt}) 
    output$inputValue2 <- renderPrint({input$ht}) 
    output$prediction <- renderPrint({bmi_calc(input$wt, input$ht)}) 
    } 
    shinyApp(ui = ui, server = server) 

어떤 이유로 인해 첫 번째 탭의 히스토그램이 슬라이더에 응답하지 않습니다. 두 번째 탭을 포함하지 않으면 정상적으로 작동합니다.

답변

1

두 개의 submitButton입니다. 두 번째 탭에서 버튼을 클릭하고 첫 번째 탭으로 돌아 가면 해당 탭이 업데이트됩니다. 불행히도 어떤 입력이 submitButton으로 업데이트 될지를 지정하는 방법은 없다고 생각합니다 (현재 두 입력은 모두 세 입력 모두에서 작동하므로 두 번째 탭의 두 입력은 중복됩니다). 몇 가지 대안은, 그러나,이 있습니다

  • 는 소득 (일하지 않는 정말 자원 집약적되는) 모든 입력을 업데이트하는 각 탭에 submitButton를 넣습니다.
  • tabsetPanel 외부에 submitButton을 넣으십시오. 하나의 버튼이 작동하므로 3 개의 입력이 모두 업데이트됩니다.
  • actionButton을 사용하면 명시 적으로 원하는 값을 업데이트 할 수 있습니다. 이 방법을 사용하면 업데이트 된 내용을 정확하게 제어 할 수 있지만 코드 작업이 많이 필요합니다. 과도하게 업데이트하고 싶지 않은 많은 리소스를 필요로하는 위젯을 가지고 있다면 이것이 최선의 선택입니다.
  • 모든 버튼을 없애면 모든 것이 즉시 업데이트됩니다. 이 예제에서는 수행하는 작업이 리소스를 많이 사용하지 않아 사용자 반응이 더 빨라지므로 이것이 필자의 기본 설정입니다.
+0

굉장! 나는 각 탭에 업데이트 버튼을 포함 시켰기 때문에 사용자는 각 탭의 결과를 별도로 제어 할 수있는 느낌을 갖게되었습니다. 감사. – user3922546

관련 문제