【问题标题】:How to create On load Event or default event in shiny?如何在闪亮中创建加载事件或默认事件?
【发布时间】:2018-07-31 08:25:57
【问题描述】:

我是 Shiny 和 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)

现在,如果您运行此应用程序,您会看到在加载后的第一个屏幕上选择了输入(这是所需的),但仅在单击“提交”按钮后才显示可视化,这是因为观察者事件。 在实际应用中,点击按钮捕获输入后进行计算。因此,只有在点击 actionButton 时才会触发计算

有没有办法,我们仍然可以保留“提交”按钮及其观察事件,但在开始时即在第一次加载应用程序时自动触发可视化或观察事件中执行的操作.

【问题讨论】:

    标签: r shiny shinydashboard shinyjs


    【解决方案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 参数,但由于某种原因,它不会产生相同的结果。

    【讨论】:

    • 先生,感谢您的回答。澄清一下,我使用的是观察事件,因为一旦捕获输入,就会执行一些计算,然后产生绘图所需的参数。但我理解您的观点,并将尝试使用该方法并发布结果。
    猜你喜欢
    • 2019-06-11
    • 1970-01-01
    • 2021-02-20
    • 1970-01-01
    • 2020-05-11
    • 1970-01-01
    • 2017-04-10
    • 1970-01-01
    相关资源
    最近更新 更多