Substitute not displaying text on plots - r

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

Related

How can I knit a .Rnw-file into latex, with plots as tikzpicture environments? (error: formal argument matched by multiple actual arguments)

I'm trying to convert a .Rnw document to latex (to be included in another .tex-document). All plots are generated using the tikz device, but each of them as a standalone document. My goal is to have them in tikzpicture environments.
Solution attempt
According to the knitr documentation on plots, it should be possible to pass standAlone=FALSE via the dev.args-list. This produces the error message formal argument "standAlone" matched by multiple actual arguments. Knitr issue #514 describes the same error message, but their solution doesn't work here.
To reproduce, run knitr::knit("mwe.Rnw") on the following file:
\section*{MWE}
<<echo=FALSE,dev="tikz",dev.args=list(standAlone=FALSE)>>=
plot(x=1:10)
#
Error message:
Quitting from lines 3-4 (mwe.Rnw)
Error in tikzDevice::tikz(..., packages = c("\n\\nonstopmode\n", packages, :
formal argument "standAlone" matched by multiple actual arguments
According to tikzDevice documentation, standAlone=FALSE should be the default anyway, no idea why this doesn't apply here.
Any help is greatly appreciated. A seemingly related, but less specific, question has been posted but without answer.
Setup
> sessionInfo()
R version 3.6.1 (2019-07-05)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Catalina 10.15.2
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.6/Resources/lib/libRlapack.dylib
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
knitr_1.27
tikzDevice_0.12.3
The argument standAlone is controlled by the chunk option external, so what you need is:
<<dev="tikz", external=FALSE>>=

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

can't open saved plot in 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()

Mathematical notion/superscripts in Rd files

I'm working on filling in an Rd file for a function.
When I use \eqn{2^{x}} in the Details section, then build and install the package, there is no superscripted exponent.
Looking at R-exts.pdf, it points to Poisson.Rd as an example on how to use \eqn or \deqn. In the example in that file, there is a superscripted exponent.
When I look at the help file for Poisson (?Poisson), There are no superscripted exponents.
Is this an issue on my computer or is this standard behavior?
Thanks!
> sessionInfo()
R version 2.11.1 (2010-05-31)
i386-apple-darwin9.8.0
locale:
[1] en_US.UTF-8/en_US.UTF-8/C/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] stats graphics grDevices datasets utils methods base
other attached packages:
[1] cimis_0.1-3 RLastFM_0.1-4 RCurl_1.4-2 bitops_1.0-4.1 XML_3.1-0 lattice_0.18-8
loaded via a namespace (and not attached):
[1] grid_2.11.1 tools_2.11.1
Nowadays, people mainly use the HTML help.
To get the superscript in the HTML help as well as in the PDF help, do:
\ifelse{html}{\out{2<sup>x</sup>}}{\eqn{2^x}}
The syntax is:
\ifelse{html}{\out{HTML CODE}}{\eqn{LATEX-LIKE CODE}{ASCII}}
with {ASCII} optional.
You don't say where you looked to see if there was a superscripted exponent. I presume the text based help, not the PDF version of the manual?
The syntax for the \eqn macro is \eqn{latex}{ascii}. The {ascii} bit is optional, in which case R will do it's best to render the LaTeX version. Conventionally, subscripts in ASCII would be wrapped in [] and superscipts with ^.
So I would write:
\eqn{2^{x}}{2^x}
But in all practical senses these are the same. The issue is just that the text help can't display superscipts, but the PDF can.

Resources