keep some comments out of the console - r

I'm writing code that will be used by others later and I'd like two kinds of comments to clarify the code.
comments that will appear in the console when run so the user sees where in the process R is
comments they'll only need when they need to tweak the code
#### Recognise, select and clean needed data #### <- I would like this in the console always
source("scripts/3 Recognise rename select and clean needed data.R") # <- my question is not on using source
# this will guarantee you have the column names X Y Z in your data <- I would like to keep this out of the console
Is there a way to keep some comments from not appearing in the console?
I searched the internet and the setting in RStudio, but found nothing that addresses this issue specifically.

Related

How Do I View All Output in Rstudio Console When the Output is Large

Take for instance,
rnorm(9999999)
produces a very large output in the console that even if I scroll up to the last limit, I can not see all output.
I prefer solution(s) that will not require me to save the output.
Do
oo <- options(max.print=2e+06) ## set and store defaults
options(oo) ## restore defaults
for two million rows to display (9999999 / 5).
In addition consider the lines cut-off. In RGui it may be configured using [Edit] > [GUI preferences] (see image below).
There are similar options for RStudio, consult this answer for more information.

Indentation issue in RStudio

Like many others, I like writing my codes on several lines and I appreciate the auto-indent feature offered by RStudio. The issue suddenly started when the word undefined showed up in the code editor whenever I pressed Enter as I expected an automatic indentation. It is important to mention that this does not affect the proper running of the code.
my_fun <- function(xxx){
undefined
}
NB: Actually, when I copied and pasted the code here, undefined was not copied to! I had to manually add it. Here is another example.
library(magrittr)
mtcars %>%
undefinedhead()
Basically this is just RStudio telling you that you have not preset your tab spaces, but it is not actually dropping commands into your code.
You have the option of setting tabs to however many spaces you feel is appropriate and it allows teams to work with consistent spacing.
If you go to the main menu and click on Tools > Global Options and on the second tab in the left column click onCode . There you can set the spaces to meet your personal needs (or teams profile) and the undefined message will disappear.
This photgraph shows where you can find the preferences.

R studio List and Dataframes auto name completion not working anymore

Suddenly R Studio stopped auto completing my list and data frame contents when entering mylist$ then pressing Tab. The result message I get is
No matches
I tried that on very simple lists or data frames like:
simpledataset <- data.frame(a=rep(0,100),b=rep(1,100))
However, when I load the dataset Mtcars, it correctly shows the column names.
I reset %localappdata%\RStudio-Desktop by renaming it, but didn't change anything.
Any idea of what could be the cause of that?
This sounds like a bug in the autocompletion system that is occasionally triggered when certain packages are loaded, or the byte-compiler optimization setting is increased. You might try checking a few things:
What is the value of compiler::getCompilerOption("optimize")? Autocompletion is known to fail when this value is equal to 3 in the current RStudio release (v0.99.896). You can try running compiler::setCompilerOptions(optimize = 2) to resolve this.
What packages do you have loaded? You can try loading packages one-by-one to see which affects RStudio's ability to provide autocompletions.
Finally, there should be a fix for this in the preview version of RStudio -- you can try it out at https://www.rstudio.com/products/rstudio/download/preview/.
Go to Tools>Global Options... and set autocomplete there.

Saving R history from a particular session

I'm an instructor, and my students have expressed interest in a record of the code I run in class. Since this is often off-the-cuff, I would like to have an easy function I could run at the end of class which would save everything I had run in the session. I know savehistory() will save my entire history, but that is not what I'm looking for.
Similar to this question, except I believe I have a pedagogically sound reason for wanting the history, and I'm looking to limit what gets saved on a per-session basis, rather than on the basis of the number of lines.
I think if you invoke R with --no-restore-history (so that history from previous sessions isn't appended to the record for this one) and add
.Last <- function() {
if(interactive())
try(savehistory(paste0("~/.Rhistory_", sys.time())))
}
to your Rprofile, you should get self-contained, and timestamped history files every time R closes naturally.
The .Last function, when defined in the global environment, is called directly before normal close. See ?.Last
NB: this won't preserve your history in the case of a fatal error (crash) in R itself, though that shouldn't come up much in a teaching context.
NB2: the above code will have generated file names with spaces in them. Depending on your OS, this could range from no big deal to hairpulling nightmarescape. If it's a problem, wrap sys.time() with your favorite datetime formatting code, e.g. format(sys.time(), "<format string>") or something from lubridate (probably, I don't actually know as I don't use it myself).
In the development version of rite on GitHub (>= v0.3.6), you can use the sinkstart() function to dump all of your code, all of your results, or both to a little interactive Tcl/tk widget, from which you could then just copy or save the output.
To make it work you can do this:
devtools::install_github("leeper/rite")
library("rite")
sinkstart(print.eval = FALSE, prompt.echo = "", split = TRUE)
## any code here
sinkstop() # stop printing to the widget
That will look something like:
You can dynamically change what is printed in the widget from the context (right-click) menu on the widget. You can also dynamically switch between sinkstart() and sinkstop() if you only want some code and/or results output to there.
Full Disclosure: This is a package I developed.

R View() does not display all columns of data frame

I have been adding columns to a data frame and using View() to check that it did what I expected. I have repeated lines of code along the lines of:
x$p <- 3 * x$a
x$q <- sqrt(x$b + x$c)
View(x)
This worked fine until the number of columns exceeded 100 (there are 47,000 rows). When I added another two columns, dim(x) shows 102 columns, names(x) shows 102 names, summary(x) shows summaries of all the expected columns. However, View(x) only displays the first 100 columns and doesn't display the last two added columns.
If I try View(x[,-(1:10)]) the most recently added columns are displayed.
I can't see any mention in the View documentation of a limit on the number of columns. Can anyone explain what is happening here?
(Updated)
You can have View() open in one of the quadrants or in a separate notepad-ish window. It opens in the quadrant where my source code is displayed on my machine at work, and in another window on my machine at home. In the latter case, it displays >1k rows & >100 columns (I just checked).
I'm not sure how you can get this to change permanently, IIRC when I updated RStudio and ran View() the first time, a window popped up and asked me to choose what program I wanted to use to display the file. In one case I selected RStudio, and in the other case, I selected notepad. In both cases, the 'use this program by default from now on' radio button was selected; I have never seen this window since. If you can switch to displaying with notepad, you should be able to get out of this problem. However, short of a permanent change, you can get View() to display your data in a separate window using the code utils::View(). This approach works on my machine at work. Here is what it looks like:
Note that I am running RStudio version 0.97.248 on a Windows 7 machine.
Here is what it looks like on my home machine where it comes up in a new window automatically:
I also see this problem with x <- matrix(1:200,nrow=1); View(x) in RStudio, but not in vanilla R. It is a known limitation and they're working on it. You can contact the devs on their forum to give your feedback (and have done so, I see).
I just ran into this issue also. As suggested by gung above, the utils::View() function is helpful as a workaround for browsing all available columns in a data frame, whereas Rstudio still defaults to only the first 100 available columns when using the View() function.
The workaround is very useful for identifying the column names for creating a subset from an existing data frame. However, it doesn't provide a quick column enumeration that the RStudio View() function allows. It's been a few years since the original post in 2013, but this limitation in the RStudio environment seems to still be effective in present-day 2017.
I found a solution that worked for me in a closed RStudio Github issue. You can change the maximum number of columns displayed (say to 1000) using the following command:
rstudioapi::writeRStudioPreference("data_viewer_max_columns", 1000L)
You should be able to run this just once, and it will thereafter be saved to your settings file as the new default in every subsequent R session. Under my Linux system, these preferences are stored in ~/.config/rstudio/rstudio-prefs.json. The relevant line that the above command will add is:
"data_viewer_max_columns":1000
Try fix(). It loads all your columns and rows. The only problem is that it might take long to load large data frames.
I'm not sure if this has been mentioned before but I found this interesting post from 2012:
https://support.rstudio.com/hc/en-us/community/posts/200669267-view-more-than-first-100-columns-.
This indexing allows you to at least check the other columns and if they even exist.
So just use: datafile[row-row, column-column].

Resources