Since your script looks like it would return a single value, ie the similarity score, this should be possible with Tableau's R integration.
Tableau doesn't have any limitations on the complexity of the R script you run, it only requires that you return either a constant result, or one result for each row of data passed. In this case, you would be returning a similarity score for each title so it would work well.
I would imagine your script would look something like this:
SCRIPT_REAL("<your R code>", ATTR([Book Title]), [String Parameter])
Where your R code takes in a vector of book titles as well as the string constant from a parameter and then your algorithm assigns a score to each book and returns them as a vector. You could then use a rank table calculation to filter the results.
My video from TC18, while mostly focusing on Python has R examples as well, and should be a good guide to how to write your script for Tableau and how to integrate parameters: Data science applications with TabPy/R - YouTube
Happy to chat further as well.
Thank you! I attempted to replicate and I was unsuccessful. It's probably because I'm not there yet to fully understand the integration. I am getting this error when I run the script:
Error in base::parse(text = .cmd) : <text>:83:1: unexpected symbol 82: 83: Return ^
It appears tableau/r is not recognizing the return function near the end of my script.
I created a calculated field named Score and entered the script below. My expectation is the score would be generated once I add it as a row in Tableau together with Unique Id, etc... and would change values when I enter a text in the string parameter field I created.
Lastly, for the parameter, I followed the convention through this link: Is it possible to use parameters within R scripts in calculated fileds? It says that the argument needs to have a  after the .arg
For some reason, there's something I'm not doing right causing the error:
# Description and Issue Titles are characters
LongText <- as.character(.arg1 )
#combine title and long text
Combined.Text <- (paste(IssueTitle, LongText, sep = ' '))
#ensures all text characters are readable
Combined.Text <- sapply(Combined.Text, function(x) iconv(enc2utf8(x), sub = 'byte'))
data <- data.frame(Combined.Text) %>%
docList <- as.list(data$Combined.Text)
N.docs <- length(docList)
QrySearch <- function(queryTerm)
# store docs in Corpus class which is a fundamental data structure in text mining
my.docs <- VectorSource(c(docList, queryTerm))
# Transform/standaridze docs to get ready for analysis
my.corpus <- VCorpus(my.docs) %>%
# Store docs into a term document matrix where rows=terms and cols=docs
# Normalize term counts by applying TDiDF weightings
term.doc.matrix.stm <- TermDocumentMatrix(my.corpus,
# Transform term document matrix into a dataframe
term.doc.matrix <- tidy(term.doc.matrix.stm) %>%
docMatrix <- term.doc.matrix %>%
qryMatrix <- term.doc.matrix %>%
# Calcualte top results by cosine similarity
searchRes <- docMatrix %>%
ATTR([Long Text]), ATTR([Issue Title]), [String Parameter])
For R, you don't need to include Return(), that's only needed for Python. I think the error is throwing becaust Return() isn't recognized in R.
If you end your scrip with searchRes it should work.
You will also need to have [Long Text] and [Issue Title] as dimensions in your view and set the table calculation to address those dimensions as in the video, so that it passes each field as one long vector.
I tried it but it still didn't work. I feel I'm doing something wrong but couldn't pinpoint the error. Another thing I did was rather than do a read.csv in tableau e.g.,
books <- read.csv("")
I made modifications in the first few lines of the script (refer to my prior post) as my assumption is that the data is already directly getting called from tableau and the above codes were now unnecessary. I replaced with what I have posted prior.
I could send my .twb file (replaced with sample data) if that would work. However, I don't see an option to attach the file.
Unfortunately, I'm still using Tableau 10.2
I did some tweaking with the formula and now this is what I'm getting:
The result returned by the SCRIPT function is of an unexpected type.
I found out how to attach the workbook to the original thread.
Thanks in advance,
Sample Tableau.twbx 6.4 KB
Thanks, I'll take a look at this ASAP.