Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having Problems with sliderInput in R Shiny - Getting NaNs

I'm trying to make a slider input that has years between 2005 and 2040. It seems pretty simple, right? Normally it works fine, but every once in a while I will pull the slider too far to the left and it gives me NaN, which tends to crash things. I've tried to set up the rest of my code so that it doesn't have a problem, but it's still bugging me. I've scoured the Internet for explanations, but so far nothing. Here's my ui.R:

library(shiny)

shinyUI(fluidPage(
  titlePanel("Test"),

  sidebarLayout(
  sidebarPanel(

    selectInput("over", "Indicator", c("Indicator 1", "Indicator 2"), selected="Trade"),

    selectInput("type", "Type", c("Discrete", "Continuous")),


    # Nothing particularly unusual here...   
    sliderInput("year", "Year", min=2005, max=2040, value=2005, animate=animationOptions(interval=1500), sep=""),

    checkboxInput("table", "Show Table")

    , width=3),

  mainPanel(    
    uiOutput("plot"),

    uiOutput("showtable")

    , width=9)
  )
))
like image 902
Caroline Avatar asked Jan 09 '23 19:01

Caroline


1 Answers

I think the problem might lie with your sep parameter. Is that supposed to be step? If so, it needs to be either a number or NULL. I changed that line to the following:

sliderInput("year", "Year", min=2005, max=2040, value=2005, animate=animationOptions(interval=1500), step=1),

It worked fine. At least, I was able to drag as far left as I wanted without any issue. On the other hand, I don't have your server.R file, so if the problem is there, or with an interaction between the files, I wouldn't see it.

Update:

The problem is with your version of Shiny. I discovered this because the sep parameter was an "unused argument" when I tried to run the app. I originally thought this was a typo, but your certainty (in your recent comment) caused me to check with the documentation for sliderInput. You are totally right, sep is a valid parameter. I figured this meant my version of the shiny package was out of date, so I updated it. After I updated it, not only did sep parameter get accepted, but I was able to replicate your issue. I found that if I attempted to drag the slider off of the browser window, its value would change to NaN. Also, the slider looks much prettier. I am guessing this has to do with a change in the code for the sliderInput function. I will take a look at that and update again if I find the change.

Update2

I found the issue. In the latest version of Shiny, they decided to use a range slider written in jQuery called ion.rangeSlider. Specifically, they used version 2.0.2. Unfortunately, that version had an issue with returning NaN when the left slider was pulled off of the browser window (sound familiar?). That has been fixed in the most recent version of ion.rangeSlider (2.0.6). I hope that the next release of the shiny package incorporates the most recent version of ion.rangeSlider.

I know this doesn't actually solve your problem. However, the following will:

  1. Figure out where your R packages are installed
  2. Go to the subfolder shiny\www\shared\ionrangeslider\css
  3. Copy the file ion.rangeSlider.skinShiny.css to somewhere else
  4. delete the contents of shiny\www\shared\ionrangeslider
  5. Download the .zip of ion.rangeSlider here
  6. Copy the contents of ion.rangeSlider-master into the ionrangeslider folder mentioned in step 2
  7. Move ion.rangeSlider.skinShiny.css back into shiny\www\shared\ionrangeslider\css

Then just detach and reload the shiny package, and you should be all set.

like image 190
Paul de Barros Avatar answered Jan 11 '23 17:01

Paul de Barros