I am running into an issue because observe is being called first before the UI loads.
我遇到了一个问题,因为在加载UI之前首先调用了observe。
Here is my ui.R
这是我的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 ....
Here is my Server.R
这是我的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))
})
This is my observe function and it runs before the input$Product and input$File1 are populated so I get an error because they are both NULL. But I need to use the input from the UI.
这是我的观察函数,它在输入$ Product和输入$ 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)
Any idea how to get the input$Product and input$File1 to be populated BEFORE observe runs?
知道如何在观察运行之前获取输入$ Product并输入$ File1以进行填充吗?
Thank you.
谢谢。
3 个解决方案
#1
10
You need to use the Shiny Event Handler and use observeEvent
instead of observe
. It seems to be about the only way to get rid of the "Unhandled error" message caused by NULL values on app startup. This is so because unlike observe
the event handler ignores NULL values by default.
您需要使用Shiny Event Handler并使用observeEvent而不是观察。它似乎是摆脱应用程序启动时由NULL值引起的“未处理错误”消息的唯一方法。这是因为不像observe,事件处理程序默认忽略NULL值。
So your observe function could end up looking something like this (no need for priorities, or resume/suspended etc!)
因此,您的观察功能可能最终看起来像这样(不需要优先级,或恢复/暂停等!)
observeEvent(input$Product, ({
max_plots<-length(getTerm2UI(input$Desk, input$Product, input$File1))
... (etc)
})# end of the function to be executed whenever input$Product changes
)
I could not copy paste your example code easily to make it run, so I'm not entirely sure what your full observe function would look like.
我无法轻松复制粘贴您的示例代码以使其运行,所以我不完全确定您的完整观察功能会是什么样子。
#2
8
The simplest way is to add an is.null(input$Product)
check at the top of each observe, to prevent it from running before the inputs it uses are initialized.
最简单的方法是在每次观察的顶部添加一个is.null(输入$ Product)检查,以防止它在初始化它使用的输入之前运行。
If you don't want your observers to do the null-check each time they're run, you can also use the suspended = TRUE
argument when registering them to prevent them from running; then write a separate observer that performs the check, and when it finds that all inputs are non-null, calls resume() on the suspended observers and suspends itself.
如果您不希望观察者在每次运行时都进行空检查,那么在注册它们时也可以使用suspended = TRUE参数来阻止它们运行;然后写一个执行检查的单独观察者,当它发现所有输入都是非空时,在挂起的观察者上调用resume()并暂停自身。
#3
0
We'd need an MRE to provide a working answer, but, assuming you need input$Product
and input$File1
, but do not want to take a dependency on them, only on input$Desk
, you could:
我们需要一个MRE来提供一个可行的答案,但是,假设你需要输入$ Product并输入$ File1,但不想依赖它们,只能在输入$ Desk上,你可以:
observe({
product <- isolate(input$Product)
file1 <- isolate(input$File1)
print("in observe")
print(product)
max_plots<-length(getTerm2UI(input$Desk, product, file1))
for (i in 1:max_plots ) {
# ...
}
})
this is probably effectively equivalent to an observeEvent(input$Desk, ....)
, but might offer more flexibility.
这可能实际上相当于observeEvent(输入$ Desk,....),但可能提供更多的灵活性。
#1
10
You need to use the Shiny Event Handler and use observeEvent
instead of observe
. It seems to be about the only way to get rid of the "Unhandled error" message caused by NULL values on app startup. This is so because unlike observe
the event handler ignores NULL values by default.
您需要使用Shiny Event Handler并使用observeEvent而不是观察。它似乎是摆脱应用程序启动时由NULL值引起的“未处理错误”消息的唯一方法。这是因为不像observe,事件处理程序默认忽略NULL值。
So your observe function could end up looking something like this (no need for priorities, or resume/suspended etc!)
因此,您的观察功能可能最终看起来像这样(不需要优先级,或恢复/暂停等!)
observeEvent(input$Product, ({
max_plots<-length(getTerm2UI(input$Desk, input$Product, input$File1))
... (etc)
})# end of the function to be executed whenever input$Product changes
)
I could not copy paste your example code easily to make it run, so I'm not entirely sure what your full observe function would look like.
我无法轻松复制粘贴您的示例代码以使其运行,所以我不完全确定您的完整观察功能会是什么样子。
#2
8
The simplest way is to add an is.null(input$Product)
check at the top of each observe, to prevent it from running before the inputs it uses are initialized.
最简单的方法是在每次观察的顶部添加一个is.null(输入$ Product)检查,以防止它在初始化它使用的输入之前运行。
If you don't want your observers to do the null-check each time they're run, you can also use the suspended = TRUE
argument when registering them to prevent them from running; then write a separate observer that performs the check, and when it finds that all inputs are non-null, calls resume() on the suspended observers and suspends itself.
如果您不希望观察者在每次运行时都进行空检查,那么在注册它们时也可以使用suspended = TRUE参数来阻止它们运行;然后写一个执行检查的单独观察者,当它发现所有输入都是非空时,在挂起的观察者上调用resume()并暂停自身。
#3
0
We'd need an MRE to provide a working answer, but, assuming you need input$Product
and input$File1
, but do not want to take a dependency on them, only on input$Desk
, you could:
我们需要一个MRE来提供一个可行的答案,但是,假设你需要输入$ Product并输入$ File1,但不想依赖它们,只能在输入$ Desk上,你可以:
observe({
product <- isolate(input$Product)
file1 <- isolate(input$File1)
print("in observe")
print(product)
max_plots<-length(getTerm2UI(input$Desk, product, file1))
for (i in 1:max_plots ) {
# ...
}
})
this is probably effectively equivalent to an observeEvent(input$Desk, ....)
, but might offer more flexibility.
这可能实际上相当于observeEvent(输入$ Desk,....),但可能提供更多的灵活性。