简体   繁体   中英

Shiny scoping error with observeEvent

I have the following in server.R in a shiny application...

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 )
     }
   )
})

When I try the ui.R, this works fine and the app is running, but nothing happens when I click the "goButton". The desired output would be a table with a data frame (for now i'm testing).

This works fine with a reactive function like this:

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 )
 })

But because the real app will calculate a computing-intensive model based on the inputs, I want the calculation to be done only after the user presses "go", not everytime an input changes.

I've looked at the shiny docs and this seems to be the way but maybe i'm missing some scoping rules? Which could be the reason it runs but I don't see anything...

isolate your button by doing something like this:

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))

Sample Output is below

样品

EDIT - your personal example I can see that you got your brackets mixed up a bit, have a look at the solution below:

(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))

Solution to your particular dropbox code

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM