gpt4 book ai didi

R Shiny 在加载 UI 之前观察运行,这会导致 Null 参数

转载 作者:行者123 更新时间:2023-12-03 21:20:56 29 4
gpt4 key购买 nike

我遇到了一个问题,因为在 UI 加载之前先调用了observe。

这是我的 ui.R

  sidebarPanel(
selectInput("Desk", "Desk:" , as.matrix(getDesksUI())),
uiOutput("choose_Product"), #this is dynamically created UI
uiOutput("choose_File1"), #this is dynamically created UI
uiOutput("choose_Term1"), #this is dynamically created UI ....

这是我的 Server.R
shinyServer(function(input, output,session) {

#this is dynamic UI
output$choose_Product <- renderUI({
selectInput("Product", "Product:", as.list(getProductUI(input$Desk)))
})

#this is dynamic UI
output$choose_File1 <- renderUI({
selectInput("File1", "File 1:", as.list(getFileUI(input$Desk, input$Product)))
})

#this is dynamic UI and I want it to run before the Observe function so the call
# to getTerm1UI(input$Desk, input$Product, input$File1) has non-null parameters
output$choose_Term1 <- renderUI({
print("Rendering UI for TERM")
print(paste(input$Desk," ", input$Product, " ", input$File1,sep=""))
selectInput("Term1", "Term:", getTerm1UI(input$Desk, input$Product, input$File1))
})

这是我的观察函数,它在 input$Product 和 input$File1 被填充之前运行,所以我得到一个错误,因为它们都是 NULL。但我需要使用来自 UI 的输入。
   observe({ 
print("in observe")
print(input$Product)
max_plots<-length(getTerm2UI(input$Desk, input$Product, input$File1))
#max_plots<-5
# Call renderPlot for each one. Plots are only actually generated when they
# are visible on the web page.
for (i in 1:max_plots ) {
# Need local so that each item gets its own number. Without it, the value
# of i in the renderPlot() will be the same across all instances, because
# of when the expression is evaluated.
local({
my_i <- i
plotname <- paste("plot", my_i, sep="")
output[[plotname]] <- renderPlot({
plot(1:my_i, 1:my_i,
xlim = c(1, max_plots ),
ylim = c(1, max_plots ),
main = paste("1:", my_i, ". n is ", input$n, sep = "") )
})
})
}##### End FoR Loop
},priority = -1000)

知道如何在观察运行之前填充 input$Product 和 input$File1 吗?

谢谢你。

最佳答案

编辑:向下滚动到 TClavelle 的答案以获得更好的解决方案。虽然这个答案获得了最多的支持,但我是在 Shiny 的功能比现在少的时候写的。
最简单的方法是添加一个 is.null(input$Product)检查每个观察的顶部,以防止它在它使用的输入被初始化之前运行。
如果你不想你的观察者每次运行时都做空检查,你也可以使用 suspended = TRUE注册它们以防止它们运行时的参数;然后编写一个单独的观察者来执行检查,当它发现所有输入都非空时,对挂起的观察者调用 resume() 并挂起自己。

关于R Shiny 在加载 UI 之前观察运行,这会导致 Null 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22845874/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com