Unable to save/open pdf file in R - r

I am new to R programming and I am trying to learn it. So, please bear with me if this question is silly!
I am trying to execute the below code in RStudio and I get the following error:
R version 3.6.2 (2019-12-12) -- "Dark and Stormy Night"
Copyright (C) 2019 The R Foundation for Statistical Computing
Platform: x86_64-w64-mingw32/x64 (64-bit)
[Workspace loaded from ~/.RData]
> x=rnorm(100)
> y=rnorm(100)
> pdf("Figure.pdf")
**Error in pdf("Figure.pdf") : cannot open file 'Figure.pdf'**
> plot(x,y, col="green")
> dev.off()
null device
I am unable to save or open the pdf file. I tried t o check my permissions and I also ran the Rstudio with administer rights but no luck!

One can direct the output of R graphics functions to PDF files through the use of the pdf() function.
The file = argument is a named argument (versus a positional argument), and therefore one needs to use the name in order to change its value. The reasoning for this is that the PDF device function's default value for file = allows multiple PDFs to be written, per the R documentation for pdf().
x=rnorm(100)
y=rnorm(100)
pdf(file = "Figure.pdf")
plot(x,y, col="green")
dev.off()
...produces a PDF in the current R working directory that contains the following image.

Related

R: Change default jpeg() export quality via console

how can I change the default jpeg() export quality via console in R version 4.0.3 (2020-10-10) -- "Bunny-Wunnies Freak Out"?
Here is an example: jpeg('../data/all/graphic1.jpg', quality=100)
But I don't want to change all separate jpeg() command...
Thanks for your help
You could define a new function that over-rides the default function with a version such as:
jpeg <- function(filename, quality=100, ...) grDevices::jpeg(filename, quality, ...)
This would work for most common situations where the jpeg function is used (say in a user script), but may not work correctly when the jpeg function is being called from within functions in other packages.

An issue with tiff and ggplot R functions inside for loop

]
2
I was not able to get a batch of images with tiff() R function inside for loop.
I have x86_64-w64-mingw32/x64 (64-bit) platform with installed Windows 8.1 and RStudio Version 1.0.143 with R version 3.6.0 (2019-04-26) -- "Planting of a Tree". I tried to obtaine a batch of tiff images by putting my code inside the for loop. Unfortunately that resulted in the numerous broken files (in attachment). But if i type exactly the same code in the RStudio console i got normal (valid) image.
#Fragment of the script
for (i in 1:nrow(BH))
{
...
tiff( paste(pos$rs[1],"tiff",sep = '.'))
ggplot(df_g_,aes(x=factor(g),fill=factor(O)))+geom_bar(stat="count")+xlab("")+labs(fill='')
dev.off()
}
#If i perform the following code in console after script execution i got a valid image
tiff( paste(pos$rs[1],"tiff",sep = '.'))
ggplot(df_g_,aes(x=factor(g),fill=factor(O)))+geom_bar(stat="count")+xlab("")+labs(fill='')
dev.off()
The solution was to envelop ggplotfunction by base print function in the loop:
print(ggplot(df_g_,aes(x=factor(g),fill=factor(O)))+geom_bar(stat="count")+xlab("")+labs(fill=''))
So, i've used the solution from the other post:
enter link description here

Cannot create PDF by ggplot2 within if-statements

Consider a minimal example: generating an empty plot with ggplot2 and put it into a PDF file. Typically people would do
pdf()
ggplot()
dev.off()
and it works as expected. However, when you wrap these statements into an if-statement, the resulting PDF file becomes corrupted.
if (TRUE) {
pdf()
ggplot()
dev.off()
}
This issue has been reproduced on both Windows 10 and macOS Mojave. I'm using the latest version of R
$ R --version
R version 3.6.0 (2019-04-26) -- "Planting of a Tree"
Copyright (C) 2019 The R Foundation for Statistical Computing
Platform: x86_64-apple-darwin18.6.0 (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under the terms of the
GNU General Public License versions 2 or 3.
For more information about these matters see
https://www.gnu.org/licenses/.
To save a ggplot object using pdf or jpg functions, one has to explicitly print the ggplot.
if (TRUE) {
pdf()
print(ggplot())
dev.off()
}
This is stated in R-FAQ 7.22 Why do lattice/trellis graphics not work?
The most likely reason is that you forgot to tell R to display the graph. Lattice functions
such as xyplot() create a graph object, but do not display it (the
same is true of ggplot2 graphics, and Trellis graphics in S-PLUS). The
print() method for the graph object produces the actual display. When
you use these functions interactively at the command line, the result
is automatically printed, but in source() or inside your own functions
you will need an explicit print() statement.
So, it would not save if one simply source a script even in the absence of a conditional statement or loop (source with echo = TRUE would save the plot).
I personally prefer to use the ggsave function since it seems more flexible.
if (TRUE) {
ggplot()
ggave(filename = "plot.pdf")
}

R View() function does not show data frame

I am using R (Version 3.1.2) with RStudio (Version 0.98.1091) on a Linux Ubuntu machine. I have a csv file loaded in variable:
rr <- read.csv("/home/user/seconds.csv")
When I call View(rr) I expect to open a new tab and display the CSV data in the rr variable. However, it opens a new tab called rr and it displays the following message.
/content?title=rr&file=94af460bd6644d3aaac734d585046c4f.htm not found
The View function is from package 'utils' which is not supported by the version of R you are using.
I am on a x86_64-w64-mingw32 system with R version 3.4.1, but I want to share a simple solution to get View working again in my case.
Following the step 4 of advice from Tian on Rstudio support (https://support.rstudio.com/hc/en-us/community/posts/115007743908-The-Rstudio-view-not-work-just-blank-), I entered:
install.packages("dplyr", dependencies = TRUE)
Now it works.

How to embed/display code in texmaker?

I am new to texmaker. I have difficulty in displaying the codes and results I got from R in my texmaker document.
For example I have this results:
> sessionInfo()
R version 3.0.2 (2013-09-25)
Platform: x86_64-w64-mingw32/x64 (64-bit)
How do I display that in texmaker that it looks like the one you are seeing? (indented, gray background, looks like a code from R).
What do I need to have?
Thanks!
Would the listings package (which supports R) achieve what you want?: http://en.wikibooks.org/wiki/LaTeX/Source_Code_Listings
In LaTeX you can also just use the verb(atim) environment for fixed-width, not interpreted text:
/begin{verb}
> R code goes here...
/end{verb}
?
If you want to display both the command and the results, then don't you need to execute the R code? (Unless you are planning to copy and paste the results...).
I would suggest checking out the knitr package.

Resources