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

use*_*875 14 r shiny

我遇到了一个问题因为在加载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  ....
Run Code Online (Sandbox Code Playgroud)

这是我的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))
  })
Run Code Online (Sandbox Code Playgroud)

这是我的观察功能,它在输入$ 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)
Run Code Online (Sandbox Code Playgroud)

知道如何在观察运行之前获取输入$ Product并输入$ File1以进行填充吗?

谢谢.

Fad*_*way 11

最简单的方法是is.null(input$Product)在每次观察的顶部添加一个检查,以防止它在初始化它使用的输入之前运行.

如果您不希望观察者在每次运行时都进行空检查,您也可以suspended = TRUE在注册时使用该参数以防止它们运行; 然后写一个执行检查的单独观察者,当它发现所有输入都是非空时,在挂起的观察者上调用resume()并暂停自身.


FvD*_*FvD 10

您需要使用Shiny Event HandlerobserveEvent不是使用observe.它似乎是摆脱应用程序启动时由NULL值引起的"未处理错误"消息的唯一方法.这是因为与observe事件处理程序不同,默认情况下会忽略NULL值.

因此,您的观察功能可能最终看起来像这样(不需要优先级,或恢复/暂停等!)

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
 )
Run Code Online (Sandbox Code Playgroud)

我无法轻松复制粘贴您的示例代码以使其运行,所以我不完全确定您的完整观察功能会是什么样子.


TCl*_*lle 6

您可以使用req()“规定”之前的反应表达执行输入,按闪亮的文档在这里:https://shiny.rstudio.com/articles/req.html和函数文档浏览:HTTPS://shiny.rstudio .com/reference/shiny/latest/req.html

例如

observeEvent({
  req(input$Product)
  req(input$File1)
  # ...
})
Run Code Online (Sandbox Code Playgroud)