当我尝试更新函数的输入变量时如何解决 Shiny 的反应性问题

How to fix reactivity issues with Shiny when I try to update the input variables of a function

我创建了一个贝叶斯信念网络,我想使用 Shiny 使其可用,这样任何人都可以通过更改一些变量来使用它。 但是,当我修改输入时,闪亮的应用程序无法更新我的功能。并且输出主面板总是给出 0。我需要如何修改我的功能才能对新输入做出反应?我已经尝试改进其他答案来解决这个问题,但我无法确定问题所在。

我知道 BBN 可以工作,如果我放置一个无功输入而不是功能,主面板会正确更新。 此外,如果函数有一些默认值而不是反应变量,则答案会正确显示在主面板上。这是我使用的代码的迭代,我认为它更接近正确的结果

library(bnlearn)
library(shiny)

基本的 BBN 设计,保存为脚本,但只是为了简单起见。

net <- model2network("[藻类][有机][耗氧量|藻类:有机]") 情节(净)

dg <- c("high", "Mid", "low")
oo=c("good", "fluctuating","hypo")
ss=c("hyper", "marine","hypo","fresh")
ff=c("costant", "seasonal")
cptAlgae = matrix(c(0.33, 0.33,0.34), ncol=3, dimnames=list(NULL, dg))
cptOrganic = matrix(c(0.33, 0.33,0.34), ncol=3, dimnames=list(NULL, dg))

cptOxygenConsumption = c(1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1) 

dim(cptOxygenConsumption) = c(3, 3,3)
dimnames(cptOxygenConsumption) = list("OxygenConsumption"=dg, "Organic"=dg, "Algae"=dg)

net.disc <- custom.fit(net, dist=list(Algae=cptAlgae, Organic=cptOrganic, OxygenConsumption=cptOxygenConsumption))

Shiny App 从这里开始

ui <- fluidPage(
  titlePanel("Test BBN"),

  sidebarLayout(
    sidebarPanel(
      helpText("Enter the pool characteristics"),

      selectInput( inputId ="Algae",
                  label= "Choose Algae Cover",
                  choices = dg,
                  selected = "high"),
      selectInput(inputId = "Organic",
                  label = "Choose Organic matter cover",
                  choices = dg,
                  selected = "high")),
     mainPanel(
                    textOutput("habitability"))
    )
    )
    server <- function(input, output, session) {

      a= reactive({
        input$Algae

      })
      b=reactive({
        input$Organic
      })


      C1= reactive({cpquery(net.disc, (OxygenConsumption =="low"), Algae=="a()" & Organic== "b()")

      })






      output$habitability = reactive({print(C1())})

    }


    shinyApp(ui = ui, server = server)

我希望输出类似于 0 和 1 之间的值,基于 2 个输入(如果两者都为 "low",则应为 1;如果均为 "high",则应为 0。

我设法构建了查询并对其进行了评估。也许这会帮助您入门。

library(bnlearn)
library(shiny)

net <- model2network("[Algae][Organic][OxygenConsumption|Algae:Organic]")
plot(net)

dg <- c("high", "mid", "low")
oo <- c("good", "fluctuating", "hypo")
ss <- c("hyper", "marine", "hypo", "fresh")
ff <- c("costant", "seasonal")
cptAlgae <- matrix(c(0.33, 0.33, 0.34), ncol = 3, dimnames = list(NULL, dg))
cptOrganic <- matrix(c(0.33, 0.33, 0.34), ncol = 3, dimnames = list(NULL, dg))
cptOxygenConsumption <- c(1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1)

dim(cptOxygenConsumption) <- c(3, 3, 3)
dimnames(cptOxygenConsumption) <- list("OxygenConsumption" = dg, "Organic" = dg, "Algae" = dg)

net.disc <- custom.fit(net, dist = list(Algae = cptAlgae, Organic = cptOrganic, OxygenConsumption = cptOxygenConsumption))

cpquery(net.disc, (OxygenConsumption == "low"), Algae == "low" & Organic == "low") # ?

ui <- fluidPage(
  titlePanel("Test BBN"),
  sidebarLayout(
    sidebarPanel(
      helpText("Enter the pool characteristics"),
      selectInput(
        inputId = "Algae",
        label = "Choose Algae Cover",
        choices = dg,
        selected = "high"
      ),
      selectInput(
        inputId = "Organic",
        label = "Choose Organic matter cover",
        choices = dg,
        selected = "high"
      )
    ),
    mainPanel(
      textOutput("habitability")
    )
  )
)

server <- function(input, output, session) {

  output$habitability <- renderText({
    req(input$Algae, input$Organic)
    text <- paste0("cpquery(net.disc, (OxygenConsumption == 'low'), Algae == '", input$Algae, "' & Organic == '", input$Organic, "')")
    eval(parse(text=text))
  })

}

shinyApp(ui = ui, server = server)