Error in rnorm - Stuck on trying to solve this issue

Good evening,
I'm trying to create a histogram app and I get the following error message:
Error in rnorm: invalid arguments

Can someone tell me please what I'm doing wrong? I'm stuck on this for hours.

Here is my UI and server codes:

UI

library(shiny)

fluidPage(

pageWithSidebar(

headerPanel("My First Shiny App"),

sidebarPanel(
  selectInput(inputId = "Distribution",
              label = "Please Select Distribution Type",
              choices = c("Normal", "Exponential")),
  sliderInput(inputId = "Sample Size",
              label = "Please Select Sample Size: ",
              min = 100, max = 5000, value = 1000, step = 100),
  conditionalPanel(condition = "input.Distribution == 'Normal'",
                   textInput(inputId = "mean",
                             label = "Please Select the Mean",
                             value = 10),
                   textInput(inputId = "sd",
                             label = "Please Select Standard Deviation",
                             value = 3)),
  conditionalPanel(condition = "input.Distribution == 'Exponential'",
                   textInput(inputId = "lambda",
                             label = "Please Select Exponential Lambda",
                             value = 1))
  
),

mainPanel(
  plotOutput(outputId = "myPlot")
)

)

)

Server

function(input, output, session) {

output$myPlot <- renderPlot({

distType <- input$Distribution
size <- input$sampleSize

if(distType == "Normal"){
  
  randomVec <- rnorm(
    n = size,
    mean = as.numeric(input$mean),
    sd = as.numeric(input$sd))
}
else {
  
  randomVec <- rexp(
    n = size,
    rate = 1/as.numeric(input$lambda))
}

hist(x = randomVec, col = "blue")

})

}

Notice that in the ui you have

sliderInput(inputId = "Sample Size",
              label = "Please Select Sample Size: ",
              min = 100, max = 5000, value = 1000, step = 100)

where the inputId has a space in the name and both words capitalized. In the server you have

size <- input$sampleSize

with the first s in lower case and all one word, as it should be. Make sure the two match and do not use any spaces in the inputId.

1 Like

Thank you! Now I can run the app without any error.
=D

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.