2015-09-26 50 views
1

我正在嘗試在Shiny中構建一個血壓評估工具,該工具將提供有關人員BP數量的指導。Shiny中的BP評估工具無法找到函數「範圍」

這裏是我的ui.R
庫(閃亮)

shinyUI(fluidPage(
    titlePanel("Blood pressure assessment"), 

    fluidRow(
      column(3, 
        h3("Systolic BP"), 
        h4("The first/top number"), 
        sliderInput("x1", label = "mm Hg", min = 90, max = 200, 
           value = 90, animate = TRUE)), 
      column(3, 
        h3("Diastolic BP"), 
        h4("The second/bottom number"), 
        sliderInput("x2", label = "mm HG", min = 50, max = 120, 
           value = 50, animate = TRUE))), 
      column(3, 
        h4("Your range"), 
        verbatimTextOutput("ranges")), 
      column(3, 
        br(), 
        actionButton("submit", "Submit")))) 

這裏是我的server.R文件

shinyServer(function(input, output) { 
    function(ranges) { reactiveValues(normal = "Normal Range", 
          caution = "Caution Range = Prehypertension", 
          high = "High Range = Stage 1 Hypertension", 
          very = "Very High Range = Stage 2 Hypertension")} 
    dataInput <- reactive({ 
    if(input$x1 > 160){return()} else{ 
      if(input$x2 > 100){return("very")} 
    } 

    if(input$x1 == 140:159){return()} else{ 
      if(input$x2 == 90:99){return("high")} 
    } 

    if(input$x1 == 120:139){return()} else{ 
      if(input$x2 == 80:89){return("caution")} 
    } 
    if(input$x1 < 120){return()} else{ 
      if(input$x2 > 80) {return("normal")} 
    } 
    }) 

    observeEvent(input$submit, { 

      output$ranges <- renderPrint({ranges(input$x1, input$x2)}) 

      }) 
} 
    ) 

說回來,當我嘗試運行應用程序的響應是

Error in func() : could not find function "ranges" 

任何想什麼我做錯了。我懷疑自己做得比自己需要更復雜,或者我錯過了一些非常明顯的事情。這是我第一個閃亮的應用程序。

+0

Insi你試圖使用函數'ranges'來渲染'renderPrint'。似乎你沒有正確地定義它。 –

回答

0

這裏你的問題是,你沒有定義一個適當的函數來查找一個人的血壓狀態。

以下函數爲您提供依賴於兩個輸入數字的輸出。我不確定,但如果它是完全正確的。你的代碼在這個意義上不是很清楚:有一個人systolic == 130diastolic == 70"Prehypertension",還是一無所有?

我定義了下面的函數,如下所示:如果兩個值分別低於120和80,則返回"Normal Range"。如果兩個值分別大於160或100,則返回"Very High Range = Stage 2 Hypertension"。如果兩個值分別在121和139之間或80和89之間,則返回"Prehypertension",等等。這是怎麼回事?

# define function which gives finds blood pressure state 
find_bp_state <- function(systolic, diastolic) { 
    if (systolic < 120 & diastolic < 80) { 
    return("Normal Range") 
    } else if (systolic > 160 | diastolic > 100) { 
    return("Very High Range = Stage 2 Hypertension") 
    } else if (systolic %in% c(121:139) | diastolic %in% c(80:89)) { 
    return("Caution Range = Prehypertension") 
    } else if (systolic %in% c(140:159) | diastolic %in% c(90:99)) { 
    return("High Range = Stage 1 Hypertension") 
    } 
} 

# test function 
find_bp_state(130, 82) 
#> [1] "Caution Range = Prehypertension" 

之後,你可以創建你的應用程序,它會給你預期的結果,這取決於你如何定義你的功能。

出於演示目的,以下代碼在單個文件中起作用。

library(shiny) 

find_bp_state <- function(systolic, diastolic) { 
    if (systolic < 120 & diastolic < 80) { 
    return("Normal Range") 
    } else if (systolic > 160 | diastolic > 100) { 
    return("Very High Range = Stage 2 Hypertension") 
    } else if (systolic %in% c(121:139) | diastolic %in% c(80:89)) { 
    return("Caution Range = Prehypertension") 
    } else if (systolic %in% c(140:159) | diastolic %in% c(90:99)) { 
    return("High Range = Stage 1 Hypertension") 
    } 
} 

ui <- fluidPage(
    titlePanel("Blood pressure assessment"), 

    fluidRow(
    column(3, 
      h3("Systolic BP"), 
      h4("The first/top number"), 
      sliderInput("x1", label = "mm Hg", min = 90, max = 200, 
         value = 90, animate = TRUE)), 
    column(3, 
      h3("Diastolic BP"), 
      h4("The second/bottom number"), 
      sliderInput("x2", label = "mm HG", min = 50, max = 120, 
         value = 50, animate = TRUE))), 
    column(3, 
     h4("Your range"), 
     verbatimTextOutput("ranges")), 
    column(3, 
     br(), 
     actionButton("submit", "Submit"))) 

server <- function(input, output) { 

    observeEvent(input$submit, { 
    output$ranges <- renderPrint({ 
     systolic <- input$x1 
     diastolic <- input$x2 

     print(find_bp_state(systolic = systolic, diastolic = diastolic)) 
    }) 
    }) 

    } 

shinyApp(server = server, ui = ui) 

如果你想有一個ui.Rserver.R,你的代碼可能如下:

ui.R

library(shiny) 
shinyUI(fluidPage(
    titlePanel("Blood pressure assessment"), 

    fluidRow(
    column(3, 
      h3("Systolic BP"), 
      h4("The first/top number"), 
      sliderInput("x1", label = "mm Hg", min = 90, max = 200, 
         value = 90, animate = TRUE)), 
    column(3, 
      h3("Diastolic BP"), 
      h4("The second/bottom number"), 
      sliderInput("x2", label = "mm HG", min = 50, max = 120, 
         value = 50, animate = TRUE))), 
    column(3, 
     h4("Your range"), 
     verbatimTextOutput("ranges")), 
    column(3, 
     br(), 
     actionButton("submit", "Submit") 
) 
)) 

server.R

library(shiny) 

find_bp_state <- function(systolic, diastolic) { 
    if (systolic < 120 & diastolic < 80) { 
    return("Normal Range") 
    } else if (systolic > 160 | diastolic > 100) { 
    return("Very High Range = Stage 2 Hypertension") 
    } else if (systolic %in% c(121:139) | diastolic %in% c(80:89)) { 
    return("Caution Range = Prehypertension") 
    } else if (systolic %in% c(140:159) | diastolic %in% c(90:99)) { 
    return("High Range = Stage 1 Hypertension") 
    } 
} 

shinyServer(function(input, output) { 

    observeEvent(input$submit, { 
     output$ranges <- renderPrint({ 
     systolic <- input$x1 
     diastolic <- input$x2 

     print(find_bp_state(systolic = systolic, diastolic = diastolic)) 
     }) 
    }) 
    } 
) 
+1

這很完美!非常感謝你。我絕對讓事情太複雜。非常感謝! – ldlpdx

+0

@LesleyLathrop你可以通過接受答案來顯示你的感謝;) –

+0

我會很樂意這樣做,但我不知道如何。我在這裏是一個新手。 :)我確實嘗試了upvote,但我不知道要去哪裏注意問題已經解決。 – ldlpdx