How to embed/display code in texmaker? - r

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.

Related

Rmarkdown fails to render inline plots when opened within an R project on GoogleDrive

I have an R project held onto a Google Drive. I access both the R project and associated markdowns using the Google Drive desktop app. I recently updated my macOS to Monterey 12.2.1 and since then, I haven't been able to render plots inline within a markdown. I get the following error message when trying to print any plot:
Error in dev.off() :
QuartzBitmap_Output - unable to open file '/Volumes/GoogleDrive/Shared drives/.../.Rproj.user/shared/notebooks/-.../1/s/cqgw7b5dagxzw_t/_rs_chunk_plot_001.png'
This seems to be specific when opening a markdown, within an R project, onto a Google Drive. I have tried having a project on my local machine and opening the same markdown, plots display inline fine. I have tried opening the markdown outside of a project, again, can display plots inline fine. The plots also display in the viewer pane fine regardless and knitting is also not an issue.
The only clue I have is that when I go within the .Rproj.user directory, I can find a folder with the naming 'cqgw7b5dagxzw' (see path above), but it is missing the '_t'. Not sure what that could mean though.
Since updating the OS I have also reinstalled XQuartz.
I can still get to the plots, so it's not a HUGE issue, but the inline display feature is rather handy to make quick modifications to my visualisations.
Below are some session info.
Thanks!!!
PS: Plot twist! I can display plots inline with ggplotly?!?!
Workaround
I found this workaround which displays the plots in the viewer pane instead. It is definitely an improvement, but displaying plots inline allows you to get an idea of the proportions of the plot you will eventually knit.
> sessionInfo()
R version 4.1.2 (2021-11-01)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Monterey 12.2.1
Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRlapack.dylib
locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8
I found an issue someone logged on Rstudio github relates to this, linked here.
It seems you're right about the niche scenario of Rmarkdown + R Project + Google Drive + Inline plotting.
Someone in the comments suggested going to Preferences>General>Graphics and changing the back end option to Cairo instead of Quartz. Tried that on my end and seems to work when I tried a basic histogram with GGplot2.
Not sure if using Cairo makes anything else worse though.
Using MacOS Monteray (12.3.1) if that helps.

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

R Notebook - code chunk gets separated in preview

I am using R notebook to preview the file. Even though all the code is in one chunk, a line gets separated into two. Would you please tell me how I can resolve this?
As you can see all the code is in one chunk.
However, when I run this chunk, it gets separated into two.
I am not sure if it matters but here is the code I am running.
library(MASS)
par(mfrow=c(3,1))
hist(galaxies, breaks=500)
hist(galaxies, breaks=100)
hist(galaxies, breaks=50)
Also here are the versions of R and RStudio. I an new to R so I am not sure what is causing this. If you need any other information, please let me know. Thanks
R version 3.3.2 (2016-10-31)
Rstudio version 1.0.136

How to find words by Ctrl + F on R data sets WIndow or R console?

I wanted to see all the data sets in all the available packages in R, for which the command I used was
data(package = .packages(all.available = TRUE))
This results in an R window called R data sets window like below showing 100s of data sets listed package wise.
Now if I want to find something based on words in the above Window, Example say word cancer in that window, I am not able to use ctrl+f on it. How can I use ctrl+f or any other find method in any of the R Windows that pops up. Even in console we can not use searching.
Of course we can copy paste text and search. I want to know if we can do it in R itself.
I can perform ctrl+f only on R Script Window and not any other Window.
Session Information:
sessionInfo()
# R version 3.3.1 (2016-06-21)
# Platform: x86_64-w64-mingw32/x64 (64-bit)
# Running under: Windows 10 x64 (build 14393)
Any help on this is highly appreciated.

Right to left languages support in R, using Mac

I am wondering if there is anyway to support right-to-left languages in R while using Mac
For instance suppose the following code:
x <- data.frame(a=runif(10),b=runif(10))
ggplot(x, aes(a,b)) + geom_point() + xlab("سلام")
Here is the result:
Here I tried to change the x-label as a Persian/Arabic word (سلام = Hello). While displayed correctly in the code (using R-Studio), in the graph the characters of سلام are displayed in the reverse order (left-to-right).
Also the linkage between two consecutive letters (i.e. س connected to ل would be سل) is broken.
Do you have any idea how to fix it?
Update
With the answer of #agstudy I found R under Ubuntu is perfectly OK. However I have no idea how to solve the problem in Mac (OS X 10.9.1) having updated version of R and R-Studio
It works fine for me. I just change the size of labels to better show it.
library(ggplot2)
x <- data.frame(a=runif(10),b=runif(10))
ggplot(x, aes(a,b)) +
geom_point()+ xlab('سلام') +
theme( axis.title=element_text(size=100,face="bold"))
I am using :
other attached packages:
[1] ggplot2_0.9.3.1
R version 3.0.2 (2013-09-25)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Also works fine under :
R version 3.0.3 (2014-03-06)
Platform: x86_64-pc-linux-gnu (64-bit)
I had the same problem with Hebrew letters, and I manage to work around it by reading the label from an Excel file instead of typing it directly into R studio. This method works as long as you don't need to mix letters with numbers, in that case things start to get messy.
for example:
I know this is an old question, but I recently wrote an R package to deal with it. It's a simple R wrapper around some python code, since this problem also occurs when using python in mac. The package reverses the Arabic string and then reconnects the letters correctly using Abdullah Diab's python-arabic-reshaper module.
My package is here.
Abdullah Diab's module (for python) is here.

Resources