从闪亮的不同分布生成样本

时间:2017-05-11 11:41:18

标签: r switch-statement shiny

我正在尝试创建一个shinyapp来从一些统计分布中生成不同的样本。我的应用程序包含一个开关,用于指定分布,然后显示参数的输入。

我无法获得任何东西!这是ui.R:

{ "name" : "A", "Active" : 2, "Unknown" : 1},
{ "name" : "B", "Deactive" : 3},
{ "name" : "C", "Unknown" : 3"}

服务器.R:

library(shiny)

shinyUI(fluidPage(

  # Application title
  titlePanel("Sampling"),

  # Sidebar with a slider input for number of bins
  sidebarLayout(
    sidebarPanel(

      selectInput("popDistX", "X",
                  list("Normal" = "normal",
                       "Beta" = "beta",
                       "Gamma" = "gamma",
                       "Weibull" = "weibull"
                  )
      ),  

      numericInput("nX", 
                   "Tamaño muestral X:", 
                   min = 1, 
                   max = 400, 
                   value = 2),


      conditionalPanel(
        condition = "input.popDistX == 'beta'",
        sliderInput("shape1", "Forma 1: ", min=0.1, max=10, value=1, step=0.05),
        sliderInput("shape2", "Forma 2: ", min=0.1, max=10, value=1, step=0.05)
      ),

      conditionalPanel(
        condition = "input.popDistX == 'gamma'",
        sliderInput("shape", "Forma: ", min=0.1, max=10, value=1, step=0.05),
        sliderInput("scale", "Escala: ", min=0.1, max=10, value=1, step=0.05)
      ),

      conditionalPanel(
        condition = "input.popDistX == 'weibull'",
        sliderInput("shape", "Forma: ", min=0.1, max=10, value=1, step=0.05),
        sliderInput("scale", "Escala: ", min=0.1, max=10, value=1, step=0.05)
      ),

      conditionalPanel(
        condition = "input.popDistX == 'normal'",
        sliderInput("mean", "Media: ", min=0, max=400, value=0),
        sliderInput("sd", "Desviación típica: ", min=0.1, max=20, value=1, step=.1)
      )
    ),

    # Show a plot of the generated distribution
    mainPanel(
      plotOutput("histogram")
    )
  )
))

请,任何帮助?这很重要,我不知道我的错误在哪里......

非常感谢您提前。 胡

2 个答案:

答案 0 :(得分:1)

没有一个,但有多个错误:

首先,一些拼写错误:"histogram"而不是"n"(如果您愿意,可以output$n代替output$histogram。有一个ainput而不是input

更有问题:为什么将函数传递给reactive()而不是直接传递表达式?

使用这些小修补程序,以下代码应该可以工作:

library(shiny)

ui <- fluidPage(
  # Application title
  titlePanel("Sampling"),
  # Sidebar with a slider input for number of bins
  sidebarLayout(
    sidebarPanel(
      selectInput("popDistX", "X",
                  list("Normal" = "normal",
                       "Beta" = "beta",
                       "Gamma" = "gamma",
                       "Weibull" = "weibull"
                  )
      ),  
      numericInput("nX", 
                   "Tamaño muestral X:", 
                   min = 1, 
                   max = 400, 
                   value = 2),
      conditionalPanel(
        condition = "input.popDistX == 'beta'",
        sliderInput("shape1", "Forma 1: ", min=0.1, max=10, value=1, step=0.05),
        sliderInput("shape2", "Forma 2: ", min=0.1, max=10, value=1, step=0.05)
      ),
      conditionalPanel(
        condition = "input.popDistX == 'gamma'",
        sliderInput("shape", "Forma: ", min=0.1, max=10, value=1, step=0.05),
        sliderInput("scale", "Escala: ", min=0.1, max=10, value=1, step=0.05)
      ),
      conditionalPanel(
        condition = "input.popDistX == 'weibull'",
        sliderInput("shape", "Forma: ", min=0.1, max=10, value=1, step=0.05),
        sliderInput("scale", "Escala: ", min=0.1, max=10, value=1, step=0.05)
      ),
      conditionalPanel(
        condition = "input.popDistX == 'normal'",
        sliderInput("mean", "Media: ", min=0, max=400, value=0),
        sliderInput("sd", "Desviación típica: ", min=0.1, max=20, value=1, step=.1)
      )
    ),
    # Show a plot of the generated distribution
    mainPanel(
      plotOutput("n")
    )
  )
)

server <- function(input, output) {
  DistX <- reactive( input$popDistX )
  nX <- reactive( input$nX )
  paramsX <- reactive(  {
    switch(DistX(),
           "normal" = list(mean=input$mean, sd=input$sd),
           "beta" = list(shape1=input$shape1, shape2=input$shape2),
           "gamma" = list(shape=input$shape, scale=input$scale),
           "weibull" = list(shape=input$shape, scale=input$scale)
    )} )
  rdistX <- reactive(  {
    switch(DistX(),
           "normal" = "rnorm",
           "beta" = "rbeta",
           "gamma" = "rgamma",
           "weibull" = "rweibull"
    )} )
  sampleDistX <- reactive( 
    do.call(rdistX(), c(nX(), paramsX()))
  )
  output$n <- renderPlot({
    hist(sampleDistX())
  })
}

shinyApp(ui = ui, server = server)

答案 1 :(得分:1)

所有这些开关对我来说似乎都没有用。你可以把它们全部放在1个反应性的东西,这样可以简化你的应用程序(并仍然给你你想要的):

library(shiny)
server <- function(input, output, session) {
  myDist <- reactive({
    if(input$popDistX == "normal"){
      rnorm(input$nX, input$mean, input$sd)

    } else if(input$popDistX == "beta"){
      rbeta(input$nX, input$shape1, input$shape2)

    } else if(input$popDistX == "gamma"){
      rgamma(input$nX, input$shape, input$scale)

    } else if(input$popDistX == "weibull"){
      rweibull(input$nX, input$shape, input$scale)

    }
  })

  output$histogram <- renderPlot({
    hist(myDist())
  })

}

ui <- fluidPage(

  # Application title
  titlePanel("Sampling"),

  # Sidebar with a slider input for number of bins
  sidebarLayout(
    sidebarPanel(

      selectInput("popDistX", "X",
                  list("Normal" = "normal",
                       "Beta" = "beta",
                       "Gamma" = "gamma",
                       "Weibull" = "weibull"
                  )
      ),  

      numericInput("nX", 
                   "Tamaño muestral X:", 
                   min = 1, 
                   max = 400, 
                   value = 2),


      conditionalPanel(
        condition = "input.popDistX == 'beta'",
        sliderInput("shape1", "Forma 1: ", min=0.1, max=10, value=1, step=0.05),
        sliderInput("shape2", "Forma 2: ", min=0.1, max=10, value=1, step=0.05)
      ),

      conditionalPanel(
        condition = "input.popDistX == 'gamma'",
        sliderInput("shape", "Forma: ", min=0.1, max=10, value=1, step=0.05),
        sliderInput("scale", "Escala: ", min=0.1, max=10, value=1, step=0.05)
      ),

      conditionalPanel(
        condition = "input.popDistX == 'weibull'",
        sliderInput("shape", "Forma: ", min=0.1, max=10, value=1, step=0.05),
        sliderInput("scale", "Escala: ", min=0.1, max=10, value=1, step=0.05)
      ),

      conditionalPanel(
        condition = "input.popDistX == 'normal'",
        sliderInput("mean", "Media: ", min=0, max=400, value=0),
        sliderInput("sd", "Desviación típica: ", min=0.1, max=20, value=1, step=.1)
      )
    ),

    # Show a plot of the generated distribution
    mainPanel(
      plotOutput("histogram")
    )
  )
)

shinyApp(ui = ui, server = server, options = list(launch.browser = T ))