2
votes

I am trying develop an application in RShiny. My Objective: An application which can do logistic regression and display the output.

Steps:

  1. User will upload the CSV(1st TAB)
  2. User select the independent variable(2nd TAB)
  3. User Select the other variables(2nd TAB)
  4. Mainpanel in 2nd TAB will display the Summary of logistic regression.

My Code:

library(shiny)
ui<-navbarPage("Model Developement by Subhasish",
               tabPanel("Data Import",sidebarLayout(sidebarPanel( fileInput("file","Upload your CSV",multiple = FALSE),
                                                                  tags$hr(),
                                                                  h5(helpText("Select the read.table parameters below")),
                                                                  checkboxInput(inputId = 'header', label = 'Header', value = FALSE),
                                                                  checkboxInput(inputId = "stringAsFactors", "stringAsFactors", FALSE),
                                                                  radioButtons(inputId = 'sep', label = 'Separator', choices = c(Comma=',',Semicolon=';',Tab='\t', Space=''), selected = ',')
               ),
               mainPanel(uiOutput("tb1"))
               ) ),
               tabPanel("Model_dev",sidebarLayout(sidebarPanel(uiOutput("model_select"),uiOutput("var1_select"),uiOutput("rest_var_select")),mainPanel( helpText("Your Selected variables"),verbatimTextOutput("other_val_show"))))
)
server<-function(input,output) { data <- reactive({
  file1 <- input$file
  if(is.null(file1)){return()} 
  read.table(file=file1$datapath, sep=input$sep, header = input$header, stringsAsFactors = input$stringAsFactors)

})  
output$table <- renderTable({
  if(is.null(data())){return ()}
  data()
})
output$tb1 <- renderUI({
  tableOutput("table")
})
output$model_select<-renderUI({
  selectInput("modelselect","Select Algo",choices = c("Logistic_reg"="logreg","SVM"="svm"))
})
output$var1_select<-renderUI({
  selectInput("ind_var_select","Select Independent Var", choices =as.list(names(data())),multiple = FALSE)
})
output$rest_var_select<-renderUI({
  checkboxGroupInput("other_var_select","Select other Var",choices =as.list(names(data())))
})
output$other_val_show<-renderPrint({
  input$other_var_select

  #f<-data()
  #library(caret)
  #logreg<-glm(f[,1]~.,family = binomial,data=f)
  #summary(logreg)

})

}
shinyApp(ui=ui,server=server)

Till now The CSV Upoload part is complete. Problem faced as glm() function req structure like: glm(var 1~ var 2+var 3+ var 4,family=binomial,data=df)

How can I use the checkbox values like var 2+ var 3..? I am using Shiny R from last 1 week. so there might be any easier solution which I am not able to discover.

Thanks in advance

1
Had a preliminary answer, but I changed it. This is probably what you want. - Mike Wise

1 Answers

3
votes

You were very close, once I understood what you were trying to do it only needed a couple of lines to complete it. It is a nice example of loading a dataframe into shiny, selecting the columns in that frame for a glm, and them performing it.

You can use string variables in glm by using the as.forumla function. See Eric Green's example http://stackoverflow.com/questions/17024685/how-to-use-a-character-string-in-formula ,

I messed around with your program and got this to work - note it needs a column with values between 0 and 1 to work - I used the randu dataset (write.csv(randu,"randu.csv"):

library(shiny)
ui<-navbarPage("Model Developement by Subhasish",
               tabPanel("Data Import",
                        sidebarLayout(sidebarPanel( fileInput("file","Upload your CSV",multiple = FALSE),
                              tags$hr(),
                              h5(helpText("Select the read.table parameters below")),
                              checkboxInput(inputId = 'header', label = 'Header', value = FALSE),
                              checkboxInput(inputId = "stringAsFactors", "stringAsFactors", FALSE),
                                          radioButtons(inputId = 'sep', label = 'Separator', 
                                         choices = c(Comma=',',Semicolon=';',Tab='\t', Space=''), selected = ',')
               ),
               mainPanel(uiOutput("tb1"))
               ) ),
               tabPanel("Model_dev",
                        sidebarLayout(sidebarPanel(
                          uiOutput("model_select"),
                          uiOutput("var1_select"),
                          uiOutput("rest_var_select")),
                          mainPanel( helpText("Your Selected variables"),
                                     verbatimTextOutput("other_val_show"))))
)
server<-function(input,output) { data <- reactive({
  file1 <- input$file
  if(is.null(file1)){return()} 
  read.table(file=file1$datapath, sep=input$sep, header = input$header, stringsAsFactors = input$stringAsFactors)

})  
output$table <- renderTable({
  if(is.null(data())){return ()}
  data()
})
output$tb1 <- renderUI({
  tableOutput("table")
})
output$model_select<-renderUI({
  selectInput("modelselect","Select Algo",choices = c("Logistic_reg"="logreg","SVM"="svm"))
})
output$var1_select<-renderUI({
  selectInput("ind_var_select","Select Independent Var", choices =as.list(names(data())),multiple = FALSE)
})
output$rest_var_select<-renderUI({
  checkboxGroupInput("other_var_select","Select other Var",choices =as.list(names(data())))
})
output$other_val_show<-renderPrint({
  input$other_var_select
  input$ind_var_select
  f<-data()

  library(caret)
  form <- sprintf("%s~%s",input$ind_var_select,paste0(input$other_var_select,collapse="+"))
  print(form)

  logreg <-glm(as.formula(form),family=binomial(),data=f)
  print(summary(logreg))

})

}
shinyApp(ui=ui,server=server)

Here the data is loaded from randu.csv enter image description here

And here is the configurable glm:

enter image description here