2

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

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Subhasish1315
  • 938
  • 1
  • 10
  • 25

1 Answers1

3

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

Mike Wise
  • 22,131
  • 8
  • 81
  • 104
  • Thanks Mike,for your code & guidance...I just added few lines in the output$renderprint section........ `if(is.null(input$other_var_select)){"You have to select altleast one variable except independent one"} else{ 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)) }` – Subhasish1315 Dec 15 '16 at 07:16
  • @MikeWise..I have a small question in here...logreg holds the logistic regression output ..so can I use it in another output function or to display another property like varimp for variable importance i need to rebuild the logistic regression again?? – Subhasish1315 Dec 15 '16 at 07:47
  • 1
    A useful hack is to make it global by using the "<<-" operator. Then you can reuse it. – Mike Wise Dec 15 '16 at 08:11
  • Thanks @Mike..for the quick tips..but its not updating..rather it holds the last output of original...can it refresh or change automatically?? – Subhasish1315 Dec 15 '16 at 09:46
  • It should do so. I use that technique a lot. Try updating a simple counter variable and printing it out somewhere to check how it works - maybe in a small separate program. If you can't get that to work post that as another question. – Mike Wise Dec 15 '16 at 10:46