首页 文章

如何在闪亮中创建On load Event或default事件?

提问于
浏览
1

我是一个新的闪亮和stackoverflow,并寻找一些我目前坚持的问题的帮助 . 我正在尝试构建一个闪亮的应用程序,它收集用户的一些输入,并根据点击按钮的输入创建可视化 . 目前,这工作正常,但主要问题之一是,当应用程序第一次加载时,它应具有基于默认输入准备的可视化 .

我正在粘贴一个示例代码,它可以解释我面临的问题:

UI.R

#loading shiny
  library(shiny)

  ui<-shinyUI(fluidPage(
    titlePanel("Iris Dataset"),
    sidebarLayout(
      sidebarPanel(
        radioButtons("x", "Select X-axis:",
                     list("Sepal.Length"='a', "Sepal.Width"='b', "Petal.Length"='c', "Petal.Width"='d')),
        radioButtons("y", "Select Y-axis:",
                     list("Sepal.Length"='e', "Sepal.Width"='f', "Petal.Length"='g', "Petal.Width"='h')),
        actionButton("action","Submit")
      ),
      mainPanel(
        plotOutput("distPlot")
      )
    )
  ))

  #SERVER.R
  library(shiny)

  #loading shiny
  server<-shinyServer(function(input, output) {

    observeEvent(input$action,{
    output$distPlot <- renderPlot({if(input$x=='a'){i<-1}

      if(input$x=='b'){i<-2}

      if(input$x=='c'){i<-3}

      if(input$x=='d'){i<-4}

      if(input$y=='e'){j<-1}

      if(input$y=='f'){j<-2}

      if(input$y=='g'){j<-3}

      if(input$y=='h'){j<-4}

      s    <- iris[, i]
      k    <- iris[, j]
      plot(s,k)
    })
  })
  })


  shinyApp(ui<-ui, server<-server)

现在,如果您运行此应用程序,您将看到在加载后第一个屏幕上选择了输入(这是所需的),但只有在单击"Submit"按钮后才会显示可视化,这是因为观察者事件 . In the actual app, there are calculation being performed after capturing the inputs at the click of the button. Hence, the calculation triggers only when the actionButton is clicked

Is there a way, we can still keep the "Submit" button and its observe event, but automatically trigger the visualization or the action performed within the observe event at the start i.e. when the app loads for the first time.

1 回答

  • 1

    observeEvent 中使用 renderPlot 或其他渲染函数被认为是不好的做法 . 您正在寻找的是一个表示绘图参数的反应对象 . 然后,您可以在 renderPlot 上下文中访问此反应对象 . 这是一个例子

    library(shiny)
    
    ui <- shinyUI(fluidPage(
      titlePanel("Iris Dataset"),
      sidebarLayout(
        sidebarPanel(
          radioButtons("x", "Select X-axis:",
                       list("Sepal.Length" = 'a', "Sepal.Width" = 'b',
                            "Petal.Length" = 'c', "Petal.Width" = 'd')),
          radioButtons("y", "Select Y-axis:",
                       list("Sepal.Length" = 'e', "Sepal.Width" = 'f', 
                            "Petal.Length" = 'g', "Petal.Width" = 'h')),
          actionButton("action", "Submit")
        ),
        mainPanel(
          plotOutput("distPlot")
        )
      )
    ))
    
    server <- shinyServer(function(input, output) {
    
      user_choice <- eventReactive(input$action,{
        if (input$x == 'a'){i <- 1}    
        if (input$x == 'b'){i <- 2}    
        if (input$x == 'c'){i <- 3}    
        if (input$x == 'd'){i <- 4}    
        if (input$y == 'e'){j <- 1}    
        if (input$y == 'f'){j <- 2}    
        if (input$y == 'g'){j <- 3}    
        if (input$y == 'h'){j <- 4}
    
        return(c(i, j))
    
        ## use ignoreNULL to fire the event at startup
      }, ignoreNULL = FALSE)
    
      output$distPlot <- renderPlot({
        uc <- user_choice()
        plot(iris[, uc[1]], iris[, uc[2]])
      })
    })
    
    
    shinyApp(ui = ui, server = server)
    

    只要用户单击按钮或应用程序启动,对象 user_choice 就会更新 .

    observeEvent 中还有 ignoreNULL 参数但由于某种原因,这不会产生相同的结果 .

相关问题