【问题标题】:R shiny - checkboxes and action button combination issueR闪亮 - 复选框和操作按钮组合问题
【发布时间】:2022-01-24 20:54:58
【问题描述】:

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

作为旁注,想象一下当单击操作按钮(不管复选框)时,我的服务器中有第三个函数会生成一个绘图。有没有办法对我的“showmodal,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)

非常感谢您的帮助!

【问题讨论】:

  • 不要嵌套你的反应。
  • 不确定我明白你的意思
  • @Limey 想告诉你,你永远不应该在observeEventobserve 调用中调用renderPlot(或任何其他渲染*-函数)。

标签: r checkbox shiny shinydashboard


【解决方案1】:

也许你应该使用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)

【讨论】:

  • 这个问题是,通过删除条件面板,如果第二个复选框被标记但第一个未选中,“第二个”图现在会在“空”第一个图下方输出
  • 试试更新的代码。
  • 非常感谢!另外,如果您想查看的话,我按照您的建议用 MRE 发表了一篇新帖子 :) stackoverflow.com/questions/70828547/…>
  • 我实际上无法重现您的解决方案,因为我的代码比我给出的这个示例更复杂。如果您想查看,我发了一个新帖子 stackoverflow.com/questions/70841734/r-shiny-checkbox-issue>
【解决方案2】:

留下一个conditionalPanel-approach,指的是讨论over here

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)

另外,请看this related answer

【讨论】:

  • 这很好用,但不幸的是我的代码有点复杂,所以我无法重现你的解决方案。如果您想查看,我在这里发了一个新帖子 stackoverflow.com/questions/70841734/r-shiny-checkbox-issue>
【解决方案3】:

模态运行良好,因为这两个函数运行时间都很短,它所产生的感觉比它应有的要少。我们可以通过添加sys.sleep 来模拟长计算来证明这一点。

关于复选框,使用conditionalPanel 将隐藏或显示绘图,而与服务器内部isolate 的存在无关。解决方法是在未单击复选框时返回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)

【讨论】:

  • 嘿,我刚刚尝试了您的解决方案,但似乎它并没有真正改变任何东西?一旦我取消选中这些框,图表仍然会消失
猜你喜欢
  • 2015-09-01
  • 1970-01-01
  • 2019-03-16
  • 2017-02-16
  • 1970-01-01
  • 2019-07-23
  • 1970-01-01
  • 2019-03-09
  • 2019-07-15
相关资源
最近更新 更多