I am opening a large (347M) .R file. In Rstudio with R3.1.2
After mucking around with various input functions, I eventually got R to return this:
file("/Users/vincentlaufer/Desktop/all.t.subsets.R")
description class mode
"/Users/vincentlaufer/Desktop/all.t.subsets.R" "gzfile" "rt"
text opened can read
"text" "closed" "yes"
can write
"yes"
So, I tried working on opening it in the following way:
splat <- scan(gzfile("/Users/vincentlaufer/Desktop/all.t.subsets.R"), what="Factor")
(I chose Factor because Rstudio told me that "RDX2" is a Factor. I don't know what RDX2 is because I cannot open the file, probably a gene name..)
displaying the variable I created, splat, returns the following
> splat
[1] "RDX2"
[2] "X"
[3] ""
[4] "\002"
[5] ""
[6] "\025beta.combat.x.th1th17"
[7] "\033q?\xd9\xce\a_o\xd2"
[8] "?\xe0\x85\x87\x93\u0757\xf6?\xe1&\027\xc1\xbd\xa5\022?\xec\037!-w1\x90?\xed|\xed\x91hr\xb0?\xe0-\xe0"
[9] "\033qv?\xc9\xf2\022\xd7s\030\xfc?\xe8\xa3\xd7"
[10] "=p\xa4?\xe1\xdc\xc6?\024\022\006?\xee>BZ\xeec"
[11] "?\xee\027\xc1\xbd\xa5\021\x9d?\xe0\xb0\xf2{\xb2\xfe\xc8?\xe7"
I am a bit out of depth here. Can anyone tell me how to open this .R file.
The file is a binary files saved by R. (You can tell because of the RDX2 entry which says what kind of file it is - see http://biostat.mc.vanderbilt.edu/wiki/Main/RBinaryFormat)
You should try loading it using load("/Users/vincentlaufer/Desktop/all.t.subsets.R")
Related
In RStudio I can save my html widget to a single file with:
htmlwidgets::saveWidget(x,"filename.html",selfcontained=TRUE)
When running an R script with the above line through windows Task Scheduler (which calls Rscript.exe) this would generate a multi-file html (i.e. an html file with an associated directory of referenced stuff). Using RGui also failed to generate a single-file html.
These are the lines from an Rgui implementation that replicates the issue:
> library(leaflet)
> library(htmlwidgets)
> map <- leaflet::leaflet() %>% addTiles() %>% setView(lng=172.4,lat=-41,zoom=6)
> saveWidget(map,file="deleteme.html",selfcontained=TRUE)
Error in saveWidget(map, file = "deleteme.html", selfcontained = TRUE) :
Saving a widget with selfcontained = TRUE requires pandoc. For details see:
https://github.com/rstudio/rmarkdown/blob/master/PANDOC.md
> list.files(getwd(),full.names=FALSE, recursive=TRUE)
[1] "deleteme.html"
[2] "deleteme_files/htmlwidgets-1.5.3/htmlwidgets.js"
[3] "deleteme_files/jquery-1.12.4/jquery.min.js"
[4] "deleteme_files/leaflet-1.3.1/images/layers-2x.png"
[5] "deleteme_files/leaflet-1.3.1/images/layers.png"
[6] "deleteme_files/leaflet-1.3.1/images/marker-icon-2x.png"
[7] "deleteme_files/leaflet-1.3.1/images/marker-icon.png"
[8] "deleteme_files/leaflet-1.3.1/images/marker-shadow.png"
[9] "deleteme_files/leaflet-1.3.1/leaflet.css"
[10] "deleteme_files/leaflet-1.3.1/leaflet.js"
[11] "deleteme_files/leaflet-binding-2.0.4.1/leaflet.js"
[12] "deleteme_files/leafletfix-1.0.0/leafletfix.css"
[13] "deleteme_files/proj4-2.6.2/proj4.min.js"
[14] "deleteme_files/Proj4Leaflet-1.0.1/proj4leaflet.js"
[15] "deleteme_files/rstudio_leaflet-1.3.1/images/1px.png"
[16] "deleteme_files/rstudio_leaflet-1.3.1/rstudio_leaflet.css"
A clue to the cause is the error message returned after running the saveWidget function (though the web link provided no longer works). The error message points out that htmlwidgets::saveWidget function requires pandoc.exe application to be available for it to create a single-file html. If it is not available then it creates a multi-file html.
But I have pandoc.exe installed, as it comes with the RStudio bundle, which I use all the time.
As it turns out, the availability of pandoc.exe is checked by htmlwidgets::saveWidget using the rmarkdown::find_pandoc function. This checks for the existence of pandoc.exe in paths saved in the "RSTUDIO_PANDOC" and "PATH" environment variables and in the ~/opt/pandoc/ directory.
When using RStudio, the "RSTUDIO_PANDOC" environment variable is automagically set. It is not set when I run a script via Task Scheduler as, in that case I use Rscript.exe. When pandoc.exe is installed (it comes in the RStudio bundle) the PATH environment variable is not set so it can't be found that way either.
So how do I get saveWidget to save to a single html file when I'm not using RStudio?
One solution is to explicitly set the RSTUDIO_PANDOC environment variable.
Alternatively pandoc.exe could be added to the PATH environment variable.
Either of the above could be added to the .Renviron file.
Here is an example of the first approach:
> library(leaflet)
> library(htmlwidgets)
> map <- leaflet::leaflet() %>% addTiles() %>% setView(lng=172.4,lat=-41,zoom=6)
> Sys.setenv(RSTUDIO_PANDOC = "C:/Program Files/RStudio/bin/pandoc")
> saveWidget(map,file="deleteme.html",selfcontained=TRUE)
> list.files(getwd(),full.names=FALSE, recursive=TRUE)
[1] "deleteme.html"
if(!file.exists("./data/c3w4")){dir.create("./data/c3w4")}
url341 <- "https://d396qusza40orc.cloudfront.net/getdata%2Fprojectfiles%2FUCI%20HAR%20Dataset.zip"
download.file(url341, destfile = "./data/c3w4/pr341.zip")
I sure i used this set of command to download the file initially, but now that I am trying to reproduce code it is give the the error Error in download.file(url341, destfile = "./data/c3w4/") :
cannot open destfile './data/c3w4/', reason 'No such file or directory'
This is some relevant output from my console:
I did delete the "pr341.zip" file i downloaded the first time.
> getwd()
[1] "C:/Users/avtarsingh/Downloads"
> list.dirs()
[1] "."
[2] "./data"
[3] "./data/c3w4"
[4] "./data/c3w4/UCI HAR Dataset"
[5] "./data/c3w4/UCI HAR Dataset/test"
[6] "./data/c3w4/UCI HAR Dataset/test/Inertial Signals"
[7] "./data/c3w4/UCI HAR Dataset/train"
[8] "./data/c3w4/UCI HAR Dataset/train/Inertial Signals"
[9] "./new"
> list.files("./data/c3w4")
[1] "project.Rmd" "UCI HAR Dataset"
*(added with edit) Just noticed that if I copy and paste the code from R markdown to the console, it works. Why?
My experience with rmarkdown is that you really need to understand that the working directory is not the same as the working directory in the console and you can't make the assumptions that "." in a path implies. If you getwd() and use paste() to create the full file path that will usually work, but you must make sure you have created any folders that you need in whatever location (which may not be obvious) is being used as the working directory. Of course you can test this to find out and then construct things directly, meaning do a test document with getwd() to find out what the actual knitr working directory is, then build the file path from there.
Often for me I end up with something like
paste0("~","downloads/","filname")
After I installed RStudio, I ran:
library()
Warning message:
libraries '/usr/local/lib/R/site-library', 'usr/lib/R/site-library' contain no packages
Then I input:
.libPaths()
[1] "/home/avalon/R/x86_64-pc-linux-gun-library/3.2"
[2] "/usr/local/lib/R/site-library"
[3] "/usr/lib/R/site-library"
[4] "/usr/lib/R/library"
How can I remove [2] and [3] to prevent the warning message appear again?
Expected output:
.libPaths()
[1] "/home/avalon/R/x86_64-pc-linux-gun-library/3.2"
[4] "/usr/lib/R/library"
Linux
First thing to do is read the manpage on it (?.libPaths), and you'll see:
'.libPaths' is used for getting or setting the library trees that R knows about (and hence uses when looking for packages). If called with argument ‘new’, the library search path is set to the existing directories in unique(c(new, .Library.site, .Library)) and this is returned. If given no argument, a character vector with the currently active library trees is returned.
(emphasis added). This should clue us in to wonder what .Library.site holds. Oddly enough, it holds system-wide (ergo "site") library paths that "should" always be kept, so they are always maintained.
It further goes on to say:
'.Library.site' can be set via the environment variable 'R_LIBS_SITE' (as a non-empty semicolon-separated list of library trees).
So one way to fix it is to give it an empty string when you start R (cannot be done from within R):
# in bash on the command line:
$ R_LIBS_SITE=" " R
# in R
R> .libPaths()
[1] "/usr/lib/R/library"
The way to get this to work with RStudio is by creating a ~/.Renviron file with at least the following:
R_LIBS_SITE=" "
That done, you should not have to do anything further to remove the secondary site library paths from .libPaths():
R> .libPaths()
[1] "/usr/lib/R/library"
Windows
Assuming you're doing the following:
R> .libPaths(c("/home/avalon/R/x86_64-pc-linux-gun-library/3.2", .libPaths()))
[1] "/home/avalon/R/x86_64-pc-linux-gun-library/3.2"
[2] "/usr/local/lib/R/site-library"
[3] "/usr/lib/R/site-library"
[4] "/usr/lib/R/library"
If you want to correct it after you've done this, then just do:
R> .libPaths( c(.libPaths()[c(1,4)]) )
[1] "/home/avalon/R/x86_64-pc-linux-gun-library/3.2"
[2] "/usr/lib/R/library"
Alternatively, you can do it this way the first time (i.e., while it still has three elements, two of which are not good for you):
R> .libPaths(c("/home/avalon/R/x86_64-pc-linux-gun-library/3.2", .libPaths()[3]))
[1] "/home/avalon/R/x86_64-pc-linux-gun-library/3.2"
[2] "/usr/lib/R/library"
There is certainly a way to filter the paths programmatically instead of blindly taking the 3rd element, but this should work for now.
For a shiny application, I have a small issue with renderMarkdown.
Consider a text file with the following simple contents:
Markdown Test File
+ Item 1
+ Item 2
Let's save this file as "Markdown Test.txt". Now, let's read it in and process it, using the following R code:
filename <- "Markdown Test.txt"
text.in <- readLines(filename)
text.out <- renderMarkdown(text=text.in)
When I run this locally - i.e. on my Windows machine - I get:
> text.out
[1] "<p>Markdown Test File</p>\n\n<ul>\n<li>Item 1</li>\n<li>Item 2</li>\n</ul>\n"
This looks good. However, running the same code on the machine that hosts shiny server, I get:
> text.out
[1] "<p>Markdown Test File+ Item 1+ Item 2</p>\n"
As you can see, the Markdown conversion is far from perfect; e.g. the list is not converted.
On the Windows machine I have:
> Sys.getlocale()
[1] "LC_COLLATE=English_United States.1252;LC_CTYPE=English_United States.1252;LC_MONETARY=English_United States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252"
On the shiny machine, I get:
> Sys.getlocale()
[1] "LC_CTYPE=en_US.UTF-8;LC_NUMERIC=C;LC_TIME=en_US.UTF-8;LC_COLLATE=en_US.UTF-8;LC_MONETARY=en_US.UTF-8;LC_MESSAGES=en_US.UTF-8;LC_PAPER=C;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=en_US.UTF-8;LC_IDENTIFICATION=C"
So, I'm assuming that this has to do with the encoding, but the little I know about encoding I wish I didn't... my experiments with dos2unix and Sys.setlocale() let to nothing but frustration.
Would anyone happen to have a clever "one liner" that can fix this? Any help appreciated!
Thanks, Philipp
I'm not sure if R has a dedicated package to fix line encodings, but one way is to use sub to replace \r\n with \n (or just strip \rs).
How can I permanently remove a library in R?
.libPaths()
[1] "\\\\per-homedrive1.corp.riotinto.org/homedrive$/Tommy.O'Dell/R/win-library/2.15"
[2] "C:/Program Files/R/R-2.15.2/library"
[3] "C:/Program Files/RStudio/R/library"
The first item is my corporate "My Documents" folder, and the apostrophe in the path from my surname is causing all kinds of grief when using R CMD INSTALL --build on a package I'm making, not to mention issues using packages installed there when I'm offline from the network.
I want to use C:/Program Files/R/R-2.15.2/library as the default instead, but I don't want to have to rely on an Rprofile.site.
What I've tried
> .libPaths(.libPaths()[2:3])
> .libPaths()
[1] "C:/Program Files/R/R-2.15.2/library" "C:/Program Files/RStudio/R/library"
That seems to work, but only until I restart my R session, and then I'm back to the original .libPaths() output...
Restarting R session...
> .libPaths()
[1] "\\\\per-homedrive1.corp.riotinto.org/homedrive$/Tommy.O'Dell/R/win-library/2.15"
[2] "C:/Program Files/R/R-2.15.2/library"
[3] "C:/Program Files/RStudio/R/library"
I thought maybe .libPaths() was using R_LIBS_USER
> Sys.getenv("R_LIBS_USER")
[1] "//per-homedrive1.corp.riotinto.org/homedrive$/Tommy.O'Dell/R/win-library/2.15"
So I've tried to unset it using Sys.unsetenv("R_LIBS_USER") but it doesn't persist between sessions.
Additional Info
If it matters, here are some environment variables that might be relevant...
> Sys.getenv("R_HOME")
[1] "C:/PROGRA~1/R/R-215~1.2"
> Sys.getenv("R_HOME")
[1] "C:/PROGRA~1/R/R-215~1.2"
> Sys.getenv("R_USER")
[1] "//per-homedrive1.corp.riotinto.org/homedrive$/Tommy.O'Dell"
> Sys.getenv("R_LIBS_USER")
[1] "//per-homedrive1.corp.riotinto.org/homedrive$/Tommy.O'Dell/R/win-library/2.15"
> Sys.getenv("R_LIBS_SITE")
[1] ""
I've tried Sys.unsetenv("R_LIBS_USER") but this also doesn't stick between sessions
Just set the environment variable R_LIBS in Windows to something like
R_LIBS=C:/Program Files/R/R-2.15.2/library
Restart R.
This is bit late response to the question, but might be useful for others.
I order to set up my own path (and remove one of the original ones) I have:
used .libPaths() inside R to check current library paths;
identified which paths to keep. In my case, it kept R's original library but removed link to my documents.
found R-Home path using R.home() or Sys.getenv("R_HOME");
R-Home\R-3.2.2\etc\Rprofile.site is read every time R kernel starts. Therefore, any modification will be persistent to every run of R.
Edited Rprofile.site by adding the following,
.libPaths(.libPaths()[2])
.libPaths("d:/tmp/R/win-library/3.2")
How it works?
First line remove all but one path (second from the original list), the second line adds an additional path. We end up with two paths.
note that I use Unix path notation despite using windows. R always use Unix notation, regardless of operating system
restarted R (using Ctr+Shift+F10)
This will work every time now.
Use this function in .Rprofile
set_lib_paths <- function(lib_vec) {
lib_vec <- normalizePath(lib_vec, mustWork = TRUE)
shim_fun <- .libPaths
shim_env <- new.env(parent = environment(shim_fun))
shim_env$.Library <- character()
shim_env$.Library.site <- character()
environment(shim_fun) <- shim_env
shim_fun(lib_vec)
}
set_lib_paths("~/code/library") # where "~/code/library" is your package directory
Original Source: https://milesmcbain.xyz/hacking-r-library-paths/
I have put the Sys.unsetenv("R_LIBS_USER") command in a .Rprofile file in my windows "own documents" folder. Seems to help. My problem was that being in an active directory environment made R upstart and package loading incredibly slow when connected via vpn.
If you want to do this at RProfile file (#library/base/R/), you can search the lines where R_LIBS_* environment variables are set (for e.g. Sys.setenv(R_LIBS_SITE=....) and Sys.setenv(R_LIBS_USER=.....))
You can also search the code .libPaths(), which sets the library tree. So you can achieve your goal by a combination of commenting, unsetting and setting the R_LIBS variables before the .libPaths() call as you wish. For e.g. Something like:
Sys.unsetenv("R_LIBS")
Sys.unsetenv("R_LIBS_USER")
Sys.setenv(R_LIBS_SITE = "D:/R/libs/site")
Sys.setenv(R_LIBS_USER = "D:/R/libs/user")
Sys.setenv(R_LIBS = "D:/R/libs")