这是一个闪亮的应用程序,可根据用户输入输出图表和表格。一旦用户单击“开始”,值就会更新。
我犹豫不决是对每个变量使用“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 子句。而是在 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 。顺便说一句,您不需要使输入变量具有反应性,因为它们已经是反应性的。
这肯定不能解决代码的所有问题,但它可能是一个开始。