【发布时间】: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 想告诉你,你永远不应该在
observeEvent或observe调用中调用renderPlot(或任何其他渲染*-函数)。
标签: r checkbox shiny shinydashboard