带有条件的RenderUI

时间:2017-06-24 12:06:50

标签: r rstudio shiny

我正在尝试使用条件布局创建一个闪亮的应用程序。问题是,我在生成我希望的布局时遇到了障碍。从我在网上看到的,由于你无法用条件输出renderUI,虽然我并不完全确定,即使代码似乎有用conditionalPanel s也没有显示出来。

应用程序将采用用户输入,在这种情况下允许用户从变量列表中进行选择。基于用户选择的变量,我想生成n个小部件,这些小部件允许您使用概率分布(正态,均匀,对数正态,三角等)定义每个变量。一旦用户根据分布选择n1的分布,一组条件面板就会变得可见,要求用户选择需要什么参数来填充变量(在这种情况下为n1)如下图所示。

如果有人能提出解决方案/解决方案会很棒 这是我在选择每个变量时要寻找的布局: Required layout

编辑:取得了一些进展

更新了代码,但条件面板全部显示,而不是与仅选择的PDF功能相关的那些。

library(shiny)

ui= fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectizeInput(inputId= "invar", label= "invar", 
                     choices= names(iris), 
                     selected= names(iris)[1],
                     multiple=T),
      uiOutput("invarpd")

    ),
    mainPanel(
      tableOutput("tab")
    )
  ))


server= function(input, output,session) {

  sorted <-  reactive({
    data <- iris[ ,c(input$invar)]
    #print(input$invar)
    data})

  output$invarpd <- renderUI({
    numvar<- length(input$invar)
    lapply(1:numvar, function(i) {
      tagList(
        selectInput(inputId=paste0("invarpd",input$invar[i]),paste0("Please Select Probability Distribution of ", input$invar[i]),
                    choices = c("Normal","Uniform","Triangular")),
        conditionalPanel(condition = "input.inputId=='Normal'",
                         textInput("invarpdfmean","Please Select Input Variable Mean:",0.25),
                         textInput("invarpdfsd","Please Select Input Variable Standard Deviation", 0.02)),
        conditionalPanel(condition = "input.inputId=='Uniform'",
                         textInput("invarpdfmin","Please Select Minimum Input Variable Value:",0.18),
                         textInput("invarpdfmax","Please Select Maximum Input Variable Value", 0.3)),
        conditionalPanel(condition = "input.inputId=='Triangular'",
                         textInput("invarpdfmin","Please Select Minimum Input Variable Value:",0.18),
                         textInput("invarpdfmax","Please Select Maximum Input Variable Value:", 0.3)),
        conditionalPanel(condition = "input.inputId=='Log Normal'",
                         textInput("invarpdfmeanlog","Please Select Mean Log of Input Variable:",0.18),
                         textInput("invarpdfsdlog","Please Select Standard Deviation Log of Input Variable:", 0.3)),
        textInput("label", "Label")
      )})})


  output$MonteCarlo <- renderPlot({
    set.seed(1)


    n <- input$sampleSize




    if(distinvarpdf=="Normal"){

      invarpdfVec <- rnorm(n,mean = as.numeric(input$invarpdfmean),sd= as.numeric(input$invarpdfsd))
    }
    if(distinvarpdf=="Uniform"){

      invarpdfVec <- runif(n,min = as.numeric(input$invarpdfmin),max = as.numeric(input$invarpdfmax))
    }
    if(distinvarpdf=="Triangular"){

      invarpdfVec <- rltriangle(n,a = as.numeric(input$invarpdfmin),b = as.numeric(input$invarpdfmax))
    }
    if(distinvarpdf=="Log Normal"){

      invarpdfVec <- rlnorm(n,meanlog = as.numeric(input$invarpdfmeanlog),sdlog = as.numeric(input$invarpdfsdlog))
    }




    h<- hist(distinvarpdf,breaks=30,col="red",freq=F)
  })
}



shinyApp(ui = ui, server = server)

编辑2: 取得了进一步的进展。现在第一个变量响应条件,但后续选定的变量不响应

library(shiny)
ui <- fluidPage(
  fluidRow(
    column(4,
    wellPanel(
      selectizeInput(inputId= "invar", label= "Select Variable", 
                     choices= names(iris), 
                     selected= names(iris)[1],
                     multiple=T),
  uiOutput("moreControls"))),


  mainPanel(
    tableOutput("tab")
  )
))
server <- function(input, output) {

  sorted <-  reactive({
    data <- iris[ ,c(input$invar)]
    #print(input$invar)
    data})

  output$moreControls <- renderUI({
    numvar<- length(input$invar)
    lapply(1:numvar, function(i) {
    tagList(
      selectInput("inv",paste0("Please Select Probability Distribution of ", input$invar[i]),
                  choices = c("Normal","Uniform","Triangular","Log Normal")),
      conditionalPanel(condition = "input.inv=='Normal'",
                       textInput("invarpdfmean","Please Select Input Variable Mean:",0.25),
                       textInput("invarpdfsd","Please Select Input Variable Standard Deviation", 0.02)),
      conditionalPanel(condition = "input.inv=='Uniform'",
                       textInput("invarpdfmin","Please Select Minimum Input Variable Value:",0.18),
                       textInput("invarpdfmax","Please Select Maximum Input Variable Value", 0.3)),
      conditionalPanel(condition = "input.inv=='Triangular'",
                       textInput("invarpdfmin","Please Select Minimum Input Variable Value:",0.18),
                       textInput("invarpdfmax","Please Select Maximum Input Variable Value:", 0.3)),
      conditionalPanel(condition = "input.inv=='Log Normal'",
                       textInput("invarpdfmeanlog","Please Select Mean Log of Input Variable:",0.18),
                       textInput("invarpdfsdlog","Please Select Standard Deviation Log of Input Variable:", 0.3))

    )})})
}





shinyApp(ui, server)

0 个答案:

没有答案