[英]Actionbutton and observeEvent not working (R shiny)
有人可以帮我检查下面的代码,告诉我怎么了吗?
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)
})
}
)
只是想让我每次单击“开始”时都从一个正态分布中抽取一个具有给定均值的随机数。 eventReactive和observeEvent均无效。
我不太喜欢在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)
eventReactive
的输出是一个反应变量。 您将需要使用renderText
和observeEvent
。 另外您还没有在用户界面中定义input$mu
,我认为应该input$Mean
完成上述修改后,您的服务器代码应如下所示:
server <- shinyServer(function(input, output) {
observeEvent(input$show, {
output$number <- renderText(round(rnorm(as.numeric(input$Mean)), 2))
})
}
)
希望能帮助到你!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.