Reputation: 443
Can somebody check the code below for me and tell me what's wrong?
shinyUI(fluidPage(
sidebarLayout(
sidebarPanel(
sliderInput("Mean",
label="Drag to select the mean of the normal distribution",
min=0,max=5,value=0),
actionButton("show","Go")
),
mainPanel(
h3("The number is"),
textOutput('number')
)
)
))
shinyServer(function(input, output) {
#observeEvent(input$show, {
#output$number <- round(rnorm(as.numeric(input$mu)), 2)
#})
output$number <- eventReactive(input$show, {
round(rnorm(as.numeric(input$mu)), 2)
})
}
)
Just want to have a random number sampled from a normal distribution with a given mean each time I click 'Go'. Neither eventReactive nor observeEvent worked.
Upvotes: 1
Views: 3450
Reputation: 29407
I'n not a big fan of having objects inside observeEvent
so here is the solution with eventReactive
library(shiny)
ui <- shinyUI(fluidPage(
sidebarLayout(
sidebarPanel(
sliderInput("Mean",label="Drag to select the mean of the normal distribution",min=0,max=5,value=0),
actionButton("show","Go")
),
mainPanel(
h3("The number is"),
textOutput('number')
)
)
))
server <- shinyServer(function(input, output) {
data <- eventReactive(input$show, {
round(rnorm(as.numeric(input$Mean)), 2)
})
output$number <- renderText({
data()
})
}
)
shinyApp(ui, server)
Upvotes: 3
Reputation: 7704
The output of eventReactive
is a reactive variable. You will need to use renderText
and observeEvent
. Also you have not defined input$mu
in your ui, I think it should be input$Mean
After implementing the above modifications your server code should be something like this:
server <- shinyServer(function(input, output) {
observeEvent(input$show, {
output$number <- renderText(round(rnorm(as.numeric(input$Mean)), 2))
})
}
)
Hope it helps!
Upvotes: 0