Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
操作按钮和观察事件不工作(R)_R_Shiny - Fatal编程技术网

操作按钮和观察事件不工作(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)
库(闪亮)
用户界面