rscript

How do I test if R is running as Rscript?

。_饼干妹妹 提交于 2019-12-30 08:11:47
问题 I have code in a single R file that I want to be able to source (i.e., to define my functions etc.) within RStudio during development, and also run using the #! /usr/bin/env Rscript syntax via the command line (actually, using Hadoop). For the latter, I need the last thing that Rscript does to be to kick off the analysis (i.e., using a call to a main() function). For the former, I don't want my main() function called. I'd like to be able to test if the code is running within Rscript (or,

How to run an R script and show a plot?

99封情书 提交于 2019-12-24 19:04:53
问题 How to wait for a keypress in R? I am attempting to construct a simple script, which creates a graph; closes it when a key is pressed. My code: #!/usr/bin/Rscript library(tidyverse) ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy), colour = "blue") I have tried the answers from the linked question to pause the script after the plot is drawn. However: No plot appears, alto the code works fine from the interactive R shell. readline() works only in interactive mode, as per the

How to speed up the package/library loading time in R?

◇◆丶佛笑我妖孽 提交于 2019-12-24 06:34:41
问题 I have a run_stat.r script that can run in command line such as Rscript anova.r on my ubuntu server. And the file content is like: #load package library(ez) #run code.... The problem is that the loading time of ez package would take 2~3 seconds... Can R preload the package one time, and then doesn't need to re-load the same package every time I run the script ? Or do any suggestions of speed up the loading time of the package? Many Thanks!!! 来源: https://stackoverflow.com/questions/42933479

Writing an RDA to CSV in R

一世执手 提交于 2019-12-24 05:31:15
问题 I'm trying to write a script to load an RDA by filename and write an equivalent file out as CSV. It's almost there (with the loading and writing) however the output CSV contains the vector of strings returned by load(), rather than the data frame referenced by load... $ cat convert.r #!/usr/bin/env Rscript argv <- commandArgs(TRUE) inFile <- toString(argv[1]) print(inFile) outFile <- gsub(".rda$", ".csv", inFile) print(outFile) inData <- load(inFile) write.csv(inData, file=outFile) This is

R Script and Library preloading?

痴心易碎 提交于 2019-12-23 23:02:10
问题 I have created an R script that it needs to load some libraries first. The problem is that the script needs 1.6 seconds to finish its calculations (measured it many times with Linux command "time") and 0.7 seconds only takes to load the libraries! The script runs quite often, so the delay for library loading accounts to almost 80% of the real workload! Is there any way to have the libraries preloaded so that they won't be loaded each time the script runs? Any other suggestion to bypass this

WScript.Shell to run a script with spaces in path and arguments from VBA

社会主义新天地 提交于 2019-12-22 13:57:28
问题 I need to call a script (R) from VBA using the WScript.Shell. The path to the file contains spaces. Additionally, a series of arguments is passed to the script, of which several also contain spaces. I have tried all thinkable combinations of quotes and double quotes around paths, arguments or even the whole string (notably this here). Nothing seems to work when arguments need to be passed (I either get "C:\Program" not recognized"- or "invalid-syntax"-errors in the command line). Option

Error installing ggplot2

烂漫一生 提交于 2019-12-21 20:33:48
问题 I was trying to run a simple code on ggplot2 and got the following error: source("u1.txt") Error in library(ggplot2) : there is no package called ‘ggplot2’ Error : package ‘foreign’ was built before R 3.0.0: please re-install it ERROR: lazy loading failed for package ‘maptools’ * removing ‘/home/shivangi/R/i686-pc-linux-gnu-library/3.0/maptools’ * installing *source* package ‘multcomp’ ... ** package ‘multcomp’ successfully unpacked and MD5 sums checked ** R ** data *** moving datasets to

How to pass Rscript -e a multiline string?

戏子无情 提交于 2019-12-21 07:00:08
问题 Is there a way to provide the code to Rscript -e in multiple lines? This is possible in vanilla R R --vanilla <<code a <- "hello\n" cat(a) code But using Rscript I get two different things depending on the R version. # R 3.0.2 gives two ignores Rscript -e ' quote> a <- 3+3 quote> cat(a, "\n") quote> ' # ARGUMENT 'cat(a,~+~"' __ignored__ # ARGUMENT '")' __ignored__ Rscript -e 'a <- 3+3;cat(a, "\n")' # ARGUMENT '")' __ignored__ # R 2.15.3 gives an ignore for the multiline, but it works with

Run R interactively from Rscript

浪子不回头ぞ 提交于 2019-12-19 17:31:10
问题 I'm trying to start a shiny app or an interactive .Rmd document from an Rscript. However, all I get is a message Listening on http://127.0.0.1:... I believe this is because R is running in interactive mode (another post about this). How can I write the proper Rscript so that either of the following would work? My script #!/usr/bin/Rscript ## This library(shiny) runApp(appDir = "../app") ## Or this ## rmarkdown::run("Main.Rmd") 回答1: If I understand your question correctly, I was able to

Including Command Line Scripts with an R Package

拈花ヽ惹草 提交于 2019-12-19 05:50:45
问题 I am interested in providing a command line interface to an R package called Slidify that I am authoring. It uses Rscript and I think that would make it cross-platform. The scripts are stored in the subdirectory inst/slidify . In order to use the script from any directory, I added its path to my .bash_profile as I am on a Mac. My question is How should I handle installation of the script in an automated cross-platform way? How can I make sure that the file permissions are retained in this