R闪亮-复选框和操作按钮组合问题 [英] R shiny - checkboxes and action button combination issue

查看:21
本文介绍了R闪亮-复选框和操作按钮组合问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个复选框和1个操作按钮。单击任一复选框时,只有在单击操作按钮之后才会输出图形。我下面的代码已经很好地做到了这一点。我这里的问题是,一旦单击了操作按钮并生成了图形,取消单击复选框就会删除该图形。同样,再次单击会生成一个新图形,而不单击操作按钮。我希望只要我不再单击操作按钮,图表就会一直显示在屏幕上。我想这与隔离复选框有关,但我不太确定如何做到这一点。

作为附注,假设当单击操作按钮(不考虑复选框)时,有第三个函数在我的服务器中生成绘图。是否可以编写My";showmode、removemodal";的代码,使弹出窗口在所有函数运行时(而不是仅在第一个函数运行期间)保持不变(&qot;showmode,removemodal";)?

以下是我的代码

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)

非常感谢您的帮助!

推荐答案

或许您应该使用eventReactive()。试试这个

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", 
        uiOutput("plot1"),
        plotOutput("GraphMC")
      )
    )
  )
)

server <- function(input, output) {

  GEF <- eventReactive(input$Go, {
    if (input$EF) {
      X(5, 10, 15)
    } else {
      NULL
    }
  })
  
  showme <- eventReactive(input$Go, {
    if (input$EF) TRUE else 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()
  })
  
  output$plot1 <- renderUI({
    if (showme()) {plotOutput("GraphEF")} else NULL
  })

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

    Sys.sleep(2)
    
    removeModal() # Removes Loading Pop-up Message
  })

}

shinyApp(ui = ui, server = server)

这篇关于R闪亮-复选框和操作按钮组合问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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