Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R shiny error: Cannot coerce type 'closure' to vector of type 'double'

Tags:

r

I would like to take in a vector of numbers as input and then simply plot the histogram. Here is my R code:

ui.R:

library(shiny)

shinyUI(pageWithSidebar(

  headerPanel("Hello Shiny!"),

  sidebarPanel(selectInput("Vector", "Select Numbers", c(1,2,3,4), selected = NULL, multiple = TRUE)),

  mainPanel( plotOutput("plotVector"))
))

Server.R:

library(shiny)
shinyServer(function(input, output) {

v<- function()
 {
  v <- rnorm(input$Vector)#take vector as input
}

  output$plotVector <- renderPlot({  hist(as.numeric(v))}) 

})

code to run the app:

library(shiny)
runApp("C:/Users/me/Desktop/R Projects/testShiny")

When I run this I am getting the error "Cannot coerce type 'closure' to vector of type 'double'"

Can you help? Thank you.

like image 545
user3022875 Avatar asked Dec 06 '13 17:12

user3022875


2 Answers

On the server side, you define v as a function:

v<- function()
 {
  v <- rnorm(input$Vector)#take vector as input
}

and then you try to use it as the argument to as.numeric(...):

output$plotVector <- renderPlot({  hist(as.numeric(v))}) 

so R is trying to convert something of class: function to double.

Edit: to answer OP's followup question. With the following for ui.R and server.R:

On the server side, shinyUI(...) takes two objects which are passed automatically: input and output. The properties of input ("columns" in R terminology) are defined in ui.R by creating various GUI objects. So you create a select object with a call to selectInput(...). The object's id is "Vector". This is referenced on the server side as: input$Vector. Note that what you are calling Vector is actually a single number: whatever the user selects in the select box. Plotting the histogram of a single number is meaningless, so I changed the code to make input$Vector the mean of a normal distribution. You also had the problem that input$Vector was initialized to NULL in your code, which threw an error. So I changed that to initialize to 0.

The statement:

output$mainplot <- ...

on the server side populates an object output$main_plot in ui.R, which is defined by the statement:

... plotOutput("main_plot")...

Rolling it all up, the following:

ui.R:

library(shiny)
shinyUI(pageWithSidebar(
  headerPanel("Hello Shiny!"),
  sidebarPanel(selectInput("Vector", "Select Mean of Distribution", c(0,1,2,3,4), selected = 0, multiple = TRUE)),
  mainPanel( plotOutput("main_plot"))
))

server.R:

library(shiny)
shinyServer(function(input, output) {
  v<- function() {
    return(rnorm(100,mean=as.numeric(input$Vector)))  
  }
  output$main_plot <- 
    renderPlot( 
      hist(v(), breaks=10, xlab="",
           main="Histogram of 100 Samples\n taken from: N[mean, sd=1]")) 
})

Generates this:

like image 158
jlhoward Avatar answered Nov 12 '22 01:11

jlhoward


It looks like doing this works!

library(shiny)
shinyServer(function(input, output) {

v<- function()
 {
  v <- rnorm(input$Vector)#take vector as input
}

  output$plotVector <- renderPlot({  
data <- v()
hist(data)
    }) 

})
like image 2
user3022875 Avatar answered Nov 12 '22 02:11

user3022875