操作按钮和观察事件不工作(R)
有人能帮我查一下下面的代码,告诉我出了什么问题吗操作按钮和观察事件不工作(R),r,shiny,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") ),
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)
})
}
)
shinyUI(fluidPage(
侧边栏布局(
侧栏面板(
滑块输入(“平均值”,
label=“拖动以选择正态分布的平均值”,
最小值=0,最大值=5,值=0),
动作按钮(“显示”、“开始”)
),
主面板(
h3(“编号为”),
textOutput('number')
)
)
))
shinyServer(功能(输入、输出){
#ObserveeEvent(输入$show{
#output$numberevent reactive
的输出是一个被动变量。您需要使用renderText
和observeEvent
。另外,您还没有在ui中定义input$mu
,我认为它应该是input$Mean
实施上述修改后,服务器代码应如下所示:
server <- shinyServer(function(input, output) {
observeEvent(input$show, {
output$number <- renderText(round(rnorm(as.numeric(input$Mean)), 2))
})
}
)
server我不太喜欢将对象放在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)
库(闪亮)
用户界面