R闪亮-条件面板输出移位? [英] R SHINY - Conditional panel output shifted?

查看:16
本文介绍了R闪亮-条件面板输出移位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此帖子与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)

这篇关于R闪亮-条件面板输出移位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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