[英]R SHINY - Conditional panel output shifted?
这篇文章与R shiny - 复选框和条件面板问题有关。 我在这里设法创建了该问题的 MRE。
再次总结一下,当同时单击第二个复选框或第一个和第二个复选框时,数据框 output 会移动...我希望它与单击时一样显示在相同的 position第一个复选框。
library(shiny)
library(ggplot2)
ui <- shinyUI(fluidPage(
titlePanel("Construction"),
sidebarLayout(
sidebarPanel(
checkboxInput("EF", "Efficient Frontier"),
checkboxInput("MonteCarlo", "Monte Carlo Simulation"),
fluidRow(
align = "center",
actionButton("Gow", "Go!")),
),
mainPanel(
column(12,
br(),
align = "left",
splitLayout(cellWidths = c("70%", "30%"),
plotOutput("Graphw"),
conditionalPanel(condition = "input.EF == true && input.MonteCarlo == false", tableOutput("EFWeightsTable")),
conditionalPanel(condition = "input.MonteCarlo == true && input.EF == false", tableOutput("MCWeightsTable")),
conditionalPanel(condition = "input.MonteCarlo == true && input.EF == true", tableOutput("EFMCWeightsTable")))),
column(12,
align = "center",
conditionalPanel(condition = "input.EF == true && input.MonteCarlo == false", plotOutput("GraphEF")),
conditionalPanel(condition = "input.MonteCarlo == true && input.EF == false", plotOutput("GraphMC")),
conditionalPanel(condition = "input.MonteCarlo == true && input.EF == true", plotOutput("GraphEFMC"))
)
)
)
)
)
#Server
server <- shinyServer(function(input, output) {
OPw <- reactiveValues()
observeEvent(input$Gow, {
OPw$PC <- Run(1,2,3)
if(input$EF == TRUE && input$MonteCarlo == FALSE){
showModal(modalDialog("Loading... Please Wait", footer=NULL))
OPw$LIST1 <- Run2(1,2,3)
}
removeModal()
if(input$MonteCarlo == TRUE && input$EF == FALSE){
showModal(modalDialog("Loading... Please Wait", footer=NULL))
OPw$LIST2 <- Run3(1,2,3)
}
removeModal()
if(input$MonteCarlo == TRUE && input$EF == TRUE){
showModal(modalDialog("Loading... Please Wait", footer=NULL))
OPw$LIST3 <- Run4(1,2,3)
}
removeModal()
})
#Output Variables
output$Graphw <- renderPlot({
OPw$PC}, height = 400, width = 400)
output$GraphEF <- renderPlot({
OPw$LIST1[[1]]
},height = 550, width = 700)
output$EFWeightsTable <- renderTable({
OPw$LIST1[[2]]}, colnames = TRUE
)
output$GraphMC <- renderPlot({
OPw$LIST2[[1]]
},height = 550, width = 700)
output$MCWeightsTable <- renderTable({
OPw$LIST2[[2]]}, colnames = TRUE
)
output$GraphEFMC <- renderPlot({
OPw$LIST3[[1]]
},height = 550, width = 700)
output$EFMCWeightsTable <- renderTable({
OPw$LIST3[[2]]}, colnames = TRUE
)
#FUNCTIONS
Run <- function(a, b, c){
Plot <- ggplot(as.data.frame(cbind(c(1,2,3),c(2,3,4))), aes(c(1,2,3), c(2,3,4))) +
geom_line()
return(Plot)
}
Run2 <- function(a,b,c){
eweights <- data.frame(cbind(seq(1,9),seq(1,9),seq(1,9)))
MYPLOT <- ggplot(as.data.frame(cbind(c(10,7,4),c(5,6,7))), aes(c(10,7,4), c(5,6,7))) +
geom_line()
return(list(MYPLOT, eweights))
}
Run3 <- function(a,b,c){
eweights <- data.frame(cbind(seq(2,10),seq(2,10),seq(2,10)))
MYPLOT <- ggplot(as.data.frame(cbind(c(4,5,6),c(7,8,9))), aes(c(4,5,6),c(7,8,9))) +
geom_line()
return(list(MYPLOT, eweights))
}
Run4 <- function(a,b,c){
Run3(a,b,c)
}
})
shinyApp (ui = ui, server = server)
谢谢你
使用renderUI()
应该可以帮助您。 尝试这个
library(shiny)
library(ggplot2)
ui <- shinyUI(fluidPage(
titlePanel("Construction"),
sidebarLayout(
sidebarPanel(
checkboxInput("EF", "Efficient Frontier"),
checkboxInput("MonteCarlo", "Monte Carlo Simulation"),
fluidRow(
align = "center",
actionButton("Gow", "Go!")),
),
mainPanel(
column(12,
br(),
align = "left",
splitLayout(cellWidths = c("70%", "30%"),
plotOutput("Graphw"), uiOutput("mytable")
)),
column(12,
align = "center",
conditionalPanel(condition = "input.EF == true && input.MonteCarlo == false", plotOutput("GraphEF")),
conditionalPanel(condition = "input.MonteCarlo == true && input.EF == false", plotOutput("GraphMC")),
conditionalPanel(condition = "input.MonteCarlo == true && input.EF == true", plotOutput("GraphEFMC"))
)
)
)
)
)
#Server
server <- shinyServer(function(input, output) {
OPw <- reactiveValues()
observeEvent(input$Gow, {
OPw$PC <- Run(1,2,3)
if(input$EF == TRUE && input$MonteCarlo == FALSE){
showModal(modalDialog("Loading... Please Wait 1", footer=NULL))
OPw$LIST1 <- Run2(1,2,3)
}
removeModal()
if(input$MonteCarlo == TRUE && input$EF == FALSE){
showModal(modalDialog("Loading... Please Wait 2", footer=NULL))
OPw$LIST2 <- Run3(1,2,3)
}
removeModal()
if(input$MonteCarlo == TRUE && input$EF == TRUE){
showModal(modalDialog("Loading... Please Wait 3", footer=NULL))
OPw$LIST3 <- Run4(1,2,3)
}
removeModal()
})
#Output Variables
output$Graphw <- renderPlot({
OPw$PC}, height = 400, width = 400)
output$GraphEF <- renderPlot({
OPw$LIST1[[1]]
},height = 550, width = 700)
output$EFWeightsTable <- renderTable({
OPw$LIST1[[2]]}, colnames = TRUE
)
output$GraphMC <- renderPlot({
OPw$LIST2[[1]]
},height = 550, width = 700)
output$MCWeightsTable <- renderTable({
OPw$LIST2[[2]]}, colnames = TRUE
)
output$GraphEFMC <- renderPlot({
OPw$LIST3[[1]]
},height = 550, width = 700)
output$EFMCWeightsTable <- renderTable({
OPw$LIST3[[2]]}, colnames = TRUE
)
output$mytable <- renderUI({
if (input$EF & !input$MonteCarlo) {tableOutput("EFWeightsTable")
} else if (!input$EF & input$MonteCarlo){tableOutput("MCWeightsTable")
} else if (input$EF & input$MonteCarlo){tableOutput("EFMCWeightsTable")
} else return(NULL)
})
#FUNCTIONS
Run <- function(a, b, c){
Plot <- ggplot(as.data.frame(cbind(c(1,2,3),c(2,3,4))), aes(c(1,2,3), c(2,3,4))) +
geom_line()
return(Plot)
}
Run2 <- function(a,b,c){
eweights <- data.frame(cbind(seq(1,9),seq(1,9),seq(1,9)))
MYPLOT <- ggplot(as.data.frame(cbind(c(10,7,4),c(5,6,7))), aes(c(10,7,4), c(5,6,7))) +
geom_line()
return(list(MYPLOT, eweights))
}
Run3 <- function(a,b,c){
eweights <- data.frame(cbind(seq(2,10),seq(2,10),seq(2,10)))
MYPLOT <- ggplot(as.data.frame(cbind(c(4,5,6),c(7,8,9))), aes(c(4,5,6),c(7,8,9))) +
geom_line()
return(list(MYPLOT, eweights))
}
Run4 <- function(a,b,c){
Run3(a,b,c)
}
})
shinyApp (ui = ui, server = server)
我看到您已经接受了答案,但我已经投入了工作。 我使用list()
object 作为条件,这似乎有效。 您可以通过将简单对象作为占位符来减少大部分服务器代码。
library(shiny)
ui <- shinyUI(fluidPage(
sidebarLayout(
sidebarPanel(
checkboxInput("EF", "Efficient Frontier"),
checkboxInput("MonteCarlo", "Monte Carlo Simulation")
),
mainPanel(
column(12, # if these are supposed to stack, I'd use `fluidRow()`
align = "left",
splitLayout(
cellWidths = c("70%", "30%"),
plotOutput("Graphw"),
list( # I made this a list object
conditionalPanel(condition = "input.EF == true && input.MonteCarlo == false", tableOutput("EFWeightsTable")),
conditionalPanel(condition = "input.MonteCarlo == true && input.EF == false", tableOutput("MCWeightsTable")),
conditionalPanel(condition = "input.MonteCarlo == true && input.EF == true", tableOutput("EFMCWeightsTable"))
)
)
),
column(12,
align = "center",
conditionalPanel(condition = "input.EF == true && input.MonteCarlo == false", plotOutput("GraphEF")),
conditionalPanel(condition = "input.MonteCarlo == true && input.EF == false", plotOutput("GraphMC")),
conditionalPanel(condition = "input.MonteCarlo == true && input.EF == true", plotOutput("GraphEFMC"))
)
)
)
))
server <- function(input, output, session) {
# dummy tables
df <- mtcars[1:3, 1:3]
output$tablew <- renderTable(df)
output$EFWeightsTable <- renderTable(df)
output$MCWeightsTable <- renderTable(df)
output$EFMCWeightsTable <- renderTable(df)
# dummy plots
output$GraphEF <- renderPlot(plot(1, 1, main = "Efficient Frontier"))
output$GraphMC <- renderPlot(plot(1, 1, main = "Monte Carlo"))
output$GraphEFMC <- renderPlot(plot(1, 1, main = "Both"))
output$Graphw <- renderPlot(plot(1, 1, main = "Graph W"))
}
shinyApp(ui, server)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.