2015-09-26 25 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