Rectangle 27 0

EuropeWater_tree <- rpart(EuropeWater$water_class ~ 
 population+GDPpercapita + area_km2 + populationdensity + EuropeWater$climate, 
 data=EuropeWater, method="class")

adding quotes to the method type was exactly the problem. Thank you!!!

r - What does the rpart "Error in as.character(x) : cannot coerce type...

r rpart cart-analysis
Rectangle 27 0

I've found this error often occurs when you're running a function that's meant to be run in server.R in ui.R.

So you'll have to remember that renderUI is going to create a full-fledged client-side UI. That means that any code you're returning in your dynamic UI (even though it happens to be stored in server.R must really be valid client-side code that could exist in ui.R. That also means that testing can be much easier in that you can just move the code you're trying to create dynamically (such as one of the out tabsetPanels) to your ui.R to see if it works. That may make debugging easier in the future.

In this case, I think the problem you'd see if you did that is around the renderPlot call. Since you're just creating a dynamic UI here, you want to use the UI-compatible functions, which is plotOutput("plotName"), in this case. Then on the server side you'll assign a plot to that output.

output$plotName <- renderPlot({
 ...
})

Which will do what you want when the plot is visible, and will have no visible effect when the plot isn't shown.

r - Shiny - renderPlot in renderUI - Error in as.character(x) : cannot...

r shiny shiny-server
Rectangle 27 0

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))})

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.

output$mainplot <- ...

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

... plotOutput("main_plot")...
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"))
))
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]")) 
})

So how do I return the data fromt he function and NOT the function itself? Any idea?Thank you.

@user3022875 Jhoward beat me to identifying the error by seconds :-), but your question misses the point: you need to remove the v<- from the body of your function. Then v(x) will return the vector of data which hist expects.

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

r
Rectangle 27 0

@BenBolker posted an answer as a comment, perhaps because he is hoping that you will stare at the difference between your code and his and get a Zen-moment. He also thinks, being the modest gentleman that he is, that it's too simple to warrant any upvotes. So I am feeling guilty that any upvotes are his rather than mine. I will now try to legitimately earn any upvotes.

The reason you got the error was the the install.packages function expects a character object, while you gave it the unquoted expression rattle. Ben use a single element character vector "rattle", thus conforming to the requirement of the function. Unlike some functions install.packages is not equipped to provide "non-standard evaluation` of its first argument.

R is a funny language at times with some inconsistent evaluation conventions. There are several functions where you can provide an unquoted expression and have it automatically converted to character. The list includes library, and its cousin, require, as well as help, subset, and $. These are considered "non-standard" evaluation by knowledgeable users and they can have their pitfalls in programming. The error message tells you that R tried to convert what it "thought" would be a language object, a closure (which loosely is an R and LiSP term for function) to a character and did not succeed. You can see the same error with this console interaction:

> as.character(mean)
Error in as.character(mean) : 
  cannot coerce type 'closure' to vector of type 'character'

If you look at the library function mentioned by Ben and scroll down past the interanl function definitions you eventual get to the meachanism whereby library avoids that error:

if (!character.only) 
            package <- as.character(substitute(package))

This would also avoid the error I intentionally used as an example:

> as.character(substitute(mean))
[1] "mean"

The substitute function is doing processing on a language element taken to be a closure and the resulting object an R "name" has an as.character method.

To be fair, it's rather confusing (I think) because functions like library() and require() try to make life easier for the user (mistakenly, I think) by allowing them to specify either a character vector or a symbol

I hope I was not being unfair in any way. I remember having exactly the same confusion when starting R. I just regretted needing to "answer" a question when you provided the insight. Oh, I get it, you want me to remove my close vote. I can try. I didn't use my imperial close powers because I couldn't find a real duplicate, despite searching.

(Copied from an answer by OP:) One more question: if you cannot install packages from CRAN and internet and you need to copy the packages locally first and install them from the zip, although this works one package at a time, but it's rather a painful manual process as you don't know what package depends on what. Is there any way to instruct "install.packages" to install the package and its dependencies from local or set the repos locally and is there any way to download all the available(or dependant packages) at the same time?

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

r rattle
Rectangle 27 0

You cannot have elements of different types in the same vector. But you can do it in a list. Maybe you could transform your vector in list direclty.

vekl <- as.list(vek)

for (i in 1: length(vekl)){
  temp <- as.numeric(vekl[[i]])
  if (! is.na(temp))
    vekl[[i]] <- temp
}

To avoid warnings, use this: suppressWarnings( temp <- as.numeric(vekl[[i]]) ). It ignores all warnings so you have to pay attention when you use this function...

It's a good idea, thank you. Maybe you know, how to avoid coersion warnings?

R coerce character vector into character and numeric - Stack Overflow

r vector character numeric coerce
Rectangle 27 0

This works in the current version of R (2.12.0) and version 0.91 of twitteR:

tw <- ldply(searchTwitter("rstats", session=getCurlHandle(), n=10), 
              function(x)  c(text=x@text, favorited=x@favorited, created=x@created, 
                             truncated=x@truncated, id=x@id, statusSource=x@statusSource, 
                             screenName=x@screenName )
                          )

I was getting the same error as you were until I switched over to the "@" operator for accessing the slot values.

Whenever I see code delving inside of objects with $ and @, I am compelled to issue a small warning ;-) It likely makes no difference, in this case, to use @ and grab the bits you want. But as a general use-case I think it is bad. If there are extractor functions available, users should be encouraged to use them. There are cases where what is stored in an object under a particular name may not be what the user expects (working, not actual, residuals for example with some models). 99 times out of 100 you will be fine, but that 1 time you might get your ass well and truly bitten.

@ Gavin: No disagreement. It was only after trying the method the OP used, failing at getting text(x) to work and looking at the documentation for that particular S4 class that I switched over.

r - Question on Plyr error: Error in as.double(y) : cannot coerce type...

r plyr
Rectangle 27 0

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

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

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

})

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

r
Rectangle 27 0

I'm still digging into the issue, but I think I've identified multiple causes of this issue. At their root, they all have to do with R expecting an S4 object but getting back an integer instead. I believe these are generally a result of the connection failing to establish.

Why is it failing? One thing I've noticed is that if you fail to close to many of your connections (~16 [see the number of maximum connections specified in the driver handle call] open) DBI won't/can't open a new connection. Make sure you are calling dbDisconnect as needed. Usually, this sort of problem results in a sensible error message, however sometimes results in the above referenced error. If possible access the DB through an abstraction layer, e.g. dplyr as some will monitor the db connections and kill them if they are inactive. Whereas, AFIK if you open a connection in a function and the function breaks, you have no way to close the open connection unless you returned the driver object from your initial call to dbConnect. In this case you have no choice but to restart your instance of R (possibly resetting your machine and clearing your workspace as well).

The other issue I recently encountered is that if RMySQL masks RPostgreSQL, then RPostgreSQL will fail. The reverse does not appear to be the case, but because others have mentioned RPostgreSQL in here as showing the same error message, it seemed worthy of note.

The latest version of RMySQL (0.10.1) seems to have finished off RPostgreSQL - RPostgreSQL now fails to work regardless of load order. The same people working on RMySQL appear to be working on RPostgres (https://github.com/rstats-db/RPostgres) and this conflict appears to be a non-issue if using that package instead of RPostgreSQL. Specifically, use RPostgres::Postgres() in the place of RPostgreSQL::PostgreSQL() when specifying the driver in dbConnect. Other packages, e.g. dplyr, currently assume RPostgreSQL, so this issue can still bite (but it seems a resolution is in the works (https://github.com/rstats-db/RMySQL/issues/28).

Any idea how RMySQL breaks RPostgreSQL, but not the other way around?

@Zach: Ping and warning not to update to RMySQL .10.1. See Update above.

Thanks for the update. I opened an issue in the RMySQL issue tracker, hopefully it will be fixed on CRAN soon! github.com/rstats-db/RMySQL/issues/28

dbConnect with R 3.0 on Ubuntu 12.04 x64 --Error in as.integer(from) :...

r
Rectangle 27 0

I would start by fixing the formula: remove the redundant EuropeWater as you already supply the data= argument:

res <- rpart(water_class ~ population + GDPpercapita + area_km2 + 
                           populationdensity + climate, 
             data=EuropeWater, method="class")

Also, make sure that all columns of your data.frame are of the appropriate type. Maybe some of the data read from the csv file was mistakenly read as a factor? A quick summary(EuropeWater) may reveal this.

I think that str could be better to quick view of `data.frame' content

Sure, or sapply(EuropeWater, class) to get class-by-column -- there are many options.

r - What does the rpart "Error in as.character(x) : cannot coerce type...

r rpart cart-analysis
Rectangle 27 0

I had the same issue with R 3.0.1 on ubuntu.

Installing the latest version of the RMySQL-package resolved the problem:

> install.pacakges("RMySQL")

Make sure to restart R after the installation.

Tried it when I posted this question and didn't work. Tried it again now and worked. Seems like they fixed the bug.

dbConnect with R 3.0 on Ubuntu 12.04 x64 --Error in as.integer(from) :...

r
Rectangle 27 0

Main problem in your code is that you give reactive function "a1" as argument to sqlQuery function. sqlQuery expects you to give character as argument.

In this case where you want to monitor any changes in input. You could use observe function. I also added error checking line to make sure that you actually have some value in a input$idnumb

However the solution is much simpler: you should simply call a1() instead of a1 since you have defined it as a reactive function.

sql1 = sqlQuery(acc_con1, paste0('select Title from dbo.Employee where EmployeeID=',a1()))
shinyServer(function(input, output) {
         library(RODBC)
         library(sqldf)
            observe({
            if(input$idnumb ==NULL)
               return(NULL)
            acc_con1 = odbcConnect("AdvWrk", uid="... ", pwd="... ")
            sql1 = sqlQuery(acc_con1, paste0('select Title from dbo.Employee where EmployeeID=',input$idnumb))
            )}
        output$emptitle = renderTable(print(sql1))
 )}

:( that gives me error, object t1 not found, infact I found a solution to this problem last night, I moved everything inside the reactive and it worked.

well, it is hard to test without mimimal working example. But moving everything inside reactive should work. Good that you get it working.

Can you try just adding the function call to a1: this is the way the reactive should be called, a1() instead of a1. See my answer

sql - r shiny error Error in as.vector(x, "character") : cannot coerce...

sql r parameter-passing shiny shiny-server
Rectangle 27 0

you need to get rid of the parens surrounding each of the key/value pairs as in:

library(hash)
titles = hash("NAME" = list("exact"=list('NAME'),"partial"=list()),
          "Dt" = list("exact"=list('Dt'),"partial"=list()),
          "CC" = list("exact"=list(),"partial"=list()))

When you include the parens hash( (a=b) ), the object (a=b) is being passed as an expression and not a key/value pair

python - cannot coerce type 'closure' to vector of type 'character' wh...

python r dictionary hash
Rectangle 27 0

In amongst your confusion about what versions you are running (there wasn't an R version 1.35!!), there are several issues. (To find out what versions of R and packages you are running, try sessionInfo().)

First, the error you are getting comes from your use of text(). It should be statusText().

Second, it seems like some of the functions/methods are not being exported in the package NAMESPACE. You can make it work by specifying the correct namespace when calling the function, as per the example below, but you should email the package maintainer (Jeff Gentry - contact details on CRAN). You can refer to unexported functions using the ::: operator. ::: takes the package/namespace name on the left-hand side, with the function name on the right hand side, e.g.:

twitteR:::statusSource(x)

Here is a full working version of your example:

library(plyr)
library(twitteR)
## simplify the call to see what is going on - function first
fooFun <- function(x) {
    data.frame(text = statusText(x), favorited=favorited(x),
               created=created(x), truncated=twitteR:::truncated(x),
               id=id(x), statusSource=twitteR:::statusSource(x),
               screenName=screenName(x))
}
## now ldply it
out <- ldply(searchTwitter("rstats", session = getCurlHandle(), n = 10), fooFun)
## show some of it:
head(out)

ah, yes. It was R.app GUI v1.35 and R version 2.12.

Hi there - I told Gavin this privately, but just so it is on a public forum, this was an oversight on my part. I've updated the package to export those methods. It might be a day or two before I can upload the package, but the new version will be 0.9.2. Sorry about that.

r - Question on Plyr error: Error in as.double(y) : cannot coerce type...

r plyr
Rectangle 27 0

I had this issue too and then realized that I forgot to load the library with library(arulesViz) after installing the package with: install.packages("arulesViz")

r - Error in as.double(y) : cannot coerce type 'S4' to vector of type ...

r
Rectangle 27 0

I was trying to pass the whole environment variable while I simply needed the text attribute. Solved it now.

r - Cannot coerce type 'environment' to vector of type 'character' - T...

r twitter environment