简体   繁体   English

R shiny - 复选框和操作按钮组合问题

[英]R shiny - checkboxes and action button combination issue

I have 2 checkboxes and 1 action button.我有 2 个复选框和 1 个操作按钮。 When clicking on either of the checkboxes, a graph should output BUT only after clicking on the action button.单击任一复选框时,图表应为 output 但仅在单击操作按钮后。 The code I have bellow does this well already.我下面的代码已经很好地做到了。 My issue here is that once the action button has been clicked and the graph generated, unclicking the checkbox removes the graph.我的问题是,一旦单击操作按钮并生成图形,取消单击复选框就会删除图形。 Similarly, clicking again generates a new graph without clicking on the action button.同样,再次单击会生成一个新图形,而无需单击操作按钮。 I would like for the graph to stay on the screen for as long as I dont click on the action button again.只要我不再单击操作按钮,我希望图表一直在屏幕上。 I imagine this has to do with "isolating" the checkboxes but Im not too sure how to do so.我想这与“隔离”复选框有关,但我不太确定该怎么做。

As a side note, imagine there was a third function generating a plot in my server when clicking on the action button (regardless of the checkboxes).附带说明一下,假设在单击操作按钮(无论复选框如何)时,我的服务器中有第三个 function 生成 plot。 Is there a way to code my "showmodal, removemodal" such that the pop up stays while all functions are running (instead of only during the first function)?有没有办法对我的“showmodal,removemodal”进行编码,以便在所有功能运行时弹出窗口(而不是仅在第一个功能期间)?

Here is my code这是我的代码

library(shiny)

#Function 1
X <- function(a,b,c){
    plot(c(a,b),c(b,c))
}

#Function 2
Y <- function(d,e,f){
    plot(c(d,e),c(e,f))
}

ui <- fluidPage(
    
    titlePanel("title"),
    
    sidebarLayout(
        sidebarPanel(
            checkboxInput("EF", "Efficient Frontier"),
            checkboxInput("MonteCarlo", "Monte Carlo Simulation"),
            actionButton("Go", "Go", style="color: #fff; background-color: #337ab7; border-color: #2e6da4; margin: auto")
        ),
        
        mainPanel(
            fluidRow(
                align = "center",
                conditionalPanel(condition = "input.EF == true", plotOutput("GraphEF")),
                conditionalPanel(condition = "input.MonteCarlo == true", plotOutput("GraphMC"))
            )
        )
    )
)

server <- function(input, output) {
    
    OPw <- reactiveValues()
    output$Graphw <- renderPlot({ 
        OPw$PC}, height = 400, width = 400)
    
    observeEvent(input$Go, {
        showModal(modalDialog("Loading... Please Wait", footer=NULL)) 
    
        output$GraphEF <- renderPlot({ #Efficient Frontier
            if(input$EF){
                X(5,10,15)
            }
        }, height = 550, width = 700)
        
        output$GraphMC <- renderPlot({ #Monte Carlo Simulation
            if(input$MonteCarlo){
                Y(5,10,15)
            }
        },height = 550, width = 700)
        
        removeModal() #Removes Loading Pop-up Message
        
        
    })
}

shinyApp(ui = ui, server = server)

Thanks a lot for your help!非常感谢你的帮助!

Leaving a conditionalPanel -approach, which is referring to a discussion over here :留下一个conditionalPanel -方法,这是指这里的讨论:

library(shiny)

# Function 1
X <- function(a, b, c) {
  plot(c(a, b), c(b, c))
}

# Function 2
Y <- function(d, e, f) {
  plot(c(d, e), c(e, f))
}

ui <- fluidPage(
  titlePanel("title"),
  sidebarLayout(
    sidebarPanel(
      checkboxInput("EF", "Efficient Frontier"),
      checkboxInput("MonteCarlo", "Monte Carlo Simulation"),
      actionButton("Go", "Go", style = "color: #fff; background-color: #337ab7; border-color: #2e6da4; margin: auto")
    ),
    mainPanel(
      fluidRow(
        align = "center", 
        conditionalPanel("output.showme == true", plotOutput("GraphEF")),
        plotOutput("GraphMC")
      )
    )
  )
)

server <- function(input, output) {
  
  GEF <- eventReactive(input$Go, {
    if (input$EF) {
      X(5, 10, 15)
    } else {
      NULL
    }
  })
  
  output$showme <- eventReactive(input$Go, {
    if (input$EF) TRUE else FALSE
  })
  outputOptions(output, "showme", suspendWhenHidden = FALSE)
  
  GMC <- eventReactive(input$Go, {
    if (isolate(input$MonteCarlo)) {
      Y(5, 10, 15)
    } else {
      NULL
    }
  })
  
  output$GraphMC <- renderPlot({
    GMC()
  })
  
  output$GraphEF <- renderPlot({ # Efficient Frontier
    GEF()
  })
  
  observeEvent(input$Go, {
    showModal(modalDialog("Loading... Please Wait", footer = NULL))
    
    Sys.sleep(2)
    
    removeModal() # Removes Loading Pop-up Message
  })
  
}

shinyApp(ui = ui, server = server)

Furthermore, please see this related answer .此外,请参阅此相关答案

The modal is working well, because both functions take so little time to run it creates de sensation than is there less than it should be.模态运行良好,因为这两个函数运行时间都很短,它产生的感觉比它应该的要少。 We can show this by adding a sys.sleep to simulate a long calculation.我们可以通过添加sys.sleep来模拟长时间计算来证明这一点。

Regarding the checkboxes, using conditionalPanel will hide or show the plots independently of the presence of isolate inside the server.关于复选框,使用conditionalPanel将隐藏或显示图表,而与服务器内部isolate的存在无关。 A workaround is just to return NULL when the checkbox is not clicked.解决方法是在未单击复选框时返回NULL

library(shiny)

# Function 1
X <- function(a, b, c) {
  plot(c(a, b), c(b, c))
}

# Function 2
Y <- function(d, e, f) {
  plot(c(d, e), c(e, f))
}

ui <- fluidPage(
  titlePanel("title"),
  sidebarLayout(
    sidebarPanel(
      checkboxInput("EF", "Efficient Frontier"),
      checkboxInput("MonteCarlo", "Monte Carlo Simulation"),
      actionButton("Go", "Go", style = "color: #fff; background-color: #337ab7; border-color: #2e6da4; margin: auto")
    ),
    mainPanel(
      fluidRow(
        align = "center",
        plotOutput("GraphEF"),
        plotOutput("GraphMC")
      )
    )
  )
)

server <- function(input, output) {
  OPw <- reactiveValues()
  output$Graphw <- renderPlot(
    {
      OPw$PC
    },
    height = 400,
    width = 400
  )

  observeEvent(input$Go, {
    showModal(modalDialog("Loading... Please Wait", footer = NULL))

    output$GraphEF <- renderPlot(
      { # Efficient Frontier
        if (isolate(input$EF)) {
          X(5, 10, 15)
        } else {
          NULL
        }
      },
      height = 550,
      width = 700
    )
    Sys.sleep(2)
    output$GraphMC <- renderPlot(
      { # Monte Carlo Simulation
        if (isolate(input$MonteCarlo)) {
          Y(5, 10, 15)
        } else {
          NULL
        }
      },
      height = 550,
      width = 700
    )

    removeModal() # Removes Loading Pop-up Message
  })
}

shinyApp(ui = ui, server = server)

Perhaps you should use eventReactive() .也许你应该使用eventReactive() Try this尝试这个

library(shiny)

# Function 1
X <- function(a, b, c) {
  plot(c(a, b), c(b, c))
}

# Function 2
Y <- function(d, e, f) {
  plot(c(d, e), c(e, f))
}

ui <- fluidPage(
  titlePanel("title"),
  sidebarLayout(
    sidebarPanel(
      checkboxInput("EF", "Efficient Frontier"),
      checkboxInput("MonteCarlo", "Monte Carlo Simulation"),
      actionButton("Go", "Go", style = "color: #fff; background-color: #337ab7; border-color: #2e6da4; margin: auto")
    ),
    mainPanel(
      fluidRow(
        align = "center",
        plotOutput("GraphEF", height = 550, width = 700),
        plotOutput("GraphMC", height = 550, width = 700)
      )
    )
  )
)

server <- function(input, output) {
  
  GEF <- eventReactive(input$Go, {
    if (input$EF) {
      X(5, 10, 15)
    } else {
      NULL
    }
  })
  
  GMC <- eventReactive(input$Go, {
    if (input$MonteCarlo) {
      Y(5, 10, 15)
    } else {
      NULL
    }
  })
  
  output$GraphEF <- renderPlot({ # Efficient Frontier
    GEF()
  })
  
  output$GraphMC <- renderPlot({ # Efficient Frontier
    GMC()
  })
  
  observeEvent(input$Go, {
    showModal(modalDialog("Loading... Please Wait", footer = NULL))
 
    Sys.sleep(2)
    
    removeModal() # Removes Loading Pop-up Message
  })
  
}

shinyApp(ui = ui, server = server)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM