I'm using a kml package of R to cluster my data and I need to get in the end a csv file with a column including the number of clusters according to each id. The data has many missing values, so I can't use kmeans function without deleting all observations, but kml works nicely with that. My problem is that I use choice() to export the results and all I get is a graphical window, but no output files. Here is my code:
setwd("/Volumes/NATASHKA/api/R files")
statadata <-read.dta("Data_wide_withdemogr_auris_for_kml_negative.dta")
mydata <- data.frame(statadata)
cldDQ <- cld(mydata)
kml(cldDQ,c(2:6),20,toPlot="none")
plotAllCriterion(cldDQ)
par(mar = rep(2, 4))
X11(type = "Xlib")
choice(cldDQ, typeGraph = "bmp")
What do I do wrong?
I had the same problem and I solved it that way:
first, you need to choose the desired partition with the arrow
second, select it pressing “space”,
then press “Enter” and you can find all files in your work directory, check getwd().
Good luck.
Related
new stackoverflow user here, so apologies if this question/context is not up to standard. I welcome feedback.
I have two data frames that I would like to visualize as two separate tables in a PDF report. One of the data frames is a summary table that averages all the more granular level data from the second data frame. I am looking to put the summary table on top of the more granular level table. They are different sizes (different number of rows and columns), so let me know if that is a problem.
As for actual code/process, I am using the "formattable" package to make good looking tables; however I have no idea how to move on from there. I have tried using the grid.arrange function (from gridExtra package) and layout functions - but I get an error for grid.arrange and have not been able to figure out how to use the layout function (even after spending time reviewing other stackoverflow questions). Code is below:
library(formattable)
library(gridExtra)
m1 <- matrix("summary",1,3)
m2 <- matrix("granular",20,5)
t1 <- formattable(data.frame(m1))
t2 <- formattable(data.frame(m2))
layout(t1, t2, nrow=2)
grid.arrange(t1,t2, nrow=2, ncol=1)
for layout function I get the following error:
Error in layout(t1, t2, nrow = 2) : unused argument (nrow = 2)
and for grid.arrange i am getting the error:
Error in `$<-.data.frame`(`*tmp*`, "wrapvp", value = list(x = 0.5, y = 0.5, :
replacement has 17 rows, data has 1
Finally, as for the exporting to PDF part, I have not been able to even export ONE of the formattable class data frames, so I have no idea how I would export the both of them to a PDF. It appears in my working directory and also appears on the Plots window in RStudio, but it says: "There was an error opening this document. This file is already open or in use by another application." So I figure the file didn't process correctly. Using:
pdf("SamplePDF.pdf", width=11, height=8)
formattable(data.frame(m1))
dev.off()
I am doing this in R because I am taking advantage of the features of the "formattable" package and mean to eventually use the "RDCOMClient" to send these reports out automatically by email. Plus, SQL and R are basically the only "code" I know.
Thank you, and apologies in advance for a first-time poster!
grid.arrange and the other commands in the gridExtra package accepts grobs, so one option is to make your tables into table grobs with the tableGrob command. If you go this route though you are no longer in the formattable universe, though from this example its not clear that you need any of its special features.
library(gridExtra)
m1 <- matrix("summary",1,3)
m2 <- matrix("granular",20,5)
tg1 <- tableGrob(m1)
tg2 <- tableGrob(m2)
grid.arrange(tg1,tg2, nrow=2, ncol=1)
Then proceed to export to pdf. Hope that helps.
Something like this works for me:
pdf('test00.pdf')
grid.table(t1)
grid.newpage()
grid.table(t2)
dev.off()
Although this forces different tables to be on different pages.
I've made different plots (more than a hundred) for a project and I haven't capture them on the way (yes it's bad , i know). Now, I need to save them all at once but without running again my script (which takes hours). Is there a way to do so within Rstudio ?
Edit: All the plot are already there and I don't want to run them again.
In RStudio, every session has a temporary directory that can be obtained using tempdir(). Inside that temporary directory, there is another directory that always starts with "rs-graphics" and contains all the plots saved as ".png" files. Therefore, to get the list of ".png" files you can do the following:
plots.dir.path <- list.files(tempdir(), pattern="rs-graphics", full.names = TRUE);
plots.png.paths <- list.files(plots.dir.path, pattern=".png", full.names = TRUE)
Now, you can copy these files to your desired directory, as follows:
file.copy(from=plots.png.paths, to="path_to_your_dir")
Additional feature:
As you will notice, the .png file names are automatically generated (e.g., 0078cb77-02f2-4a16-bf02-0c5c6d8cc8d8.png). So if you want to number the .png files according to their plotting order in RStudio, you may do so as follows:
plots.png.detials <- file.info(plots.png.paths)
plots.png.detials <- plots.png.detials[order(plots.png.detials$mtime),]
sorted.png.names <- gsub(plots.dir.path, "path_to_your_dir", row.names(plots.png.detials), fixed=TRUE)
numbered.png.names <- paste0("path_to_your_dir/", 1:length(sorted.png.names), ".png")
# Rename all the .png files as: 1.png, 2.png, 3.png, and so on.
file.rename(from=sorted.png.names, to=numbered.png.names)
Hope it helps.
Although this discussion has been inactive for a while, there are some persons, like myself, who still come across the same problem, and the other solutions don't really seem to even get what the actual question is.
So, hands on. Your plot history gets saved in a variable called .SavedPlots. You can either access it directly, assign it to another variable in code or do the latter from the plots window.
# ph for plot history
ph <- .SavedPlots
In R 3.4.2, I could index ph to reproduce the corresponding plot in a device. What follows is rather straightforward:
Open a new device (png, jpeg, pdf...).
Reproduce your plot ph[index_of_plot_in_history].
Close the device (or keep plotting if it is a pdf with multiple pages).
Example:
for(i in 1:lastplot) {
png('plotname.png')
print(ph[i])
dev.off()
}
Note: Sometimes this doesn't happen because of poor programming. For instance, I was using the MICE package to impute many datasets with a large number of variables, and plotting as shown in section 4.3 of this paper. Problem was, that only three variables per plot were displayed, and if I used a png device in my code, only the last plot of each dataset would be saved. However, if the plots were printed to a window, all the plots of each dataset would be recorded.
If your plots are 3d, you can take a snapshot of all your plots and save them as a .png file format.
snapshot3d(filename = '../Plots/SnapshotPlots.png', fmt = 'png')
Or else, the best way is to create a multi-paneled plotting window using the par(mfrow) function. Try the following
plotsPath = "../Plots/allPlots.pdf"
pdf(file=plotsPath)
for (x in seq(1,100))
{
par(mfrow = c(2,1))
p1=rnorm(x)
p2=rnorm(x)
plot(p1,p2)
}
dev.off()
You can also use png, bmp, tiff, and jpeg functions instead of pdf. You can read their advantages and disadvantages and choose the one you think is good for your needs.
I am not sure how Rstudio opens the device where the plot are drawn, but I guess it uses dev.new(). In that case one quick way to save all opened graphs is to loop through all the devices and write them using dev.print.
Something like :
lapply(dev.list(),function(d){dev.set(d);dev.print(pdf,file=file.path(folder,paste0("graph_",d,".pdf"))})
where folder is the path of the folder where you want to store your graph (could be for example folder="~" if you are in linux and want to store all your graph in your home folder).
If you enter the following function all that will follow will be save in a document:
pdf("nameofthedocument.pdf")
plot(x~y)
plot(...
dev.off()
You can also use tiff(), jpg()... see ?pdf
I am trying to create a loop in R that does the following. I have a map with for example 10 datasets: file1.txt.csv, file2.txt.csv, etc. Now I simply want to create one graph per data set and output that, so that I get file1.jpeg, file2.jpeg, etc.
This is what I got so far:
fileNames <- Sys.glob("*.txt.csv")
for (fileName in fileNames) {
VolumeData <- read.delim(fileName, header = FALSE)
# convert data frame to data table
VolumeDatat <- VolumeData [, -(7:14)]
# set column names
setnames(VolumeDatat, c("MCS", "cell_type", "cell_number", "total_volume"))
jpeg(sub(".txt.csv",".jpg"))
plot(VolumeDatat$total_volume~VolumeDatat$MCS,type="l")
dev.off()
}
And I guess that
jpeg (sub(".txt.csv",".jpg"))
is the essential part for this, but I can't figure this out, also the arguments for jpeg in the library didn't help me much further.
First, I tried with jpeg (fileName.jpeg), which works, but only for the last file in the loop as the file gets overwritten for each time you run the script, e.g. each file.
So now I tried it with some sub-function, cause I thought that might work, but by doing so, I got:
Error in sub(".txt.csv", ".jpg") :
argument "x" is missing, with no default
Could anyone help me with this? I'd be very grateful!
I am using tableNominal{reporttools} to produce frequency tables. The way I understand it, tableNominal() produces latex code which has to be copied and pasted onto a text file and then saved as .tex. But is it possible to simple export the table produced as can be done in print(xtable(table), file="path/outfile.tex"))?
You may be able to use either latex or latexTranslate from the "Hmisc" package for this purpose. If you have the necessary program infrastructure the output gets sent to your TeX engine. (You may be able to improve the level of our answers by adding specific examples.)
Looks like that function does not return a character vector, so you need to use a strategy to capture the output from cat(). Using the example in the help page:
capture.output( TN <- tableNominal(vars = vars, weights = weights, group = group,
cap = "Table of nominal variables.", lab = "tab: nominal") ,
file="outfile.tex")
I am still new to R and I have searched around for a solution to my simple question, but I haven't found an answer that I've been able to get to work. I am looking to use a previously identified variable per data set, here variable=SNPname to include in script for automated generation of graph output in png format.
I am using this to generate a kmeans plot and have:
(cl <- kmeans(FilteredData[,6:7], 5, nstart=25))
png("C:/temp/$SNPnamegraph1.png") #SNPname to include in filename
plot(FilteredData[,6:7], col=cl$cluster)
points(cl$centers, col=1:5, pch=8)
dev.off()
where I want to include that variable in line 2 at the beginning of the file name. Is there a simple way to do this that I am just missing?
Close, you're just missing the use of paste() and setwd()
setwd("C:/temp/") # use this to set where you want things saved
...
c1 <- kmeans...
png(paste(SNPname, " graph1.png", sep=""))
...
If it's in a loop of some kind, you might need to use SNPname[loop_var]