【问题标题】:Shiny scoping error with observeEvent观察事件的闪亮范围错误
【发布时间】:2015-11-29 17:50:21
【问题描述】:

我在一个闪亮的应用程序中的 server.R 中有以下内容...

shinyServer(function(input, output, session) {

   observeEvent(
     input$goButton,  #dependency
     {
       Predictores<-reactive({
         test<-input$LUNES*20
         test2<-input$LUNES*30  
         data.frame("one" = test, 
                    "two" = test2)
       })
     output$productos<-renderTable({as.data.frame(Predictores())},
                                   include.rownames = FALSE )
     }
   )
})

当我尝试使用 ui.R 时,它工作正常并且应用程序正在运行,但是当我单击“goButton”时没有任何反应。所需的输出将是一个带有数据框的表格(现在我正在测试)。

这适用于这样的反应函数:

shinyServer(function(input, output, session) {

  Predictores<-reactive({
    test<-input$LUNES*20
    test2<-input$LUNES*30  
    data.frame("uno" = test, 
               "dos" = test2)
  })
output$productos<-renderTable({as.data.frame(Predictores())},
                               include.rownames = FALSE )
 })

但是因为真正的应用程序会根据输入计算计算密集型模型,所以我希望仅在用户按下“开始”后才进行计算,而不是每次输入更改时进行。

我查看了闪亮的文档,这似乎是这样,但也许我错过了一些范围规则?这可能是它运行的原因,但我什么也没看到......

【问题讨论】:

    标签: r scope shiny-server shiny


    【解决方案1】:

    isolate你的按钮,做这样的事情:

    rm(list = ls())
    library(shiny)
    
    ui =(pageWithSidebar(
      headerPanel("Table output"),
      sidebarPanel(
        sliderInput("LUNES", "LUNES", 100, 500, 2000, sep = ""),
        actionButton("goButton","GO")
        ),
      mainPanel(tableOutput("productos"))
    ))
    
    server = function(input, output, session){
      Predictores<-reactive({
        if (is.null(input$goButton) || input$goButton == 0){return()}
        isolate({
          input$goButton
          test<-input$LUNES*20
          test2<-input$LUNES*30  
          data.frame("uno" = test, "dos" = test2)
        })
      })
      output$productos<-renderTable({as.data.frame(Predictores())},include.rownames = FALSE )
    }
    runApp(list(ui = ui, server = server))
    

    示例输出如下

    编辑 - 你的个人例子 我可以看到你的括号有点混乱,看看下面的解决方案:

    (list = ls())
    library(shiny)
    
    ui = (
      fluidPage(
        title = 'Modelo de Caídas en Venta',
        ## --------- titulo
        titlePanel("Modelo de Caídas en Venta"),
        ## --------- barra lado de inputs
        sidebarPanel(
          ## ----- tabs  
          tabsetPanel(type = "tabs", 
                      # tab 1 -------------------------
                      tabPanel("Mes", 
                               selectInput(inputId = "MES",label = "Mes a predecir",selectize = TRUE,choices = "201508"),
                               selectInput(inputId = "MES_APERTURA",label = "Mes Apertura",selectize = TRUE,choices = "201508"),
                               sliderInput(inputId = "LUNES",label = "Lunes en mes a predecir", value = 4,min = 2, max = 6),
                               sliderInput(inputId = "VIERNES",label = "Viernes en mes a predecir",value = 4,min = 2, max = 6),
                               sliderInput(inputId = "FINDE",label = "Dias de fin en mes a predecir",value = 8, min = 6, max = 10)), 
                      # tab 2 -------------------------
                      # tab 2 -------------------------
                      tabPanel("Mes Antes", 
                               helpText("Todos los indicadores en esta sección se refieren
                                        a un mes anterior al que se va predecir por el modelo"),
                               checkboxInput(inputId = "EVENTO_PREVIO", 
                                             label = "Caída un mes antes", 
                                             value = FALSE),
                               numericInput(inputId = "CLIENTES",
                                            label = "Clientes",
                                            value = 2900,
                                            min = 1400,  max = 9500),
                               # UNIDADES 
                               numericInput(inputId = "U_FARMAMP",
                                            label = "Unidades Farma MP",
                                            value = 7900,
                                            min = 900,  max = 29500),
                               numericInput(inputId = "U_OTCMP",
                                            label = "Unidades OTC MP",
                                            value = 7900,
                                            min = 900,  max = 29500),
                               numericInput(inputId = "U_BEBE",
                                            label = "Unidades Bebé",
                                            value = 2900,
                                            min = 1400,  max = 9500),
                               numericInput(inputId = "U_CONV",
                                            label = "Unidades Conveniencia",
                                            value = 2900,
                                            min = 1400,  max = 9500),
                               numericInput(inputId = "U_RECETA",
                                            label = "Unidades Receta",
                                            value = 2900,
                                            min = 1400,  max = 9500),
                               # YOY DE UNIDADES
                               numericInput(inputId = "Y_FARMAMP",
                                            label = "Unidades Farma MP (mes año pasado)",
                                            value = 7900,
                                            min = 900,  max = 29500),
                               numericInput(inputId = "Y_OTCMP",
                                            label = "Unidades OTC MP (mes año pasado)",
                                            value = 7900,
                                            min = 900,  max = 29500),
                               numericInput(inputId = "Y_BEBE",
                                            label = "Unidades Bebe (mes año pasado)",
                                            value = 2900,
                                            min = 1400,  max = 9500),
                               numericInput(inputId = "Y_CONV",
                                            label = "Unidades Conveniencia (mes año pasado)",
                                            value = 2900,
                                            min = 1400,  max = 9500),
                               numericInput(inputId = "Y_RECETA",
                                            label = "Unidades con Receta (mes año pasado)",
                                            value = 2900,
                                            min = 1400,  max = 9500),
                               #OTROS DE MES ANTES 
                               numericInput(inputId = "PROD",
                                            label = "Productos únicos",
                                            value = 2900,
                                            min = 1400,  max = 9500),
                               #PORCENTAJES DE CAÍDAS
                               sliderInput(inputId = "P_PLAZA",
                                           label = "Porcentaje de sucursales en plaza con caídas",
                                           value = 30,
                                           min = 0,  max = 100),
                               sliderInput(inputId = "P_ESTADO",
                                           label = "Porcentaje de sucursales en estado con caídas",
                                           value = 30,
                                           min = 0,  max = 100),
                               sliderInput(inputId = "P_ZONA",
                                           label = "Porcentaje de sucursales en zona con caídas",
                                           value = 30,
                                           min = 0,  max = 100),
                               sliderInput(inputId = "P_CIUDAD",
                                           label = "Porcentaje de sucursales en ciudad con caídas",
                                           value = 30,
                                           min = 0,  max = 100)
                               ),
                      # tab 3 -------------------------
                      tabPanel("Sucursales", 
                               sliderInput(inputId = "SUCURSALES",
                                           label = "Sucursales en total (mismas tiendas)",
                                           value = 990,
                                           min = 300,  max = 3000),
                               numericInput(inputId = "DISTANCIA_MIN",
                                            label = "Distancia con sucursal más cercana (en metros)",
                                            value = 850,
                                            min = 200,  max = 240000),
                               numericInput(inputId = "DISTANCIA_PROM",
                                            label = "Distancia con promedio contra otras sucursales (en metros)",
                                            value = 18500,
                                            min = 1500,  max = 1000000),
                               sliderInput(inputId = "SUC_PLAZA",
                                           label = "Sucursales en plaza (incluyendo esta)",
                                           value = 20,
                                           min = 0,  max = 900),
                               sliderInput(inputId = "SUC_ESTADO",
                                           label = "Sucursales en estado (incluyendo esta)",
                                           value = 55,
                                           min = 0,  max = 900),
                               sliderInput(inputId = "SUC_ZONA",
                                           label = "Sucursales en zona (incluyendo esta)",
                                           value = 30,
                                           min = 0,  max = 900),
                               sliderInput(inputId = "SUC_CIUDAD",
                                           label = "Sucursales en ciudad (incluyendo esta)",
                                           value = 15,
                                           min = 0,  max = 900))
        ), #fin tabs
        hr(), # soy un delimitador
        helpText("Para consultas: eduardo.lomas@fahorro.com.mx"),
        actionButton("goButton","GO")
        ), #fin de sidebar
        mainPanel(
          helpText("Predicción del Modelo"),
          hr(), # soy un delimitador
          tableOutput("productos")
    
        ) #- mainpanel
      )
      )
    
    
    
    server = function(input, output, session){
      Predictores<-reactive({
        if (is.null(input$goButton) || input$goButton == 0){return()}
        isolate({
          input$goButton
          test<-input$LUNES*20
          test2<-input$LUNES*30  
          data.frame("uno" = test, "dos" = test2)
        })
      })
      output$productos<-renderTable({as.data.frame(Predictores())},include.rownames = FALSE )
    }
    
    runApp(list(ui = ui, server = server))
    

    特定保管箱代码的解决方案

    【讨论】:

    • 很抱歉,我一直得到相同的结果...即使我单击按钮,表格也不会出现...
    • 我已经包含了一个详细的示例和输出来说明我的意思
    • 感谢这可以独立使用,但不适用于我的应用程序,我猜那是我的 UI 特有的东西。具体来说,我认为我的操作按钮不是“发送”或改变它的状态,你知道这是否可能吗?
    • 这里是文件,如果你想看看服务器:dropbox.com/s/kja67u6qavdspe8/server.R?dl=0,ui:dropbox.com/s/6a80snc19d9wv2j/ui.R?dl=0
    • 很高兴能帮上忙 :))
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 2018-05-26
    • 2019-03-12
    • 2016-07-25
    • 2020-05-11
    相关资源
    最近更新 更多