Asked  7 Months ago    Answers:  5   Viewed   128 times

I am running R on Windows, not as an administrator. When I install a package, the following command doesn't work:

> install.packages("zoo")
Installing package(s) into ‘C:/Program Files/R/R-2.15.2/library’
(as ‘lib’ is unspecified)
Warning in install.packages :
  'lib = "C:/Program Files/R/R-2.15.2/library"' is not writable

To install a package, I have to specify a library location:

install.packages("zoo", lib="C:/software/Rpackages")

To load a package, I also have to specify the library location:

library("zoo", lib.loc="C:/software/Rpackages")

All of this is OK, but I wanted to see if I could add C:/software/Rpackages to the library path somehow and thus not have to type it each time.

As I searched online, I found that one way to do this is to edit the Rprofile.site file and to add the line

.libPaths("C:/software/Rpackages")

However, after doing this, and starting RStudio, this is the output that I get

> .libPaths()
[1] "C:/Program Files/R/R-2.15.2/library" "C:/Program Files/RStudio/R/library" 

The .libPaths command that I added to the Rprofile.site doesn't seem to have had any effect! Why is this the case? Or more importantly, how can I fix the problem so that I can install and load packages without typing in the library location?

Note: if I start RStudio the .libPaths() command seems to work as it is supposed to

.libPaths("C:/software/Rpackages")
> .libPaths()
[1] "C:/software/Rpackages"               "C:/Program Files/R/R-2.15.2/library"

Isn't that strange?

 Answers

100

I generally try to keep all of my packages in one library, but if you want to add a library why not append the new library (which must already exist in your filesystem) to the existing library path?

.libPaths( c( .libPaths(), "~/userLibrary") )
 # obviously this would need to be a valid file directory in your OS
 # min just happened to be on a Mac that day

Or (and this will make the userLibrary the first place to put new packages):

.libPaths( c( "~/userLibrary" , .libPaths() ) )

Then I get (at least back when I wrote this originally):

> .libPaths()
[1] "/Library/Frameworks/R.framework/Versions/2.15/Resources/library"
[2] "/Users/user_name/userLibrary"  

The .libPaths function is a bit different than most other nongraphics functions. It works via side-effect. The functions Sys.getenv and Sys.setenv that report and alter the R environment variables have been split apart but .libPaths can either report or alter its target.

The information about the R startup process can be read at ?Startup help page and there is RStudio material at: https://support.rstudio.com/hc/en-us/articles/200549016-Customizing-RStudio

In your case it appears that RStudio is not respecting the Rprofile.site settings or perhaps is overriding them by reading an .Rprofile setting from one of the RStudio defaults. It should also be mentioned that the result from this operation also appends the contents of calls to .Library and .Library.site, which is further reason why an RStudio- (or any other IDE or network installed-) hosted R might exhibit different behavior.

Since Sys.getenv() returns the current system environment for the R process, you can see the library and other paths with:

Sys.getenv()[ grep("LIB|PATH", names(Sys.getenv())) ]

The two that matter for storing and accessing packages are (now different on a Linux box):

R_LIBS_SITE                          /usr/local/lib/R/site-library:/usr/lib/R/site-library:/usr/lib/R/library
R_LIBS_USER                          /home/david/R/x86_64-pc-linux-gnu-library/3.5.1/
 
Tuesday, June 1, 2021
 
rypskar
answered 7 Months ago
97

You can define your own favorite breaks function. In the example below, I show equally spaced breaks. Note that the x in the function has a range that is already expanded by the expand argument to scale_x_continuous. In this case, I scaled it back (for the multiplicative expand argument).

# loading required packages
require(ggplot2)
require(grid)
# defining the breaks function, 
# s is the scaling factor (cf. multiplicative expand)
equal_breaks <- function(n = 3, s = 0.05, ...){
  function(x){
    # rescaling
    d <- s * diff(range(x)) / (1+2*s)
    seq(min(x)+d, max(x)-d, length=n)
  }
}
# plotting command 
p <- ggplot(a, aes(x, y)) + 
  geom_point() + 
  facet_grid(~group, scales="free_x") +
  # use 3 breaks, 
  # use same s as first expand argument, 
  # second expand argument should be 0
  scale_x_continuous(breaks=equal_breaks(n=3, s=0.05), 
                     expand = c(0.05, 0)) + 
  # set the panel margin such that the 
  # axis text does not overlap 
  theme(axis.text.x = element_text(angle=45), 
        panel.margin = unit(1, 'lines'))

resulting image

Friday, July 23, 2021
 
samrap
answered 5 Months ago
92

You're 99% of the way there. Just one or two more tweaks and you'll have it. You may consider adding the following to ensure your slides are using the separate css file with something like the following added to your title slide.

Intro
======
author: Clever Name
css: css-file.css

You then can overwrite the background of the title slide by including the following between <style> </style> tags at the very top of your .Rpres file before your intro slide, or in your separate css file.

<style>
/* Your other css */

.section .reveal .state-background {
    background: url(foo1.png);
    background-position: center center;
    background-attachment: fixed;
    background-repeat: no-repeat;
    background-size: 100% 100%;
}
</style>

You're really close. Just change your title-slide { /* do stuff */ } to .section .reveal .state-background { /* do stuff */ }

Wednesday, August 4, 2021
 
THEK
answered 4 Months ago
15

Yes this can be done by a service worker but :

  • The browser can terminate a service worker if it thinks the service worker has finished working. So be sure to wrap everything inside a promise and use event.waitUntil to make sure that the process isn't terminated before it has finished.

    self.addEventListener("message", function(event) { event.waitUntil(test(event)); });

  • The service worker runs on a single thread, so if you do long synchronous operation you'd be slowing every request of your page. Service workers are not meant to do long calculation.

Yes shared workers can access the cache api. You can access it the same way you do from a service worker by using the global variable caches

caches.open(cacheName).then(function(cache) {
      cache.match("...")
});
Saturday, August 14, 2021
 
Saxophlutist
answered 4 Months ago
50

I had the same problem and found out the code you wrote works if you put in your .Renviron file:

PYTHONPATH=/home/alex/anaconda2/lib/python2.7/site-packages/

Change the path to your site-packages (or where packages are)

{python} import pandas data_py = pandas.read_csv('data.csv', sep= ';') data_py.describe(include='all') summary = data_py.describe(include='all') print( summary)

Sunday, August 29, 2021
 
Fanda
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share