Posts

Showing posts with the label RStudio

Perf_Plotter: Benchmark and Visualize Application Performance with C# and R

Image
Perf_Plotter Log with C#, plot with R Shiny Your App → C# logger → CSV → R Shiny → Plots Perf_Plotter is an open-source tool for benchmarking and visualizing the runtime performance of your application. It has two parts that work together: a logging component written in C# and a plotting utility written in R. How it works The workflow is simple: a Windows Forms application (C#) launches your program and records its performance metrics into a CSV file. You then upload that CSV into an R Shiny web app, which reads the data and plots it so you can see how your application behaved over the run. What is in the project The repository (and the Perf_Plotter.zip bundle) contains two folders and one file: perfloggengui - the C# solution that generates the logs from your application. Uploads - sample log files so you can try the plotter immediately. app.R - the Shiny app you run in R to read a CSV and plot it. Using it Open the perfloggengui C# solution,...

Count number of items and sum of the items in a column in R

Image
Code: mydf <- data.frame(Key=c('a','b','c','d','a','a','a','a','b','c','d','c','c','d','b','b','a'), Values=c(4,6,3,4,5,6,2,1,3,4,5,6,4,2,4,5,6))                 mydf_keys <- unique(mydf$Key) mydf_vals <- c(nrow(mydf_keys)) for (i in mydf_keys) {     mydf_vals <- c(mydf_vals, nrow(mydf[mydf$Key %in% i, ])) } #mydf_count gives count of items i.e. how many times they appear mydf_count <- data.frame(item=mydf_keys, count=mydf_vals) print(mydf_count) #mydf_sum gives sum of values of items in the column you specify mydf_sum <- setNames(aggregate(mydf$Values, by = list(item=mydf$Key), FUN = sum, na.rm = TRUE), c('item', 'sum')) print(mydf_sum) Output: If you have a better approach for this then please share in the comments below or email me. :) This R example groups rows by a key column and comput...

Run Microsoft R Open Script From Command Prompt

Image
Microsoft R Open is Multi-core environment developed on Core R. Microsoft R Open (MRO), formerly known as Revolution R Open (RRO), is the enhanced distribution of R from Microsoft Corporation. It is a complete open source platform for statistical analysis and data science. Microsoft R Open 3.4.1 is coming September 11th. MRO comes with Microsoft R Open GUI and you can also use it in RStudio IDE. But in case you want to run it from command prompt, it is pretty easy. First you need to set Path in environment variable. For 32-bit version path would be like: C:\Program Files\Microsoft\R Open\R-3.4.0\bin For 64-bit version path would be like: C:\Program Files\Microsoft\R Open\R-3.4.0\bin\x64 Now save it and restart command prompt if already open or open a new command prompt window. Type command "where rscript" and check whether it returns a correct path. Now you can type command "rscript your_script.r" and it will get executed. No...

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

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...

R: 'file' must be a character string or connection

Image
I was using the Shiny app in R , and I was trying to read a file using fileInput control, but I faced the following error:   Listening on http://127.0.0.1:3558 Error in read.table(file = file, header = header, sep = sep, quote = quote,  :   'file' must be a character string or connection Problem code: if(is.null(input$file1))       return(NULL)  file <- read.csv(file=input$file1,head=TRUE,sep=",", stringsAsFactors=FALSE) csv <- str(file) Solution: inFile <- input$file1 if (is.null(inFile))       return(NULL)     csv <- read.csv(inFile $datapath , header=TRUE, sep=",") The resolution was to use the datapath property of the input file and not the file itself.

Introduction to R (Download PDF)

Image
Download Link: https://drive.google.com/file/d/0B_HnjRcH9aTpNG10ME1raW0zdTA/view?usp=sharing (Click this link to download pdf) This PDF is a part of Introduction to R workshop conducted by Chris Bruno , Dason Kurkiewicz and Adam Loy from Department of Statistics, IOWA State University on 15 July 2010. Reference: http://streaming.stat.iastate.edu/workshops/r-intro/ Breakdown of chapters is as following: 1. Introduction 2. Data Management 3. Graphics 4. Models 5. Programming 6. Advanced Manipulations Reference: http://streaming.stat.iastate.edu/workshops/r-intro/lectures/ This post shares a downloadable PDF introduction to R from a statistics workshop, aimed at newcomers to the language. R is a free environment widely used for statistics, data analysis, and graphics. A guided introduction like this is a good starting point before moving on to packages such as ggplot2 and dplyr for visualization and data wrangling.

Plot histogram in RStudio using Shiny

Image
CODE: # '#' denotes a comment # install.packages("UsingR") uncomment if package is not installed library (UsingR) # install.packages("shiny") uncomment if package is not installed library (shiny) # install.packages("Hmisc") uncomment if package is not installed library (Hmisc) # install.packages("corrplot") uncomment if package is not installed library (corrplot) # getwd() gives the current directory where R looks up for files wd <- getwd() setwd(wd)