ShinyApp 错误:找不到函数
Error in ShinyApp: Could not find function
这是一个 ShinyApp,可根据用户输入输出图表和 table。一旦用户单击 "go",值就会更新。
我不确定是对每个变量使用 "eventReactive",例如 pt_class、arm、freq 等。还是在开始时使用 "observeEvent" 来捕捉 "go"一次。我选择了后者,但我无法理解为什么会出现此错误:
Warning: Error in freq: could not find function "freq"
72: observeEventHandler [~/ShinyApp/app.R#108]
1: runApp
正如您在下面的代码中看到的,我已经定义了 "freq"...
提前致歉,因为该应用程序依赖于其他 python 脚本,但如果需要它们来诊断问题,我很乐意分享它们。
ui <- navbarPage(numericInput(inputId = "vl",
"Viral load threshold (copies/mL):", value = 1000, min=50, max=10000000),
radioButtons("one_or_duration", "Duration at or above viral load threshold:",
c("A single measurement","Multiple measurements")),
radioButtons("pt_class", "Time of treatment:",
c("All study participants","Early treated participants","Chronic treated participants")),
br(),
radioButtons("nnrti", "Include participants on NNRTIs?",
c("No, exclude participants on NNRTIs","Yes, include participants on NNRTIs")),
radioButtons("freq", "Expected frequency of post-treatment controllers:",
c("Same frequency as observed by authors","Input expected frequency")),
actionButton(inputId="go",label="Go!"),
mainPanel(
plotOutput(outputId = "graph"), DT::dataTableOutput(outputId="table")))
server <- function(input, output,session) {
observeEvent(input$go,{
one_or_duration <-
if(input$one_or_duration == "A single measurement"){
"single"}
else if(input$one_or_duration == "Multiple measurements"){
"multiple"}
nnrti <-
if(input$nnrti == "Yes, include participants on NNRTIs"){
"yes"}
else if(input$nnrti == "No, exclude participants on NNRTIs"){
"no"}
freq <-
if(input$freq == "Same frequency as observed by authors"){
"same"}
else if(input$freq == "Input expected frequency"){
"diff"}
pt_class <-
if(input$pt_class == "All study participants"){
"all"}
else if(input$pt_class == "Early treated participants"){
"early"}
else if(input$pt_class == "Chronic treated participants"){
"chronic"}
ptcs_plus_ncs <-
if (freq() == "same"){do_this
}
})}
shinyApp(ui = ui, server = server)
在目前的表格中,我无法运行你的代码,所以我无法深入研究问题。
然而,乍一看,R 会遇到上述错误,因为您没有将 freq
定义为反应式,但仍将其称为 freq()
,这导致 R 查找名为 freq
。这同样适用于一堆其他变量,例如 pt_class()
、one_or_duration()
、nnrti()
、arm()
等
带有 observeEvent(input$go,{
的部分有点问题,您似乎不需要 if-clauses。而是在 UI 输入值中使用命名向量。例如代替
radioButtons("one_or_duration", "Duration at or above viral load threshold:",
c("A single measurement","Multiple measurements"))
写
radioButtons("one_or_duration", "Duration at or above viral load threshold:",
c("A single measurement" = "single",
"Multiple measurements" = "multiple"))
然后你就不需要说
的部分了
one_or_duration <-
if(input$one_or_duration == "A single measurement"){
"single"}
else if(input$one_or_duration == "Multiple measurements"){
"multiple"}
之后,不再使用 one_or_duration()
(这不是反应式,不能通过添加括号来调用),只需使用 input$one_or_duration。顺便说一句,你不需要让输入变量成为反应性的,因为它们已经是反应性的了。
这绝对不能解决您代码的所有问题,但它可能是一个开始。
这是一个 ShinyApp,可根据用户输入输出图表和 table。一旦用户单击 "go",值就会更新。
我不确定是对每个变量使用 "eventReactive",例如 pt_class、arm、freq 等。还是在开始时使用 "observeEvent" 来捕捉 "go"一次。我选择了后者,但我无法理解为什么会出现此错误:
Warning: Error in freq: could not find function "freq"
72: observeEventHandler [~/ShinyApp/app.R#108]
1: runApp
正如您在下面的代码中看到的,我已经定义了 "freq"... 提前致歉,因为该应用程序依赖于其他 python 脚本,但如果需要它们来诊断问题,我很乐意分享它们。
ui <- navbarPage(numericInput(inputId = "vl",
"Viral load threshold (copies/mL):", value = 1000, min=50, max=10000000),
radioButtons("one_or_duration", "Duration at or above viral load threshold:",
c("A single measurement","Multiple measurements")),
radioButtons("pt_class", "Time of treatment:",
c("All study participants","Early treated participants","Chronic treated participants")),
br(),
radioButtons("nnrti", "Include participants on NNRTIs?",
c("No, exclude participants on NNRTIs","Yes, include participants on NNRTIs")),
radioButtons("freq", "Expected frequency of post-treatment controllers:",
c("Same frequency as observed by authors","Input expected frequency")),
actionButton(inputId="go",label="Go!"),
mainPanel(
plotOutput(outputId = "graph"), DT::dataTableOutput(outputId="table")))
server <- function(input, output,session) {
observeEvent(input$go,{
one_or_duration <-
if(input$one_or_duration == "A single measurement"){
"single"}
else if(input$one_or_duration == "Multiple measurements"){
"multiple"}
nnrti <-
if(input$nnrti == "Yes, include participants on NNRTIs"){
"yes"}
else if(input$nnrti == "No, exclude participants on NNRTIs"){
"no"}
freq <-
if(input$freq == "Same frequency as observed by authors"){
"same"}
else if(input$freq == "Input expected frequency"){
"diff"}
pt_class <-
if(input$pt_class == "All study participants"){
"all"}
else if(input$pt_class == "Early treated participants"){
"early"}
else if(input$pt_class == "Chronic treated participants"){
"chronic"}
ptcs_plus_ncs <-
if (freq() == "same"){do_this
}
})}
shinyApp(ui = ui, server = server)
在目前的表格中,我无法运行你的代码,所以我无法深入研究问题。
然而,乍一看,R 会遇到上述错误,因为您没有将 freq
定义为反应式,但仍将其称为 freq()
,这导致 R 查找名为 freq
。这同样适用于一堆其他变量,例如 pt_class()
、one_or_duration()
、nnrti()
、arm()
等
带有 observeEvent(input$go,{
的部分有点问题,您似乎不需要 if-clauses。而是在 UI 输入值中使用命名向量。例如代替
radioButtons("one_or_duration", "Duration at or above viral load threshold:",
c("A single measurement","Multiple measurements"))
写
radioButtons("one_or_duration", "Duration at or above viral load threshold:",
c("A single measurement" = "single",
"Multiple measurements" = "multiple"))
然后你就不需要说
的部分了one_or_duration <-
if(input$one_or_duration == "A single measurement"){
"single"}
else if(input$one_or_duration == "Multiple measurements"){
"multiple"}
之后,不再使用 one_or_duration()
(这不是反应式,不能通过添加括号来调用),只需使用 input$one_or_duration。顺便说一句,你不需要让输入变量成为反应性的,因为它们已经是反应性的了。
这绝对不能解决您代码的所有问题,但它可能是一个开始。