r
Plot two graphs in a same plot
lines() or points() will add to the existing graph, but will not create a new window. So you’d need to do plot(x,y1,type=”l”,col=”red”) lines(x,y2,col=”green”)
Folder management with r : Check existence of directory and create it if it doesn’t exist
Use showWarnings = FALSE: dir.create(file.path(mainDir, subDir), showWarnings = FALSE) setwd(file.path(mainDir, subDir)) dir.create() does not crash if the directory already exists, it just prints out a warning. So if you can live with seeing warnings, there is no problem with just doing this: dir.create(file.path(mainDir, subDir)) setwd(file.path(mainDir, subDir))
Create a group index for values connected directly and indirectly
Using igraph get membership, then map on names: library(igraph) # convert to graph, and get clusters membership ids g <- graph_from_data_frame(df1[, c(2, 3, 1)]) myGroups <- components(g)$membership myGroups # A B C D E F Z X Y W V U s T # 1 1 2 3 4 4 1 1 1 2 2 … Read more
How can I `print` or `cat` when using parallel
Using outfile param in makeCluster you can redirect the output to a file and then check that file to see how your program progresses. Interestingly on a Linux machine setting it to “” outputs to the console, but that doesn’t work for me on a Windows machine. File output works on both.
Deploying R shiny app as a standalone application [closed]
There is now a way to turn a Shiny app into a standalone Electron app (which is a desktop app, used for apps like Slack). To find out more, see this excellent presentation (YouTube) from useR 2018, which contains further links: GitHub ColumbusCollaboratory: electron-quick-start GitHub ColumbusCollaboratory: Photon. RStudio Add-in to build Shiny apps utilizing the … Read more
How to draw a nice arrow in ggplot2
Here are some reproducible examples (try running them) A Simple arrow (i.e. line segment): library(dplyr) library(ggplot2) # Create a scatter plot i <- ggplot(mtcars, aes(wt, mpg)) + geom_point() # Add arrow i + geom_segment(aes(x = 5, y = 30, xend = 3.5, yend = 25), arrow = arrow(length = unit(0.5, “cm”))) A Simple curved arrow … Read more
Horizontal Barplot in ggplot2
ggplot(df, aes(x=reorder(Seller, Num), y=Avg_Cost)) + geom_bar(stat=”identity”) + coord_flip() Without stat=”identity” ggplot wants to aggregate your data into counts.
How to pass command-line arguments when calling source() on an R file within another R file
I assume the sourced script accesses the command line arguments with commandArgs? If so, you can override commandArgs in the parent script to return what you want when it is called in the script you’re sourcing. To see how this would work: file_to_source.R print(commandArgs()) main_script.R commandArgs <- function(…) 1:3 source(‘file_to_source.R’) outputs [1] 1 2 3 … Read more
Append lines to a file
Have you tried using the write function? line=”blah text blah blah etc etc” write(line,file=”myfile.txt”,append=TRUE)