eventReactive doesn't work

...Hello!

Sorry for a newbie question.

Can you explain please why the button "Calculate" doesn't display an output using this code?

library(shiny)
library(xgboost)

data(agaricus.train, package='xgboost')
data(agaricus.test, package='xgboost')

ui=fluidPage(
    numericInput(inputId="GrLivArea", label="Enter Ground Living Area", 
                 value=0,min=0, max=100000),
    numericInput(inputId="OverallRate", label="Enter Overall rating", 
                 value=1,min=1, max=20),
    numericInput(inputId="AreaInside", label="Enter Area Inside", 
                 value=0,min=0, max=100000),
    selectInput(inputId = "Neighborhood", label="Choose neighborhood of house",
                choices = list("Blmngtn"="Blmngtn","Blueste"="Blueste","BrDale"="BrDale"
                               # "BrkSide"=BrkSide,"ClearCr"=ClearCr,"CollgCr"=CollgCr,
                               # "Crawfor"=Crawfor,"Edwards"=Edwards,"Gilbert"=Gilbert,
                               # "Greens"=Greens,"GrnHill"=GrnHill,"IDOTRR"=IDOTRR,
                               # "Landmrk"=Landmrk,"MeadowV"=MeadowV,"Mitchel"=Mitchel,
                               # "NAmes"=NAmes,"NoRidge"=NoRidge,"NPkVill"=NPkVill,
                               # "NridgHt"=NridgHt,"NWAmes"=NWAmes,"OldTown"=OldTown,
                               # "Sawyer"=Sawyer,"SawyerW"=SawyerW,"Somerst"=Somerst,
                               # "StoneBr"=StoneBr,"SWISU"=SWISU,"Timber"=Timber,"Veenker"=Veenker)
                )),
    numericInput(inputId="TotalArea", label="Enter total area of property", 
                 value=0,min=0, max=10000000),
    actionButton("Enter", "Enter Values"),
    actionButton("btn", "Calculate"),
    verbatimTextOutput("modelSummary")
)

server = function(input,output, session){
    x <- eventReactive( input$Enter, {
        Gr.Liv.Area = input$GrLivArea
        Area.Inside = input$AreaInside
        Neighborhood = as.factor(input$Neighborhood)
        Total.Area = input$TotalArea
        Overall.Qual = as.factor(input$OverallRate)
        t = data.frame(Gr.Liv.Area, Area.Inside, Neighborhood,Total.Area,Overall.Qual)
        
        # bst = xgboost(data.matrix(training), 
        #               label = training$SalePrice, verbose=0, max.depth = 2, 
        #               eta = 0.1, gamma=0, nrounds = 500, colsample_bytree=0.8, min_child_weight=3)
        
        bst = xgboost(data = agaricus.train$data, label = agaricus.train$label, 
                      max_depth = 2, eta = 1, nthread = 2, nrounds = 2, 
                      objective = "binary:logistic")
        predict(bst, agaricus.test$data)
          
    })
    
eventReactive(input$btn, 
              {
                  output$modelSummary <- renderPrint({
                    x()  
                  })
                  
              })
}
shinyApp(ui=ui, server=server)

However, if I use observeEvent the code works.

library(shiny)
library(xgboost)

data(agaricus.train, package='xgboost')
data(agaricus.test, package='xgboost')

ui=fluidPage(
    numericInput(inputId="GrLivArea", label="Enter Ground Living Area", 
                 value=0,min=0, max=100000),
    numericInput(inputId="OverallRate", label="Enter Overall rating", 
                 value=1,min=1, max=20),
    numericInput(inputId="AreaInside", label="Enter Area Inside", 
                 value=0,min=0, max=100000),
    selectInput(inputId = "Neighborhood", label="Choose neighborhood of house",
                choices = list("Blmngtn"="Blmngtn","Blueste"="Blueste","BrDale"="BrDale"
                               # "BrkSide"=BrkSide,"ClearCr"=ClearCr,"CollgCr"=CollgCr,
                               # "Crawfor"=Crawfor,"Edwards"=Edwards,"Gilbert"=Gilbert,
                               # "Greens"=Greens,"GrnHill"=GrnHill,"IDOTRR"=IDOTRR,
                               # "Landmrk"=Landmrk,"MeadowV"=MeadowV,"Mitchel"=Mitchel,
                               # "NAmes"=NAmes,"NoRidge"=NoRidge,"NPkVill"=NPkVill,
                               # "NridgHt"=NridgHt,"NWAmes"=NWAmes,"OldTown"=OldTown,
                               # "Sawyer"=Sawyer,"SawyerW"=SawyerW,"Somerst"=Somerst,
                               # "StoneBr"=StoneBr,"SWISU"=SWISU,"Timber"=Timber,"Veenker"=Veenker)
                )),
    numericInput(inputId="TotalArea", label="Enter total area of property", 
                 value=0,min=0, max=10000000),
    actionButton("Enter", "Enter Values"),
    verbatimTextOutput("modelSummary")
)

server = function(input,output, session){
    observeEvent( input$Enter, {
        Gr.Liv.Area = input$GrLivArea
        Area.Inside = input$AreaInside
        Neighborhood = as.factor(input$Neighborhood)
        Total.Area = input$TotalArea
        Overall.Qual = as.factor(input$OverallRate)
        t = data.frame(Gr.Liv.Area, Area.Inside, Neighborhood,Total.Area,Overall.Qual)
        
        # bst = xgboost(data.matrix(training), 
        #               label = training$SalePrice, verbose=0, max.depth = 2, 
        #               eta = 0.1, gamma=0, nrounds = 500, colsample_bytree=0.8, min_child_weight=3)
        
        bst = xgboost(data = agaricus.train$data, label = agaricus.train$label, 
                      max_depth = 2, eta = 1, nthread = 2, nrounds = 2, 
                      objective = "binary:logistic")
        
        output$modelSummary <- renderPrint({
            predict(bst, agaricus.test$data)
        })
    })
}
shinyApp(ui=ui, server=server)
library(shiny)
library(xgboost)

data(agaricus.train, package='xgboost')
data(agaricus.test, package='xgboost')

ui=fluidPage(
    numericInput(inputId="GrLivArea", label="Enter Ground Living Area", 
                 value=0,min=0, max=100000),
    numericInput(inputId="OverallRate", label="Enter Overall rating", 
                 value=1,min=1, max=20),
    numericInput(inputId="AreaInside", label="Enter Area Inside", 
                 value=0,min=0, max=100000),
    selectInput(inputId = "Neighborhood", label="Choose neighborhood of house",
                choices = list("Blmngtn"="Blmngtn","Blueste"="Blueste","BrDale"="BrDale"
                               # "BrkSide"=BrkSide,"ClearCr"=ClearCr,"CollgCr"=CollgCr,
                               # "Crawfor"=Crawfor,"Edwards"=Edwards,"Gilbert"=Gilbert,
                               # "Greens"=Greens,"GrnHill"=GrnHill,"IDOTRR"=IDOTRR,
                               # "Landmrk"=Landmrk,"MeadowV"=MeadowV,"Mitchel"=Mitchel,
                               # "NAmes"=NAmes,"NoRidge"=NoRidge,"NPkVill"=NPkVill,
                               # "NridgHt"=NridgHt,"NWAmes"=NWAmes,"OldTown"=OldTown,
                               # "Sawyer"=Sawyer,"SawyerW"=SawyerW,"Somerst"=Somerst,
                               # "StoneBr"=StoneBr,"SWISU"=SWISU,"Timber"=Timber,"Veenker"=Veenker)
                )),
    numericInput(inputId="TotalArea", label="Enter total area of property", 
                 value=0,min=0, max=10000000),
    actionButton("Enter", "Enter Values"),
    actionButton("btn", "Calculate"),
    verbatimTextOutput("modelSummary")
)

server = function(input,output, session){
    x <- eventReactive( input$Enter, {
        Gr.Liv.Area = input$GrLivArea
        Area.Inside = input$AreaInside
        Neighborhood = as.factor(input$Neighborhood)
        Total.Area = input$TotalArea
        Overall.Qual = as.factor(input$OverallRate)
        t = data.frame(Gr.Liv.Area, Area.Inside, Neighborhood,Total.Area,Overall.Qual)
        
        # bst = xgboost(data.matrix(training), 
        #               label = training$SalePrice, verbose=0, max.depth = 2, 
        #               eta = 0.1, gamma=0, nrounds = 500, colsample_bytree=0.8, min_child_weight=3)
        
        bst = xgboost(data = agaricus.train$data, label = agaricus.train$label, 
                      max_depth = 2, eta = 1, nthread = 2, nrounds = 2, 
                      objective = "binary:logistic")
        return(predict(bst, agaricus.test$data))
        
    })
    
    y <- eventReactive(input$btn, 
                  {
                      return(x())
                      
                  })
    
    output$modelSummary <- renderPrint({
        y()  
    })
    
}
shinyApp(ui=ui, server=server)

Would that work?

I created an reactive expression outside the render function.

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.