[英]Actionbutton and observeEvent not working (R shiny)
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. eventReactive和observeEvent均无效。
I'n not a big fan of having objects inside observeEvent
so here is the solution with eventReactive
我不太喜欢在observeEvent
中包含对象,因此这是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)
The output of eventReactive
is a reactive variable. eventReactive
的输出是一个反应变量。 You will need to use renderText
and observeEvent
. 您将需要使用renderText
和observeEvent
。 Also you have not defined input$mu
in your ui, I think it should be input$Mean
另外您还没有在用户界面中定义input$mu
,我认为应该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! 希望能帮助到你!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.