When dashboard is present inside action-button it is showing result when button is pressed twice

白昼怎懂夜的黑 提交于 2019-12-11 18:22:22

问题


In my code when i press load scenario button it is showing the dashboard page when load scenario button is pressed twice.I want my dashboard to appear in a single click.Can anyone help.Here is my code

 library("shiny")
 library("shinydashboard")
 ui <- fluidPage(


  actionButton("create_scenario", "Create Scenario"),
  actionButton("load_scenario","load scenario"),
 uiOutput("input"),
  uiOutput("inputs")
 )

 server <- function(input, output,session) {

  observeEvent(input$create_scenario,{

    output$input <- renderUI({
     textInput("txtInput","Enter Scenario Name","Enter name as scenario         
              (number of senario created +1)")
    })


   })

  observeEvent(input$load_scenario,{

    output$inputs <- renderUI({
     # textInput("txtInput","Enter Scenario Name","Enter name as scenario  
     #(number of senario created +1)")
      dashboardPage(
       dashboardHeader(title = "Basic dashboard"),
       dashboardSidebar(),
      dashboardBody(
      # Boxes need to be put in a row (or column)
      fluidRow(
        box(plotOutput("plot1", height = 250)),

        box(
          title = "Controls",
          sliderInput("slider", "Number of observations:", 1, 100, 50)
        ))))


} )

histdata <- rnorm(500)
output$plot1 <- renderPlot({
  data <- histdata[seq_len(input$slider)]
  hist(data)
    })


  })
}
shinyApp(ui, server)

Please ignore the create scenario button.One more help which is off topic can i add some colour to dashboard as it is not very attractive.


回答1:


You'll need to set ignoreNULL = FALSE for your observeEvent:

library("shiny")
library("shinydashboard")

ui <- fluidPage(
  actionButton("create_scenario", "Create Scenario"),
  actionButton("load_scenario","load scenario"),
  uiOutput("input"),
  uiOutput("inputs")
)

server <- function(input, output,session) {

  observeEvent(input$create_scenario, {
    output$input <- renderUI({
      textInput("txtInput","Enter Scenario Name","Enter name as scenario         
              (number of senario created +1)")
    })
  })

  observeEvent(input$load_scenario, {

    output$inputs <- renderUI({
      # textInput("txtInput","Enter Scenario Name","Enter name as scenario  
      #(number of senario created +1)")
      dashboardPage(
        dashboardHeader(title = "Basic dashboard"),
        dashboardSidebar(),
        dashboardBody(
          # Boxes need to be put in a row (or column)
          fluidRow(
            box(plotOutput("plot1", height = 250)),

            box(
              title = "Controls",
              sliderInput("slider", "Number of observations:", 1, 100, 50)
            ))))
    })

    histdata <- rnorm(500)
    output$plot1 <- renderPlot({
      data <- histdata[seq_len(input$slider)]
      hist(data)
    })

  }, ignoreNULL = FALSE)
}

shinyApp(ui, server)


来源:https://stackoverflow.com/questions/56118973/when-dashboard-is-present-inside-action-button-it-is-showing-result-when-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!