Wordcloud Package: Get "Error in Strwidth(…):Invalid 'Cex' Value"

Power BI WordCloud with R value 'cex' incorrect

My question was wrongly asked but the answer to my problem was :

var<- paste( dataset$Day , dataset$Month, dataset$Year , sep='-' )

power Bi takes the date, and split it. So I got to put it together .

R Shiny - Incorrect cex value - uploading text file, wordcloud package

There are a few changes/edits to be done to get your app working! The way you are handling the file input is completely wrong :). You can directly put input$selection in the getTermMatrix() function and then read the file contents in global.R. Have a look at this to understand how to upload a file and read its contents in Shiny.

The error was because there was no file read and as a result, there was no data to be fed into Corpus() function. In the following code, as there was no file input when you start the app, an error was displaying that no file is read. But, after uploading a file, the error disappears and the corpus is shown. To not display the error, I included a small tags() in ui.R. May be you can find a better get around for that.

Have a look at the following working code and try to extend it to your future purposes.

ui.R

shinyUI(
fluidPage(
# Application title
titlePanel("Word Cloud"),
tags$style(type="text/css",
".shiny-output-error { visibility: hidden; }",
".shiny-output-error:before { visibility: hidden; }"
),

sidebarLayout(
# Sidebar with a slider and selection inputs
sidebarPanel(
#######
fileInput("selection", "Choose a text:"),

actionButton("update", "Change"),
hr(),
sliderInput("freq",
"Minimum Frequency:",
min = 1, max = 50, value = 15),
sliderInput("max",
"Maximum Number of Words:",
min = 1, max = 300, value = 100)
),

# Show Word Cloud
mainPanel(
plotOutput("plot")
)
)
)
)

server.R

library(shiny)

shinyServer(function(input, output, session) {
# Define a reactive expression for the document term matrix

terms <- reactive({
# Change when the "update" button is pressed...

input$update

# ...but not for anything else
isolate({
withProgress({
setProgress(message = "Processing corpus...")
getTermMatrix(input$selection)
})
})
})

# Make the wordcloud drawing predictable during a session
wordcloud_rep <- repeatable(wordcloud)

output$plot <- renderPlot({
v <- terms()
wordcloud_rep(names(v), v, scale=c(4,0.5),
min.freq = input$freq, max.words=input$max,
colors=brewer.pal(8, "Dark2"))
})
})

global.R

library(tm)
library(wordcloud)
library(memoise)

# Using "memoise" to automatically cache the results
getTermMatrix <- function(f) {
# Careful not to let just any name slip in here; a
# malicious user could manipulate this value.

text <- readLines(f$datapath,encoding = "UTF-8")

myCorpus = Corpus(VectorSource(text))

myCorpus = tm_map(myCorpus, content_transformer(tolower))
myCorpus = tm_map(myCorpus, removePunctuation)
myCorpus = tm_map(myCorpus, removeNumbers)
myCorpus = tm_map(myCorpus, removeWords,
c(stopwords("SMART"), "thy", "thou", "thee", "the", "and", "but"))

myDTM = TermDocumentMatrix(myCorpus,
control = list(minWordLength = 1,wordLengths=c(0,Inf)))

m = as.matrix(myDTM)

sort(rowSums(m), decreasing = TRUE)
}


Related Topics



Leave a reply



Submit