nate
nate

Reputation: 1244

Get maximum and minimum values for input$variable shiny

Right now I am using my own data, but the values are numeric...let's just pretend I am using the mtcars dataset that comes with R.

I would like to:

  1. allow my user to select a numeric variable from a dataframe via a drop down menu

  2. have the computer calculate the minimum and maximum values for said variable

  3. allow my user to input a number between 1 and 50 from a slider input

  4. Divide the difference between the maximum and minimum of the first variable by the value obtained from the slider input

The first error I got was:

  1. Non-numeric argument to binary operator

So I tried to coerce the variable using as.numeric(), but this led to my second error:

  1. NAs introduced by coercion

Putting a print(max(input$Var) line in my server.R file gives me the variable name instead of a number.

Any help with this would be greatly appreciated. I have posted part of my ui.r and server.r files below. Thank you in advance.

ui.R:

library(shiny)
load('mtcars')
shinyUI(pageWithSidebar(  
  headerPanel("MtCars Interactive Analysis Tool"),
  sidebarPanel(
selectInput(inputId = "histVar",
label= "Pick Your Variable",
choices=names(mtcars),
selected = NULL)),

sliderInput(inputId = "binNum",
label = h5("Number of bins for Histogram--1 through 50 are allowed"),
min = 1,
max = 50,
value = 15, 
step = 1)

mainpanel(...blah blah blah) 

server.R:

library(shiny)
load("mtcars")


shinyServer(function(input, output) {

output$histPlot <- renderPlot({

  varRangeGet <- range(input$histVar)
  #print(varRangeGet)
  #print(class(varRangeGet))
  #print(max(input$histVar))
  realRange<-(varRangeGet[[2]]- varRangeGet[[1]])
  binwidthX <- realRange/input$binNum

# Errors out...can't continue

})


})

Again, thank you for any help on this...I don't understand why I can't perform simple calculations on an input variable within Shiny....

Upvotes: 1

Views: 7527

Answers (2)

Rohit Das
Rohit Das

Reputation: 2042

I like to start out simple so here is your code modified into a single script. First see if you can get this going. Works on my system. I have never seen anyone load data in the ui.R code. If you need to make your drop down dynamic then you need to utilize uiOutput, load data in server.R and then render it in the ui. Anyway first try running this code.

library(shiny)
runApp(list(
  ui = pageWithSidebar(  
    headerPanel("MtCars Interactive Analysis Tool"),
    sidebarPanel(
      selectInput(inputId = "histVar",
                  label= "Pick Your Variable",
                  choices=names(mtcars),
                  selected = NULL)),

    sliderInput(inputId = "binNum",
                label = h5("Number of bins for Histogram--1 through 50 are allowed"),
                min = 1,
                max = 50,
                value = 15, 
                step = 1)),
  server = function(input, output) {
    output$histPlot <- renderPlot({

    varRangeGet <- range(input$histVar)
    #print(varRangeGet)
    #print(class(varRangeGet))
    #print(max(input$histVar))
    realRange<-(varRangeGet[[2]]- varRangeGet[[1]])
    binwidthX <- realRange/input$binNum

    # Errors out...can't continue

  })
  }
),port = 3300)

Upvotes: 2

MrFlick
MrFlick

Reputation: 206382

input$histVar allows you get at the value that's stored in your selectInput named "histVar". You've filled that select with choices=names(mtcars) so the only thing you'll get back is one of the names of the columns of mtcars as a character vector. This is just telling you what value is selected.

There's no connection between input and your dataset. The input just comes from your UI. If you want to get information out of mtcars using that variable, you do so the same way you would use a string with a column name in R.

vals <- mtcars[, input$histVar]
range(vals)
max(vals)

etc...

Upvotes: 4

Related Questions