如何在SHINY中创建加载事件或默认事件? [英] How to create On load Event or default event in shiny?

查看:0
本文介绍了如何在SHINY中创建加载事件或默认事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Slight和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)
现在,如果您运行此应用程序,您将看到输入在加载后的第一个屏幕上被选中(这是所需的),但可视化只有在单击提交按钮后才会出现,这是因为观察者事件。在实际的APP中,会在点击按钮时捕获输入后进行计算。因此,只有在单击actionButton时才会触发计算

有没有一种方法,我们仍然可以保留提交按钮及其观察事件,但在启动时(即应用程序首次加载时)自动触发观察事件中执行的可视化或操作。

推荐答案

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参数,但由于某些原因,这不会产生相同的结果。

这篇关于如何在SHINY中创建加载事件或默认事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆