cannot coerce type 'closure' to vector of type 'character'
The call to datasetInput1 and datasetInput2 in the two last lines is the reason of the error.
You should instead call datasetInput1() and datasetInput2().
Otherwise R tries to convert the function to char. It should be:
#Debug print value of sting being passed
output$testvar = renderText(print(datasetInput1()))
# Plot
output$myplot = renderPlot({myplotfunct(iris, datasetInput1(), datasetInput2())})
The ()
allows you to get the value of the reactive element rather than interact with the reactive element itself. This is a pretty fundamental concept with Shiny and if that's not something you are yet familiar yet, perhaps revisit the shiny tutorial.
Just by adding the (), the error disappears as you can see below:
Error in as.character(query) : cannot coerce type 'closure' to vector of type 'character'
In this line, you assign the base function sprintf
to sql_statement
:
sql_statement <- sprintf
You then pass it to sqlQuery()
as the second argument (query
):
df<- sqlQuery(sql_connection,sql_statement,stringsAsFactors=0)
The function expects query
to be a character, so you get the error that R can't coerce a 'closure' (essentially another name for a function) to a character.
I assume you were trying to imitate this commented-out line:
#sql_statement <- sprintf(sql_string, BackDate, DidList[m])
But it's not necessary if all you want to do is run the query you just wrote. So if we just tidy up your code so sql_string
is passed straight to sqlQuery()
, it should work as intended, though I can't actually test it because we don't have access to your database:
require(RODBC)
require(lubridate)
require(stringr)
require(dplyr)
require(tidyr)
#server for N2 purge attribute
target <- "xxxx"
user <- "xxxx"
pass <- "xxxx"
#print("Working on lot history...")
sql_string <- paste0("select
xxxx
from
xxxx
where
xxxx
")
sql_connection <- odbcConnect(dsn = target, uid = user, pwd = pass,believeNRows=FALSE)
df<- sqlQuery(sql_connection,sql_string,stringsAsFactors=0)
Cannot coerce type 'closure' to vector of type list when creating a lag variable by id
Don't use quotes around the column name.
library(data.table)
TandCtable[, RDexp_pre1 := c(NA, RD_expenses[-.N]), by=id]
If you want lag of different offset length it is better to use shift
as suggested by @Waldi instead of appending NA
's.
TandCtable[, RDexp_pre1 := shift(RD_expenses, 2), by=id]
r shiny - cannot coerce type 'closure' to vector of type 'double'
Try this:
library(tidyverse)
library(shiny)
df2 <- tibble(years = c(2012,2013,2014,2015))
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
sliderInput("year_range",
"Select Years:",
min = 2012,
max = 2019,
value = c(2012, 2019),
ticks = FALSE)),
mainPanel(tableOutput('table'))
)
)
server <- function(input, output) {
output$table <- renderTable({
filter(df2, years >= input$year_range[1], years <= input$year_range[2])
})
}
shinyApp(ui = ui, server = server)
Don't understand Cannot Coerce type 'closure' Error
TidySymptoms
data has no id
column in it. Assuming it's a mistake and you have that already in your data you can do the following changes in the function.
- There is no need to pass
df.new
to the function. - The column in
TidySymptoms
is called asword
but you are usingtext
in the function.
Try this code.
minus_TextNum <- function(df){
df.new <- mutate(df, text = gsub(x = word, pattern = "[0-9]+|\\(.*\\)", replacement = "")) %>%
unnest_tokens(input = text, output = word) %>%
filter(!word %in% c(stop_words$word, "patient")) %>%
group_by(id) %>%
summarise(text = paste(word, collapse = " "))
return(new.df)
}
minus_TextNum(TidySymptoms)
Related Topics
Annotating Facet Title as Strip Over Facet
How to Plot One Variable in Ggplot
Determining the Distance Between Two Zip Codes (Alternatives to Mapdist)
Why Does Rendering a PDF from Rmarkdown Require Closing Rstudio Between Renders
Ggplot2: Geom_Text() with Facet_Grid()
How to Convert Date and Time from Character to Datetime Type
Wrap Long Text in Kable Table Column
Remove Parenthesis from a Character String
How to Manually Create a Dendrogram (Or "Hclust") Object? (In R)
How to Use a MACro Variable in R? (Similar to %Let in Sas)
Plot Circle with a Certain Radius Around Point on a Map in Ggplot2
The Result of Rpart Is Just with 1 Root
Row-By-Row Operations and Updates in Data.Table
R Shiny Error: Cannot Coerce Type 'Closure' to Vector of Type 'Double'