1

I am running a regression model using an available training dataset, say 1000 observations and 5 independent variables.

Now I want to run this model against a test dataset. So I am trying to fetch test dataset manually from user on Shiny UI. User should have an option to select number of observations in test dataset, for e.g. 50 observations. Once the user provides number of observations, user should be able to view a table of 50 rows and independent variables (or any other view). User should now be able to enter arbitrary values manually in these 50 rows for each independent variable on Shiny UI.

Currently, I am able to fetch only 1 observation from user for each variable after running below code for UI.

Code:

library(shiny)



  ui<-shinyUI(fluidPage(
    titlePanel("Model"),


    sidebarLayout(
      sidebarPanel(
      sliderInput("Var1",label="Variable 1",min=100,max=1000,value=200,step=1),
      sliderInput("Var2",label="Variable 2",min=500,max=800,value=600,step=1),
      sliderInput("Var3",label="Variable 3",min=0,max=1,value=0.5,step=0.01),
      sliderInput("Var4",label="Variable 4",min=1,max=20,value=5,step=1),
      sliderInput("Var5",label="Variable 5",min=0,max=1,value=0.6,step=0.01),
      actionButton("Run_model", "Run model")
      ),

      mainPanel(

        tabsetPanel(
        tabPanel("model summary", verbatimTextOutput('summary'))
        )
      )
    )))


  server<- function(input,output,session){
    set.seed(1234)
    observe({
      # this is how you fetch the input variables from ui component

      Var1 <- as.integer(input$Var1)
      Var2 <- as.integer(input$Var2)
      Var3 <- as.numeric(input$Var3)
      Var4 <- as.integer(input$Var4)
      Var5 <- as.numeric(input$Var4)

      test <- cbind(Var1, Var2, Var3, Var4, Var5)
      test <- as.data.frame(test)

      ## Import dataset
      train <- data.frame(replicate(6,sample(1:100,1000,rep=TRUE)))
      names(train) <- c("Var1", "Var2", "Var3", "Var4", "Var5", "Var6")

      # Var6 is the dependent variable
      test$Var6 <- ""

      # Model action button
      observeEvent(input$Run_model, {
        model <- randomForest(Var6 ~ .,  data = train, ntree=500)
        pred <- predict(model, newdata = test)
        output$summary <- renderPrint(model)

      })
    })
  }

shinyApp(ui=ui, server=server)
5
  • 1
    Can you also provide your server function, so we can run the code? Commented Jan 12, 2018 at 14:21
  • 1
    there are still several mistakes in your code preventing us from running it. Double-check your quotes in the ui argument; we don't have access to data.csv; in test <- as.data.frame(new) new was not defined anywhere else. Commented Jan 12, 2018 at 14:49
  • @MLavoie, can you please check it now, I have made the rectifications. Commented Jan 12, 2018 at 15:00
  • almost done. But we still can't have access to train <- read.csv("M:\\Sales forecasting Model\\Abhay\\data.csv") which is on your local computer. You can use dput() (google it) to add a subset of this train dataset. Commented Jan 12, 2018 at 15:03
  • @MLavoie I am now creating sample train dataset in the code. Could you please assist me with possible solution. Commented Jan 15, 2018 at 6:26

1 Answer 1

1

Hi you want to look over how to use observe and reactive. Simply speaking - Observe are when you want to modify some UI object or Input or generally do something in the background. Calculations for outputs are better placed in reactive expressions.

I have modified your code and like this it is running.

library(shiny)
library(randomForest)


ui<-shinyUI(fluidPage(
  titlePanel("Model"),


  sidebarLayout(
    sidebarPanel(
      sliderInput("Var1",label="Variable 1",min=100,max=1000,value=200,step=1),
      sliderInput("Var2",label="Variable 2",min=500,max=800,value=600,step=1),
      sliderInput("Var3",label="Variable 3",min=0,max=1,value=0.5,step=0.01),
      sliderInput("Var4",label="Variable 4",min=1,max=20,value=5,step=1),
      sliderInput("Var5",label="Variable 5",min=0,max=1,value=0.6,step=0.01),
      actionButton("Run_model", "Run model")
    ),

    mainPanel(

      tabsetPanel(
        tabPanel("model summary", verbatimTextOutput('summary'))
      )
    )
  )))


server<- function(input,output,session){
  set.seed(1234)
  test <- reactive({
    # this is how you fetch the input variables from ui component

    Var1 <- as.integer(input$Var1)
    Var2 <- as.integer(input$Var2)
    Var3 <- as.numeric(input$Var3)
    Var4 <- as.integer(input$Var4)
    Var5 <- as.numeric(input$Var4)

    test <- cbind(Var1, Var2, Var3, Var4, Var5)
    test <- as.data.frame(test)


    # Var6 is the dependent variable
    test$Var6 <- ""
    test
    # Model action button
  })
  train <- reactive({
    ## Import dataset
    train <- data.frame(replicate(6,sample(1:100,1000,rep=TRUE)))
    names(train) <- c("Var1", "Var2", "Var3", "Var4", "Var5", "Var6")
    train
  })
  pred <- eventReactive(input$Run_model, {
    model <- randomForest(Var6 ~ .,  data = train(), ntree=500)
    predict(model, newdata = test())

  })
  output$summary <- renderText(pred())
}

shinyApp(ui=ui, server=server)

Hope this helps!

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your help. Cheers!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.