我正在尝试在Shiny中建立血压评估工具,该工具将提供与人的血压数据相关的指导。
这是我的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 :(得分:0)
你的问题是,你没有定义一个正确的函数来找到一个BP的状态。
以下功能根据两个输入数字为您提供输出。我不确定,如果完全正确的话。从这个意义上说,您的代码不是很明确:有systolic == 130
和diastolic == 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.R
和server.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))
})
})
}
)