selectInput fails to select (always resets) with Shiny > 10.0

880 views
Skip to first unread message

Michael Conklin

unread,
Jan 13, 2015, 11:52:31 AM1/13/15
to shiny-...@googlegroups.com
I have a number of apps that after uploading a datafile read the variable names and utilize selectInput controls to select independent and dependent variables.  All of these controls seem to break with shiny 10.1 and 10.2.  The controls are populated but as soon as you select an item the control resets. This happens both using the selectize.js version or the html version.  Our server is currently running shiny 9.1 which behaves normally. 

Here is the relevant part of the server.R file.

shinyServer(function(input,output){
  library(foreign)
  # Drop-down selection box for which data set
 # output$choose_dataset <- renderUI({
#    fileInput("file1", "Choose Input File")
#  })
  
 Data<-reactive({
   inFile<-input$file1
   if(is.null(inFile))
     return(NULL)
   depvar<-input$DepVar
   indvars<-input$IndVars
   wtvar<-input$WeightVar
   gd<-num.dec$nd
   df.raw<-ReadFile(inFile$datapath)
   if(is.null(input$DepVar)){
     depvar<-names(df.raw)[1]
   }
   if(is.null(input$IndVars)){
     indvars<-names(df.raw)[-1]
   }
#     print(paste(depvar,paste(indvars,collapse="+"),sep="~"))
   if(input$runstatus==0){
     mod1<-NULL
   }
   if(input$runstatus==1){
     
   #fmla<-as.formula(paste(depvar,paste(indvars,collapse="+"),sep="~"))
  # print(depvar)
  # print(indvars)
  # print(names(df.raw))
   fmla<-as.formula(df.raw[c(depvar,indvars)])
   bindv<-FALSE
   if(length(table(df.raw[[depvar]]))==2){bindv<-TRUE}
   if(wtvar=="None"){
      wtvar<-NULL
   } else {
     wtvar<-df.raw[[wtvar]]
   }
   
   mod1<-shapley.reg(fmla,mydata=df.raw,binaryDV=bindv,myweights=wtvar)
 }
   info<-list(df.raw=df.raw,mod1=mod1)
   return(info)
   
 })
getdigits<-observe({
  tx<-input$runstatus
  num.dec$nd<<-as.numeric(input$numdec)
  info<-list(nd=as.numeric(input$numdec))
  return(info)
})
output$raw<-renderPrint({
  if(is.null(input$file1)){return()}
  summary(Data()$df.raw)
})
output$DepVar<-renderUI({
  if(is.null(input$file1)){return()}
  selectInput('DepVar','Dependent Variable',choices=names(Data()$df.raw),
                     selected=names(Data()$df.raw)[1])
})
 output$IndVars<-renderUI({
   if(is.null(input$file1)){return()}
   vars<-names(Data()$df.raw)
   depvar<-input$DepVar
   selectInput('IndVars','Independent Variables',choices=vars,
                      selected=vars[-match(depvar,vars)],multiple=TRUE,selectize=FALSE)
 })
  output$WeightVar<-renderUI({
    if(is.null(input$file1)){return()}
    vars<-names(Data()$df.raw)
    wtvar<-input$WeightVar
    selectInput('WeightVar','Weighting Variable',choices=c("None",vars),
                       selected="None")
  })


Thanks for your help

Yihui Xie

unread,
Jan 14, 2015, 12:00:27 PM1/14/15
to Michael Conklin, shiny-discuss
Could you prepare a minimal, self-contained, and reproducible example?
There are way too many unknown factors in your code, and it is
difficult to solve a problem by only imagination. Thanks!

Regards,
Yihui

Michael Conklin

unread,
Jan 14, 2015, 5:14:28 PM1/14/15
to shiny-...@googlegroups.com, michael.c...@gmail.com
In the attached zip file are ui.r, server.r and a test data csv file to upload.  The basic problem is that the selectInput drop downs for Dependent Variable and Independent Variables and weight variable reset after each selection.  This behavior did not occur in shiny versions < 10, i.e. verion 9.1 on our production server does not exhibit this behavior for the same code.
Example.zip

Michael Conklin

unread,
Jan 23, 2015, 9:12:25 AM1/23/15
to shiny-...@googlegroups.com, michael.c...@gmail.com
Does anyone have any ideas?  This code worked perfectly under Shiny 9.1, I could upload data and then use the selectInput control to set the dependent variable, the independent variables and the weight variable. Now, with Shiny 10 if I try to select anything in the selectInput controls the control immediately returns to it's default state.  

Yihui Xie

unread,
Mar 16, 2015, 3:54:14 PM3/16/15
to Michael Conklin, shiny-discuss
I do not know why it worked before, but it seems to me that it should
not have worked -- there is cyclic dependency in your code:

input$DepVar <- output$DepVar <- Data() <- input$DepVar

So when the select input changes, output$DepVar will be regenerated,
hence input$DepVar is regenerated as well, and because it is
regenerated without a pre-selected value, it always uses the first
value. Although it looks a little weird, you can use input$DepVar as
the pre-selected value, e.g.

selectInput('DepVar','Dependent Variable',choices=names(Data()$df.raw),
selected = input$DepVar,
selectize=FALSE)


Regards,
Yihui


On Fri, Jan 23, 2015 at 8:12 AM, Michael Conklin
Reply all
Reply to author
Forward
0 new messages