ShinyApp中的错误:找不到功能

时间:2019-05-21 19:34:03

标签: r shinyapps

这是一个ShinyApp,可根据用户输入输出图形和表格。用户单击“开始”后,值将更新。

我不确定在为每个变量(例如pt_class,arm,freq等)使用“ eventReactive”还是在开始时使用“ observeEvent”捕获一次“ go”的点击之间。我选择了后者,但无法理解为什么会发生此错误:

Warning: Error in freq: could not find function "freq"
  72: observeEventHandler [~/ShinyApp/app.R#108]
   1: runApp 

您可以在下面的代码中看到,我已经定义了“频率” ... 由于该应用程序依赖于其他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)

1 个答案:

答案 0 :(得分:1)

在当前形式下,我无法运行您的代码,因此无法更深入地研究问题。

但是,乍看之下,R直通上述错误,因为您没有将freq定义为反应式,但仍将其称为freq(),这导致R查找名为{{1 }}。一堆其他变量,例如freqpt_class()one_or_duration()nnrti()等也是如此。

带有arm()的那部分是有问题的,似乎您不需要if子句。而是在UI输入值中使用命名向量。例如代替

observeEvent(input$go,{

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"} (它不是响应式的,不能通过添加方括号来调用),而只需使用input $ one_or_duration。顺便说一下,您不需要使输入变量具有反应性,因为它们已经存在。

这肯定不能解决您所有代码的问题,但这可能只是一个开始。