R SHINY - 有条件的面板输出发生了变化?
R SHINY - Conditional panel output shifted?
此 post 与 R shiny - Checkbox and conditional panels issues 有关。我在这里设法创建了一个问题的 MRE。
再次总结一下,当同时单击第二个复选框或第一个和第二个复选框时,数据框输出会发生偏移...我希望它显示在与它相同的位置是当您单击第一个复选框时。
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()
对象作为条件,这似乎有效。您可以通过将简单对象作为占位符来减少大部分服务器代码。
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)
此 post 与 R shiny - Checkbox and conditional panels issues 有关。我在这里设法创建了一个问题的 MRE。
再次总结一下,当同时单击第二个复选框或第一个和第二个复选框时,数据框输出会发生偏移...我希望它显示在与它相同的位置是当您单击第一个复选框时。
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()
对象作为条件,这似乎有效。您可以通过将简单对象作为占位符来减少大部分服务器代码。
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)