Posts

Host shiny server in Ubuntu 14.04 LTS

Image
I have installed shiny server on Ubuntu 14.04 LTS running under a VM. This post is a collection of commands that I had used for setup. Note that if you want to use shiny server on LAN then use Bridged Network Adapter. On VM you can access the shiny server via localhost:3838 and on remote machines, you can use IP-address:3838 to open it. You can host your shiny app in the /srv/shiny-server/ directory. You can go to /etc/shiny-server/shiny-server.conf to change default configurations like you can change port with listen <port number>; in server {} module. $ sudo sh -c 'echo "deb http://cran.rstudio.com/bin/linux/ubuntu trusty/" >> /etc/apt/sources.list' $ sudo gpg --keyserver keyserver.ubuntu.com --recv-key E084DAB9 $ sudo gpg -a --export E084DAB9 | sudo apt-key add - $ sudo apt-get update $ sudo apt-get -y install r-base $ sudo su - -c "R -e \"install.packages(c('shiny','shinyjs','shinydashboard','...

Generate word cloud in R

Image
TEXT (.txt) This working script was tested on R 3.2.5 . Code is adapted from https://github.com/gimoya/theBioBucket-Archives/blob/master/R/txtmining_pdf.R . It reads a text file, processes it to remove unnecessary words and plots it. Code: library(tm) library(wordcloud) library(Rstem) filetxt <- "C:\\Users\\310211146\\Documents\\Other\\May_Report.txt" txt <- readLines(filetxt) txt <- tolower(txt) txt <- removeWords(txt, c("\\f", stopwords())) corpus <- Corpus(VectorSource(txt)) corpus <- tm_map(corpus, removePunctuation) tdm <- TermDocumentMatrix(corpus) m <- as.matrix(tdm) d <- data.frame(freq = sort(rowSums(m), decreasing = TRUE)) d$stem <- wordStem(row.names(d), language = "english") d$word <- row.names(d) d <- d[nchar(row.names(d)) < 20,] agg_freq <- aggregate(freq ~ stem, data = d, sum) agg_word <- aggregate(word ~ stem, data = d, function(x)   x[1]) d <- cbind(freq...

Learn R with 5 Video Tutorials

Image
If you are new to R or wants to enhance your basic skills in R and wondering where to get started provided there is lots of free material available online then you have come to right place. I have presented 5 video tutorials that is more than enough to get you started with R and will push you in direction of excelling R. 1. What is R and How to install it? 2. What are data types and variables in R?

R: Count occurrences in vector

Image
If you are having a vector and you want to count frequency of individual member (in other words, occurrences of each element) then you can do as following: use hist() function: x <- c(1,3,2,5,4,4,2,2,4,2,4,2,4,6,4,5,2)   x [1] 1 3 2 5 4 4 2 2 4 2 4 2 4 6 4 5 2   hist(x) or use summary() function: x <- factor(x)   x [1] 1 3 2 5 4 4 2 2 4 2 4 2 4 6 4 5 2 Levels: 1 2 3 4 5 6   summary(x) 1 2 3 4 5 6 1 6 1 6 2 1   convert vector into factor and then call summary function. OR if you are working with characters then hist() won't be useful as it expects numeric elements so use summary() as shown below:   x <- c("devharsh","trivedi","R","C#","ASP.NET","R","C#","devharsh","R")   x <- factor(x)   summary(x) ASP.NET C# devharsh R trivedi 1 2 2 3 1

5.5.1 Authentication Error in Gmail

Image
Code for sending email using gmail in C# : using System.Net.Mail; using System.Net; MailMessage mail = new MailMessage(); mail.To.Add("dummy@test.com"); mail.From = new MailAddress("dummy-sender@gmail.com"); mail.Subject = "subject"; mail.SubjectEncoding = System.Text.Encoding.UTF8; mail.Body = "foo foo bar"; mail.Priority = MailPriority.High; SmtpClient client = new SmtpClient(); client.UseDefaultCredentials = false; client.Credentials = new NetworkCredential("dummy-sender@gmail.com", "gmail-password"); client.Port = 587; client.Host = "smtp.gmail.com"; client.EnableSsl = true; client.Send(mail); Error: "The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required" Solution: Login to your gmail account from which you are trying to send email and then go to https://www.google.com/settings/security/lesss...

Fix _CRT_SECURE_NO_WARNINGS in VC++

Image
Error: To disable deprecation, use _CRT_SECURE_NO_WARNINGS. Error    1    error C4996: 'sscanf': This function or variable may be unsafe. Consider using sscanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. Error    1    error C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. Solution: Go to Project -> Properties -> C/C++ -> Preprocessor -> Preprocessor Definitions and add _CRT_SECURE_NO_WARNINGS in the list. This Visual C++ warning, C4996, flags standard C functions such as sscanf and fopen as unsafe because they do not bound their buffers, and MSVC suggests the _s secure variants instead. You can switch to the suggested secure functions, or define _CRT_SECURE_NO_WARNINGS to silence the warning once you have rev...

cannot coerce type 'externalptr' to vector of type 'character'

Image
Error: Error in as.vector(x, "character") :   cannot coerce type 'externalptr' to vector of type 'character' Problem Code: con <- dbConnect(RMySQL::MySQL(), dbname = "test") sql <-     paste0(       "INSERT INTO `devharsh` (`un`, `pw`, `bl`) VALUES ('", TextUN ,"', '", TextPW ,"', '",0,"')"     )   dbGetQuery(con, sql) Solution: sql <-     paste0(       "INSERT INTO `devharsh` (`un`, `pw`, `bl`) VALUES ('", TextUN$getText() ,"', '", TextPW$getText() ,"', '",0,"')"     ) Note: It was giving error because it was not getting text from textbox that we should get by getText() function. This R error means a value that is not a plain character vector was passed where a string was expected. Here a database connection handle, an external pointer, was used where text was needed. The fix is to pass th...