can't open saved plot in R - r

I am not able to open the plot when saved as pdf or other format. Consistently getting the following error. I am working on mac.
> plot(1:10)
> pdf('deleteIt.pdf')
> dev.off()
RStudioGD
2
> sessionInfo()
R version 2.15.1 (2012-06-22)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] pROC_1.5.4 plyr_1.7.1
loaded via a namespace (and not attached):
[1] tools_2.15.1
I am getting the following error. Itried with png and jpeg too, but no luck..
The file “deleteIt.pdf” could not be opened.
It may be damaged or use a file format that Preview doesn’t recognize.

You did it backwards. Try this
pdf("deleteIt.pdf")
plot(1:10)
dev.off()
Start the device. Write to it. Turn it off.
Alternatively, as pointed out by #Spacedman in a comment, you can create a pdf with whatever is currently plotted by using dev.copy like this:
plot(1:10)
dev.copy(pdf, "deleteIt.pdf")
dev.off()

Related

R and RStudio don't display greek letters and other symbols - display squares instead

I can't seem to display greek letters in r. I'm working with RStudio, and I can't seem to display greek characters no matter what I try. Initially I tried to display greek letters inside bquote, using syntax from a class I took, which the prof. published and demonstrated it worked in class. When all it showed was squares**, I tried more basic ways of displaying greek letters. I tried to use expression and than I tried to find the simplest way and ran this syntax which I found online:
mycoef <- rnorm (1000)
hist(mycoef, main = expression(beta))
In all cases it either display a square (like an unrecognized symbol), or just the word (beta, chi, etc.,). This syntax also displayed a square using R instead of RStudio.
I tried to change a bit the encoding of the file (ASCII/UTF-8), but this didn't change anything. I am working on a mac (macOS Sierra 10.12.4) if that changes anything.
**when using the bquote syntax, greek letters were not the only characters it changed to squares. Also == and ~~ without quotes were displayed as squares.
This is my sessionInfo() -
R version 3.4.2 (2017-09-28)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Sierra 10.12.4
Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRlapack.dylib
locale:
[1] pt_PT.ISO8859-1/pt_PT.ISO8859-1/pt_PT.ISO8859-1/C/pt_PT.ISO8859-1/C
attached base packages:
[1] grid stats graphics grDevices utils datasets methods base
other attached packages:
[1] plyr_1.8.4 meta_4.8-4 openxlsx_4.0.17
loaded via a namespace (and not attached):
[1] compiler_3.4.2 tools_3.4.2 Rcpp_0.12.13
=====
Note that as a try to fix this problem I ran this syntax that is displayed in the sessionInfo - Sys.setlocale("LC_ALL", "pt_PT.ISO8859-1"), which didn't help.
Thanks!
You can try using package latex2exp if you know the basics of LaTeX:
library(latex2exp)
mycoef <- rnorm (1000)
hist(mycoef, main = TeX("$\\beta$"))
I solved this problem updating the Symbols Font on Windows. (1) Download the file Symbol.ttf (https://www.fontsupply.com/fonts/S/Symbol.html),
(2) Find the file symbol.ttf in your \Windows\Fonts folder, and replace with the fresh copy.

RStudio suddenly stopped showing plots in the plot pane

RStudio is not showing the generated plots in the plot pane anymore. Instead they open in a separate file with the name "Quartz". Here's my session info.:
> sessionInfo()
R version 3.3.1 (2016-06-21)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.9.5 (Mavericks)
locale:
[1] en_CA.UTF-8/en_CA.UTF-8/en_CA.UTF-8/C/en_CA.UTF-8/en_CA.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] cluster_2.0.4 tm_0.6-2 NLP_0.1-9 ggplot2_2.1.0
loaded via a namespace (and not attached):
[1] colorspace_1.2-6 scales_0.4.0 plyr_1.8.4 parallel_3.3.1 tools_3.3.1 gtable_0.2.0 Rcpp_0.12.5 slam_0.1-35
[9] grid_3.3.1 munsell_0.4.3
I tried tools > global options > panes > plots is selected.
I don't know what other info to share. I can see the plot pane with "plots" tab active. But it's blank.
Screen:
Notice the new menu with "Quartz" appear. Not sure what this is, but it comes up when R generates the plot and the usual RStudio menu disappears.
How do I get RStudio to display plots in the plots pane?
Try running this:
dev.off()
Check if it works:
plot(rnorm(50), rnorm(50))
I am working with Rmarkdown, and in my case I solved this problem like this:
Go to Tools->Global Options->Rmarkdown
In "Show output preview in" select "Viewer Pane"
Uncheck the box "Show output inline for all R Markdown documents"
Follow below procedure to resolve the issue.
dev.off()
print(plot(1)) # Basically use print command once
now use plot command as you normally use and should work fine.
For me, it worked when I first closed all the devices #udit-gupta. Recursively closing all the devices after finding the devices.
dev.cur()
dev.off(i) #where i = index of device to be switched off
get the device back with
getOption("device")
or
dev.set(which = dev.next())
I was able to visualize plots in the plot pane for a while before it vanished. All the attempts with the suggestions above failed. What is referred in the blog below got it working.
https://datasciencelearner.wordpress.com/2014/08/17/my-plot-is-not-showing-up/
The trick is to wrap the graphics command inside print function, for instance:
print(
plt2 <- ggplot(housing,
aes(x = Home.Value)) +
geom_histogram()
)
I had the same problem after updating R to version 3.3.2 and using RStudio 0.98.0. Installing the latest version of RStudio fixed the problem.
I had the same problem and none of the above solutions fixed the issue. I completely uninstalled the Rstudio and installed it again and only after that the Rstudio started to show my plots in the plot pane again.
It could be because graphic device are already on .
To find indices of open devices:
dev.cur()
To close devices:
dev.off(i)
where i is index of device to be switched off.
enter dev.off() command and Enter, it will fix the problem.
Go to "view" on the menu bar.
Select "panes".
Mark "all panes".

Substitute not displaying text on plots

I just recently learnt about plotmath and its ability to put mathematical expression in text on plots.
However I'm encountering some strange behaviour where the substitute function does not render any text.
Here's a simple case that should work (from this answer):
labNames <- c('xLab','yLab')
plot(c(1:10),
xlab=substitute(paste(nn, x^2), list(nn=labNames[1])),
ylab=substitute(paste(nn, y^2), list(nn=labNames[2])))
When I run this code snippet, there is no xlab or ylab text.
I'm running mac osx and R 3.0.0. This problem persists whether i output to Quartz or to a png file.
Here's my sessionInfo():
R version 3.0.0 (2013-04-03)
Platform: x86_64-apple-darwin12.3.0 (64-bit)
locale:
[1] en_AU.UTF-8/en_AU.UTF-8/en_AU.UTF-8/C/en_AU.UTF-8/en_AU.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
I found the culprit: there was a weird interaction with an option in my .Rprofile
The following line causes the issue if i don't load the devtools package:
options(devtools.desc.author="'Scott Ritchie <sritchie73#gmail.com> [aut, cre]'")
Strangely, there doesn't seem to be an issue if i define other irrelevant options (options(notanoption=TRUE) caused no issues).

ggplot2 ggsave function causes graphics device to not display plots

I've updated my ggplot2 library and I am now having a problem where after calling ggsave, the graphics device no longer displays the plot. I'm unable to resolve the issue meaning I cannot export the plots as needed. Please help.
Searching the web has revealed some mention of problems with dpi mismatches, but setting or changing the dpi appears to have no effect. This problem appears to have begun after doing an update of the ggplot2 library.
The following code reproduces the problem:
library(ggplot2);
df <- read.table(textConnection('value,value1
1,1
2,2
3,3 '),header=TRUE,sep=',');
ggplot(df,aes(x=value,y=value1)) + geom_point();
setwd('c:/temp');
ggsave('test.png');
After running, the output is blank, and the graphics device no longer displays the plot. Any subsequent plots are not displayed on the graphics device.
After running dev.off(), new plots are displayed okay, until ggsave is called again.
sessionInfo() reports the versions of R and libraries as:
R version 3.0.0 (2013-04-03)
Platform: i386-w64-mingw32/i386 (32-bit)
locale:
[1] LC_COLLATE=English_New Zealand.1252 LC_CTYPE=English_New Zealand.1252
[3] LC_MONETARY=English_New Zealand.1252 LC_NUMERIC=C
[5] LC_TIME=English_New Zealand.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] plyr_1.8 ggplot2_0.9.3.1 RODBC_1.3-6
loaded via a namespace (and not attached):
[1] colorspace_1.2-2 dichromat_2.0-0 digest_0.6.3 grid_3.0.0 gtable_0.1.2
[6] labeling_0.1 MASS_7.3-26 munsell_0.4 proto_0.3-10 RColorBrewer_1.0-5
[11] reshape2_1.2.2 scales_0.2.3 stringr_0.6.2 tools_3.0.0
The problem occurs in 2.15.0 and 2.15.3 of R also.
EDIT: changed code so data would load using sep=',' rather than whitespace
ADDITION: further testing reveals that this may in fact be a bug with RStudio and how it is displaying the graphics device after ggsave, rather than R itself. Running the above script in RGui does not reproduce the problem, only in RStudio V0.97.336 + V0.97.449.
Seems to be an issue with RStudio. The issue is described here and here.
The workaround is to install RStudio V0.97.311, or ensure that you execute the ggplot and ggsave lines separately (i.e. don't select and run together in a block with control+enter).

error with function regmatches using R

I'm trying to use the function regmatches, in R, but i'm getting an error (R says it can't find the function).
I'm quite tired now and maybe I'm doing some really silly mistake. However, I don't know what's wrong.
I tried to use ??regmatches, but it didn't find anything.
Next I tried the "sos" package and function findFn("regmatches"), but it returned:
found 0 matches
x has zero rows; nothing to display.
Mensagens de aviso perdidas:
In findFn("regmatches") : HIT not found in HTML; processing one page only.
I don't know what else to do.
I know I may be doing a silly mistake like this one, but I don't know what else to do. Below is my session info:
R version 2.12.2 (2011-02-25)
Platform: i386-pc-mingw32/i386 (32-bit)
locale:
1 LC_COLLATE=Portuguese_Brazil.1252 LC_CTYPE=Portuguese_Brazil.1252
[3] LC_MONETARY=Portuguese_Brazil.1252 LC_NUMERIC=C
[5] LC_TIME=Portuguese_Brazil.1252
attached base packages:
1 stats graphics grDevices utils datasets methods base
other attached packages:
1 sos_1.3-1 brew_1.0-6
loaded via a namespace (and not attached):
1 tools_2.12.2
regmatches was added in R 2.14.0...
I'm sorry guys. It happens that is was a silly mistake. After almost giving up, I decided to check if it was a problem with my version of R. So, I took a look at the CRAN webpage and found this under what's new with R 2.14.
New function regmatches() for extracting or replacing matched or non-matched substrings from match data obtained by regexpr(), gregexpr() and regexec().
So, It's time to update R. I thought that using version 2.12.2 wasn't that bad, but it happens it was.

Resources