r - cannot coerce type 'closure' to vector of type 'character' -


i trying build interactive scatter-plot using shiny. using iris data, have user select x , y dimensions of scatter plot *petal vs sepal) , output simple scatter plot of selected dimensions. pretty straightforward.

first needed build function allows me pass strings representing dimensions ggplot. did , tested static data. works fine.

next define 2 dropdowns , 2 subsequent strings (using shiny) petal , sepal dimensions (these x , y axis).

i next set 2 string variables using shiny's reactive() function using switch statement.

this appears things go wrong.

the error is: error: cannot coerce type 'closure' vector of type 'character'

i've taken number of steps debug code. first plugged in hard coded dimensions (e.g. "petal.length") final line of code output$myplot = renderplot({myplotfunct( ...

this works great. plot renders expect to.

i added debug line track value of string passing plot function. bingo. it's empty. why empty?? seems should passed value ui.r file.

code below.

any appreciated. thanks!

ui.r

library(shiny)  # define ui dataset viewer application shinyui(fluidpage(  # application title titlepanel("shiny text"),  # sidebar controls select dataset , specify # number of observations view sidebarlayout(   sidebarpanel(     selectinput("dataset1", "choose sepal measure:",                choices = c("sepal length", "sepal width")),      selectinput("dataset2", "choose petal measure:",                choices = c("petal length", "petal width"))  ),   # main scatter plot  mainpanel(     textoutput("testvar"),     plotoutput("myplot")    )   ) )) 

server.r

library(shiny) library(datasets)  library(ggplot2)  #define function plot passed string variables in ggplot  myplotfunct = function(df, x_string, y_string) {   ggplot(df, aes_string(x = x_string, y = y_string)) + geom_point() }  shinyserver(function(input, output) {  # sepal inputs datasetinput1 <- reactive({   switch(input$dataset1,        "sepal length" = "sepal.length",        "sepal width" = "sepal.width") })  # petal inputs datasetinput2 <- reactive({   switch(input$dataset2,        "petal length" = "petal.length",        "petal width" = "petal.width") })  #debug print value of sting being passed output$testvar = rendertext(print(datasetinput1))  # plot output$myplot = renderplot({myplotfunct(iris, datasetinput1, datasetinput2)})  }) 
share|improve question
1  
do need use datasetinput1(), datasetinput2() in myplotfunct() call? – tospig apr 23 '15 @ 2:53
up vote 7 down vote accepted

the call datasetinput1 , datasetinput2 in 2 last lines reason of error.

you should instead call datasetinput1() , datasetinput2(). otherwise r tries convert function char.

just adding (), error disappears can see below:

enter image description here

share|improve answer

your answer

 
discard

posting answer, agree privacy policy , terms of service.

not answer you're looking for? browse other questions tagged or ask own question.