skip to main content
Power BI
    • What is Power BI
    • Why Power BI
    • Customer stories
    • Data visuals
    • Security
    • Power BI Desktop
    • Power BI Pro
    • Power BI Premium
    • Power BI Mobile
    • Power BI Embedded
    • Power BI Report Server
  • Pricing
    • Azure + Power BI
    • Office 365 + Power BI
      • Energy
      • Healthcare
      • Manufacturing
      • Media
      • Retail
    • For analysts
    • For IT
      • Overview
      • Embedded analytics
      • Power BI visuals
      • Automation
      • Documentation
      • Community
    • Overview
    • Find consulting services
    • Partner showcase
    • Find a partner
    • Become a partner
    • Instructor-led training
    • Getting started
      • Overview
      • Online workshops
      • Self-guided learning
      • Webinars
      • Documentation
      • Roadmap
      • Overview
      • Issues
      • Give feedback
    • Blog
    • Business intelligence topics
    • Overview
    • Forums
    • Galleries
    • Submit ideas
    • Events
    • User groups
    • Community blog
    • Register
    • ·
    • Sign in
    • ·
    • Help
    Go To
    • Galleries
    • Community Connections & How-To Videos
    • COVID-19 Data Stories Gallery
    • Themes Gallery
    • Data Stories Gallery
    • R Script Showcase
    • Webinars and Video Gallery
    • Quick Measures Gallery
    • 2021 MSBizAppsSummit Gallery
    • 2020 MSBizAppsSummit Gallery
    • 2019 MSBizAppsSummit Gallery
    cancel
    Turn on suggestions
    Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.
    Showing results for 
    Search instead for 
    Did you mean: 
    • Microsoft Power BI Community
    • Galleries
    • R Script Showcase
    • Wordcloud with r
    Accepted Solution

    Wordcloud with r

    08-19-2016 01:15 AM - last edited 12-13-2017 00:06 AM

    wbob
    Advocate I
    51124 Views
    LinkedIn LinkedIn Facebook Facebook Twitter Twitter
    wbob
    wbob Advocate I
    Advocate I
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Email to a Friend
    • Report Inappropriate Content

    Wordcloud with r

    ‎08-19-2016 01:15 AM

    Although there is a custom visualisation for Wordcloud now, I did this one with r in Power BI Desktop as you get a greater degree of control over it:

     

     

    library(tm)
    library(wordcloud)
    
    words <- Corpus(VectorSource(dataset))
    words <- tm_map(words, stripWhitespace)
    words <- tm_map(words, content_transformer(tolower))
    words <- tm_map(words, removeNumbers)
    words <- tm_map(words, removePunctuation)
    words <- tm_map(words, removeWords, stopwords("english"))
    words <- tm_map(words, stemDocument)
    wordcloud(words, scale=c(5,0.5), max.words=50, random.order=FALSE, rot.per=0.35, use.r.layout=FALSE, colors=brewer.pal(8, "Dark2"))

     

    I have attached a sample .pbix file with the text from Wikipedia's 'big data' article and the R visualisation.

    Preview file
    64 KB
    wordcloud r.pbix
    wordcloudR_1.pbix
    Labels:
    • Labels:
    • Advanced Analytics Visualizations
    Message 1 of 14
    51,124 Views
    9
    Reply
    1 ACCEPTED SOLUTION
    Greg_Deckler
    Super User Greg_Deckler
    Super User
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Email to a Friend
    • Report Inappropriate Content

    ‎12-05-2017 06:00 AM

    When I tried the original code, it worked with older versions of R, 3.2.3 but not newer versions of R, 3.3.1 and 3.4.2. The Corpus construction was returning only numbers which were then stripped out by the rest of the code causing problems. So, I hacked together a variation of the original code based upon comments and have a working version here:

     

    require(tm)
    require(wordcloud)
    require(RColorBrewer)
    
    datain = as.data.frame(table(as.character(dataset[,1])))
    words <- Corpus(VectorSource(dataset$text))
    
    words <- tm_map(words, stripWhitespace)
    words <- tm_map(words, content_transformer(tolower))
    words <- tm_map(words, removeNumbers)
    words <- tm_map(words, removePunctuation)
    words <- tm_map(words, removeWords, stopwords("english"))
    words <- tm_map(words, stemDocument)
    
    wordcloud(words, scale=c(5,0.75), max.words=50, random.order=FALSE, rot.per=0.35, use.r.layout=FALSE, colors=brewer.pal(8, "Dark2"))

    The main change is the construction of the Corpus, VectorSource(dataset) becomes VectorSource(dataset$text)

     

    This is based upon the original PBIX file included in the original post.


    @ me in replies or I'll lose your thread!!!
    Become an expert!: Enterprise DNA
    External Tools: MSHGQM
    YouTube Channel!: Microsoft Hates Greg
    Latest book!:
    Learn Power BI 2nd Edition
    Message 13 of 14
    48,179 Views
    2
    Reply
    • All forum topics
    • Previous Topic
    • Next Topic
    Greg_Deckler
    Super User Greg_Deckler
    Super User
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Email to a Friend
    • Report Inappropriate Content

    ‎12-05-2017 06:00 AM

    When I tried the original code, it worked with older versions of R, 3.2.3 but not newer versions of R, 3.3.1 and 3.4.2. The Corpus construction was returning only numbers which were then stripped out by the rest of the code causing problems. So, I hacked together a variation of the original code based upon comments and have a working version here:

     

    require(tm)
    require(wordcloud)
    require(RColorBrewer)
    
    datain = as.data.frame(table(as.character(dataset[,1])))
    words <- Corpus(VectorSource(dataset$text))
    
    words <- tm_map(words, stripWhitespace)
    words <- tm_map(words, content_transformer(tolower))
    words <- tm_map(words, removeNumbers)
    words <- tm_map(words, removePunctuation)
    words <- tm_map(words, removeWords, stopwords("english"))
    words <- tm_map(words, stemDocument)
    
    wordcloud(words, scale=c(5,0.75), max.words=50, random.order=FALSE, rot.per=0.35, use.r.layout=FALSE, colors=brewer.pal(8, "Dark2"))

    The main change is the construction of the Corpus, VectorSource(dataset) becomes VectorSource(dataset$text)

     

    This is based upon the original PBIX file included in the original post.


    @ me in replies or I'll lose your thread!!!
    Become an expert!: Enterprise DNA
    External Tools: MSHGQM
    YouTube Channel!: Microsoft Hates Greg
    Latest book!:
    Learn Power BI 2nd Edition
    Message 13 of 14
    48,180 Views
    2
    Reply
    boefraty
    Microsoft boefraty
    Microsoft
    In response to Greg_Deckler
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Email to a Friend
    • Report Inappropriate Content

    ‎12-13-2017 12:00 AM

    Thanks a lot, we will review the PBIX. Excpecially because the R-engine is going to be upgraded in service soon

    Message 14 of 14
    20,908 Views
    0
    Reply
    ToveHjelm
    ToveHjelm
    Regular Visitor
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Email to a Friend
    • Report Inappropriate Content

    ‎11-13-2017 02:40 AM

    I love the idea with a word cloud that you have more control over. However, when I open the pbix-file to try the visual I get the message "Can't display visuals", with the following details:

    Error Message:

    R script error.

    Loading required package: NLP

    Loading required package: methods

    Loading required package: RColorBrewer

    Error in strwidth(words[i], cex = size[i], ...) : invalid 'cex' value

    Calls: wordcloud -> strwidth

    In addition: Warning messages:

    1: In max(freq) : no non-missing arguments to max; returning -Inf

    2: In max(freq) : no non-missing arguments to max; returning -Inf

    Execution halted

     

    I have the packages installed. Any ideas?

    Message 10 of 14
    46,740 Views
    0
    Reply
    Datamijn
    Datamijn Helper I
    Helper I
    In response to ToveHjelm
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Email to a Friend
    • Report Inappropriate Content

    ‎11-15-2017 02:22 AM

    I got the same error when working on my own word cloud. I solved it by adding the 

    'require("package")' for all the packages used. In my case it worked after adding the require call, but the end of the script is slightly different than the example here.

     

    require("tm")
    require("SnowballC")
    require("wordcloud")
    require("RColorBrewer")

     

    library(tm)
    library(SnowballC)
    library(wordcloud)
    library(RColorBrewer)

     

    #Data
    docs <- Corpus(VectorSource(dataset$Column1))

    # Convert the text to lower case
    docs <- tm_map(docs, content_transformer(tolower))
    # Remove numbers
    docs <- tm_map(docs, removeNumbers)
    # Remove english common stopwords
    #docs <- tm_map(docs, removeWords, stopwords("dutch"))
    # Remove your own stop word
    # specify your stopwords as a character vector
    docs <- tm_map(docs, removeWords, c("het", "met", "ons", "dit", "hem", "als", "dat", "heb"))
    # Remove punctuations
    docs <- tm_map(docs, removePunctuation)
    # Eliminate extra white spaces
    #docs <- tm_map(docs, stripWhitespace)
    # Text stemming
    #docs <- tm_map(docs, stemDocument)

    #Build a term-document matrix
    dtm <- TermDocumentMatrix(docs)
    m <- as.matrix(dtm)
    v <- sort(rowSums(m),decreasing=TRUE)
    d <- data.frame(word = names(v),freq=v)
    head(d, 10)

    set.seed(1234)
    wordcloud(words = d$word, freq = d$freq, min.freq = 1,
    max.words=200, random.order=FALSE, rot.per=0.35,
    colors=brewer.pal(8, "Dark2"))

     

     

    Message 11 of 14
    20,940 Views
    0
    Reply
    shadho
    shadho
    Regular Visitor
    In response to Datamijn
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Email to a Friend
    • Report Inappropriate Content

    ‎11-13-2019 03:54 PM

    Hi, 

    I pretty much did the same exact steps to generate a word cloud visual in R , however i'm still getting  the same error.

    The script runs fine on my machine.

     

    Here is the code snippet

     

    # Paste or type your script code here:
    require("tm")
    require("SnowballC")
    require("wordcloud")
    require("wordcloud2")
    require("RColorBrewer")

    library(tm)
    library(SnowballC)
    library(wordcloud)
    library(RColorBrewer)
    library(wordcloud2)

    #Data
    docs <- Corpus(VectorSource(dataset$UserFeedback))

    ##Data Cleaning and Wrangling
    docs <- tm_map(docs, PlainTextDocument)
    docs <- tm_map(docs,tolower)
    docs <- tm_map(docs,removeNumbers)
    docs <- tm_map(docs,removeWords,c(stopwords("english"),"expert","just","asdfasdf","asdf","session","slide","also","slides","asked","test","deck","around","took","look","might","make","many","give","keep","adding","going","go","making","really","job","ones","even","see","made","bit"))
    docs <- tm_map(docs,removePunctuation)
    docs <- tm_map(docs,stripWhitespace)
    docs <- tm_map(docs,stemDocument)

    tdm<-TermDocumentMatrix(docs)
    m <-as.matrix(tdm)
    v <-sort(rowSums(m),decreasing = TRUE)
    Docs2 <-data.frame(word=names(v),freq =v)
    head(Docs2, 10)
    wordcloud2(Docs2, color = "random-dark",size = 0.5, shape = "circle", backgroundColor = "black")
     
    Any thoughts what  could be the issue? 
    Message 12 of 14
    18,748 Views
    0
    Reply
    patoduck
    patoduck Helper II
    Helper II
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Email to a Friend
    • Report Inappropriate Content

    ‎11-30-2016 03:14 PM

    Hi,

     

    Nice post 🙂

    Do you know how to make a word cloud out of a columns (the entire content) for example i have a colum with company names and number of cases?

     

    The problem is that by altering the Dataset I cannot use the code again in Power BI ( I had to write colClasses = c("character", "numeric"),  to tell R that my dataset has 2 type of columns.

     

    Thanks!

     

    > `dataset` = read.csv('C:/Users/PatricioLobos/AppData/Local/Radio/REditorWrapper_8fe60428-ecc0-478f-ab77-56803f9407f4/input_df_227d85ab-962e-4701-b1ac-ff6e53817c56.csv', colClasses = c("character", "numeric"), check.names = FALSE, encoding = "UTF-8", blank.lines.skip = FALSE);
    + # Original Script. Please update your script content here and once completed copy below section back to the original editing window #
    >require(wordcloud)
    > require(RColorBrewer)
    > pal2 <- brewer.pal(8, 'Dark2')
    > plot(wordcloud(dataset$Kundenavn, dataset$`Nr. Cases`, scale = c(8, .4), min.freq = 5, max.words = Inf, random.order = FALSE, rot.per = .15, colors = pal2))

    Message 8 of 14
    49,322 Views
    0
    Reply
    boefraty
    Microsoft boefraty
    Microsoft
    In response to patoduck
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Email to a Friend
    • Report Inappropriate Content

    ‎12-05-2016 02:10 AM
    require(wordcloud)
    require(RColorBrewer)
    
    datain = as.data.frame(table(as.character(dataset[,2])))
    
    pal2 <- brewer.pal(4,"Dark2")
    
    wordcloud(datain[,1],datain$Freq, 
              scale=c(4,.3),
              min.freq=1, max.words=Inf, 
              random.order=FALSE, 
              rot.per=.15, 
              colors=pal2)

     The dataset is 2 columns: "ID", "Term" . If you have "Frequency" instead of "ID" no need to call "table" operator  

     

     

    wordcloud_R_terms_n_words.pbix
    Message 9 of 14
    49,282 Views
    0
    Reply
    kavasimihaly
    kavasimihaly
    Frequent Visitor
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Email to a Friend
    • Report Inappropriate Content

    ‎09-26-2016 02:06 AM

    Hi, Thanks for sharing.

     

    I have a problem with special charaters braking the visualisation.

     

    Can you help me what to do with it.

     

    The R code is working when no special charaters present.

     

    This is the Error Message i get:

    Error Message:

    R script error.
    Error in type.convert(data[[i]], as.is = as.is[i], dec = dec, numerals = numerals, :
    invalid input '@MSEmpresasLatam @DiegoBek exactamente! Es la era de la inteligencia #AzureML #SQLEnterprise #PowerBI #IoT 😍' in 'utf8towcs'
    Calls: read.csv -> read.table -> type.convert
    Execution halted

     

     

    This is the R code i have:

    library(tm)
    library(SnowballC)
    library(wordcloud)
    dataset <-Corpus(VectorSource(dataset))
    dataset <- tm_map(dataset, removePunctuation)
    dataset <-tm_map(dataset, removeWords, c('powerbi','PowerBI',stopwords('english')))
    dataset <-tm_map(dataset, stemDocument)
    wordcloud(dataset, max.words = 100, random.order = FALSE)

     

    Thanks in advance if anyone could help.

     

    Best Regards,

    Kávási Mihály

    Message 5 of 14
    49,976 Views
    0
    Reply
    mike_honey
    mike_honey Memorable Member
    Memorable Member
    In response to kavasimihaly
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Email to a Friend
    • Report Inappropriate Content

    ‎09-26-2016 03:28 AM

    Hi Kávási,

     

    It looks you are using the R Word Cloud, not the Power BI Custom Visual?  If that is the case then I suggest you re-post this e.g. to stackoverflow.com, tagged for R.

     

    Message 6 of 14
    49,970 Views
    0
    Reply
    kavasimihaly
    kavasimihaly
    Frequent Visitor
    In response to mike_honey
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Email to a Friend
    • Report Inappropriate Content

    ‎09-26-2016 05:13 AM

    Hi,

     

    the reason why i posted here because this is the R script showcase forum and this error is related to the R integration of PowerBI.

     

    Best regards, 

    Kávási Mihály ( in English it is Mihály Kávási correctly 🙂 will change from now)

    Message 7 of 14
    49,963 Views
    0
    Reply
    mike_honey
    mike_honey Memorable Member
    Memorable Member
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Email to a Friend
    • Report Inappropriate Content

    ‎09-21-2016 07:07 PM

    @wbob - this seems an incomplete sample and perhaps why it is generating confusion.  I dont think that code alone can be used in PBI Desktop without a source datatset.

     

    Can you please post your PBIX file?

    Message 4 of 14
    50,034 Views
    0
    Reply
    erikskov
    erikskov Advocate II
    Advocate II
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Email to a Friend
    • Report Inappropriate Content

    ‎08-23-2016 03:11 PM

    Getting an error when trying to render in PowerBI.com.  Works fine in PowerBI Desktop...

    ERROR:

    [Monitor] Loaded provider: [WindowsLibOsProvider] [Monitor] Loaded provider: [SocketStreamProvider] [Monitor] Loaded provider: [DnsStreamProvider] [Monitor] Loaded provider: [HttpServerStreamProvider] [Monitor] Loaded provider: [ClockStreamProvider] [Monitor] Loaded provider: [ResourceManager] [Monitor] Loaded Security Monitor Profile: [C:\Sandboxing\Sandboxes\5a4800e1-dd61-41e3-9bce-3fb922b5f659\Profile\0.dbconfig] [Monitor] Writable folder: [file:///Users] => [C:\Sandboxing\Sandboxes\5a4800e1-dd61-41e3-9bce-3fb922b5f659\Users] [Monitor] Writable folder: [file:///Results] => [C:\Sandboxing\Sandboxes\5a4800e1-dd61-41e3-9bce-3fb922b5f659\Results] [Monitor] Writable folder: [file:///Work] => [C:\Sandboxing\Sandboxes\5a4800e1-dd61-41e3-9bce-3fb922b5f659\Work] [Monitor] R/O folder: [file:///ThirdParty] => [C:\WFRoot\SBRole.0\Fabric\work\Applications\ASAzureApp_App0\temp\R_Root_13.0.1605.329] [Monitor] R/O folder: [file:///Script] => [C:\Sandboxing\Sandboxes\5a4800e1-dd61-41e3-9bce-3fb922b5f659\Script] [Monitor] R/O folder: [file:///InputData] => [C:\Sandboxing\Sandboxes\5a4800e1-dd61-41e3-9bce-3fb922b5f659\InputData] [Monitor] Scratch folder: [C:\Sandboxing\Sandboxes\5a4800e1-dd61-41e3-9bce-3fb922b5f659\Scratch] [Monitor:WARNING] Cannot find dependency: "Windows.0.0.0.0.0" x64 [Monitor:WARNING] Unable to resolve package dependencies: 0x80070490 [Monitor:ERROR] Unable to create application sandbox environment; the most likely cause of this error is a missing package dependency. Please confirm that all application dependencies are present. (Status=0x80070490) [Monitor:ERROR] Failed to launch application: 0x80070490 [Monitor:WARNING] Failed to retrieve application's exit code! [Monitor] Done.
    Please try again later or contact support. If you contact support, please provide these details.

    Message 2 of 14
    50,722 Views
    0
    Reply
    boefraty
    Microsoft boefraty
    Microsoft
    In response to erikskov
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Email to a Friend
    • Report Inappropriate Content

    ‎09-04-2016 05:24 AM

    Dear erikskov,

    Thank you very much for the input and sorry for late response.

    Can I please get PBIX with data and R code, which is not working for you in server? It worked for me on toy data sample.

     

    Best Regards,

    B. (boefraty@microsoft.com)

     

     

     

     

    Message 3 of 14
    50,308 Views
    0
    Reply

    Power Platform

    • Overview
    • Power BI
    • Power Apps
    • Power Automate
    • Power Virtual Agents

    • Sign in
    • Sign up

    Browse

    • Solutions
    • Partners
    • Consulting Services

    Downloads

    • Power BI Desktop
    • Power BI Mobile
    • Power BI Report Server
    • See all downloads

    Learn

    • Guided learning
    • Documentation
    • Support
    • Community
    • Give feedback
    • Webinars
    • Developers
    • Blog
    • Newsletter

    © 2022 Microsoft

    Follow Power BI

    • Privacy & cookies
    • Manage cookies
    • Terms of use
    • Trademarks