I Want a single line code to run a shiny app from windows command line without opening the R application.
If I open the R from cmd and run the code it works.
But it gives error for the following single line code.
R -e 'shiny::runApp(system.file("examples/01_hello", package="shiny"), launch.browser = T)'
............
............
> 'shiny::runApp(system.file(examples/01_hello,
+
+ Error: unexpected end of input
Execution halted
Please help. Thanks..
The following code is working:
RScript -e "shiny::runApp(system.file('examples/01_hello', package='shiny'), launch.browser = T)"
I have made two changes:
Replace R by RScript
Interchange single and double quotes
You can try in cmd something like this (for example)
> "C:\Program Files\R\R-3.5.0\bin\i386\Rscript.exe" ".\app.R"
Inside app.R i have, so it is runs shiny app
shinyApp(
ui = ui,
server = server,
options = list(launch.browser = T))
You can try, in Windows command prompt, single quote doesn't work for this case.
R -e "shiny::runApp(system.file("'examples/01_hello'", package="'shiny'"), launch.browser = T)"
Related
I've been using the here package to make my projects more portable. It works great apart from when I use cronR to schedule some of my scripts. When I run my_script.R from Rstudio I get a message from library(here):
here() starts at /home/pd/projects/my_proj
When I set script.R to run using cronR I get a different message:
here() starts at /home/pd
Which is where my_schedule.cron is stored. Ideally I want to keep my_schedule.cron where it is. I can see from the logs that my_script.R runs fine apart from when it comes to saving data because the path used by here() is incorrect. Is there anyway to get the here function to detect the project dir when my_script.R is run from cronR or the terminal?
You can modify the command cmd that is usually created with cron_rscript() by adding cd to your project folder followed by the usual part:
cmd <- "cd /home/pd/projects/my_proj && /usr/lib/R/bin/Rscript ./my_script.R >> ./my_script.log 2>&1"
cron_add(command = cmd, frequency = 'daily', at = '18:00')
If the first line of your #rstats script is wd <- here(), I will come
into your lab and SET YOUR COMPUTER ON FIRE.
Learn how to use environment variables
wd <- Sys.getenv("HOME")
wd <- file.path(wd, "projects", "my_proj")
Or use the 'Additional arguments to Rscript' element in the cronR user interface to pass something extra to the Rscript and fetch it with commandArgs().
If you don't use the cronR interface but cron_rscript, use cronR::cron_rscript(..., rscript_args = "/home/pd/projects/my_proj")
args <- commandArgs(trailingOnly = TRUE)
if(length(args) > 0){
wd <- args[1]
}
I'm new to R and I'm stuck in a very simple task.
I want to run an R script from console, but I want the script to be able to read user inputs.
This is how I'm reading from the script:
library = readLines(n = 1L)
if(library == "1")
{
library = "GUDHI"
}
And this is how I'm running my script from R-Portable with a .bat:
#echo on
cd..
cd..
cd..
cd..
cd..
PATH C:\Users\MyUser\Desktop\App\RFolder\R-Portable\App\R-Portable\bin;%path%
cd C:\Users\MyUser\Desktop\App\RFolder
Rscript Phom.R 1
pause
When I run this .bat it throws an error (Argument is of length zero):
As if the console didn't wait for user input.
The problem is not the .bat code. If I remove the readLines functions from my script and hardcode the input, it works perfectly. I also tried the readline function with no success.
Thanks.
Solution for Interactive R script from Windows CMD:
cat("Prompt Message: ")
library = readLines(con = "stdin", 1)
I'm not sure if the prompt MUST end with ": ", but I had trouble when I removed that piece of string.
This worked for me, I hope this helps somebody.
I'm trying to run some demo R code for the optparse package that I got from R-bloggers. I am using ubuntu 14.04
The code is:
#!/usr/bin/env Rscript
library(optparse)
option_list = list( make_option(c("-f", "--file"),
type="character", default=NULL,
help="dataset file name",
metavar="character"),
make_option(c("-o", "--out"),
type="character", default="out.txt",
help="output file name [default=
%default]", metavar="character")
);
opt_parser = OptionParser(option_list=option_list);
opt = parse_args(opt_parser);
if (is.null(opt$file)){
print_help(opt_parser)
stop("At least one argument must be supplied (input file).n",
call.=FALSE)
}
## program...
df = read.table(opt$file, header=TRUE)
num_vars = which(sapply(df, class)=="numeric")
df_out = df[ ,num_vars]
write.table(df_out, file=opt$out, row.names=FALSE)
If the entire script is saved in a file called yasrs.R using the call:
Rscript --vanilla yasrs.R
should return the help messages.
I get an error:
Rscript --vanilla yasrs.R Error in library(optparse) : there is no package called ‘optparse’
I have installed the package (optparse) through RStudio when writing the code and also ensured that it is installed when calling from the terminal. Both terminal and RStudio are running the same R version.
Any suggestions would be appreciated.
Where did RStudio install optparse? Get that from packageDescription("optparse").
Then check the output of .libPaths() in your Rscript environment and your RStudio environment. Maybe RStudio stuck it somewhere that RScript doesn't look.
Then check that even though they may be the same version of R, they might be two different installations. What does R.home() say in each?
One or more of these things will show you why it doesn't find it. Solution is probably to write and run a little RScript that installs it, then you should be fairly sure its going to go in a location that RScript will find it in future.
I am trying to run exiftool through R to get metadata from photos using the system() command. When I run this on a mac, it works fine, but from windows I am not linking up with the cmd.exe properly and getting the following error from this code:
exif_datetime <- function(path) {
exif_cmd <- 'exiftool.pl -T -r -DateTimeOriginal '
cmd <- paste(exif_cmd, "'", path, "'" ,sep='')
exif_time <- system(cmd, intern = TRUE)
exif_time
}
photo_time <- exif_datetime('C:/Users/photo.jpg')
photo_time
Error in system(cmd, intern = TRUE) :
'CreateProcess' failed to run 'C:\Windows\exiftool.pl -T -r -DateTimeOriginal 'C:/Users/photo.jpg''
When I run the exiftool command from cmd.exe in Windows, I get the proper result. My exiftool.pl is in the C:Windows folder on my computer. Is there something regarding the PATH that I am missing? Also, I remember something about windows needing a shell, but I have not figured out if that is what I need in my case nor how to create one properly.
Thanks for all your suggestions. I found a solution that works for me, involving the shell() command. I thought that it had to be incorporated with the system() command somehow, but it appears that it will work on its own.
exif_datetime <- function(path) {
exif_call <- 'exiftool.pl'
exif_cmd<-' -r -T -DateTimeOriginal '
exif_timestamp <- shell(paste(exif_call, exif_cmd, path), intern=T)
exif_timestamp
}
photo_time <- exif_datetime('C:/Users/photo.jpg')
photo_time
[1] "2016:02:14 11:50:29"
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")
If I understand your question correctly, I was able to achieve this with littler, which I use in lieu of Rscript for scripting tasks that revolve around R. I'm running CentOS 7, and based on the code in your question it looks like you are on a Unix-like machine, so installing littler should not be an issue. For minimal reproducibility, I used the default shiny application and shiny-based Rmarkdown templates provided by RStudio, saving them as testapp (the project / application directory name) and testRMD.rmd, respectively. Then, I have the following scripts:
testapp.r
#!/usr/bin/env r
shiny::runApp(
"~/tmp/delete/testapp",
port = 7088,
launch.browser = TRUE,
host = "127.0.0.1")
testRMD.r
#!/usr/bin/env r
rmarkdown::run(
file = "testRMD.rmd",
dir = "~/tmp/delete",
shiny_args = list(
port = 7088,
launch.browser = TRUE,
host = "127.0.0.1"))
Set the permissions for these files so they can be executed -
[nathan#nrussell R]$ chmod +x testapp.r testRMD.r
(chmod +u ... should suffice, but regardless...), and you should be all set to run them from your terminal, etc...
[nathan#nrussell R]$ ./testapp.r
Loading required package: shiny
Listening on http://127.0.0.1:7088
[nathan#nrussell R]$ ./testRMD.r
Loading required package: shiny
Listening on http://127.0.0.1:7088
There is some additional command line output for the Rmd file that I omitted, but I'm sure this could be suppressed easily if desired. Anyhow, this seems to be working properly - both the shiny application and Rmarkdown application are interactive, just as when launched from RStudio - but if you had something else in mind please clarify.
Thanks #nrussell, your example helped me a lot!
Here is my solution for launching an interactive markdown doc on Windows 10.
REM Change to correct directory
cd "C:\Users\me\Documents\project_folder\"
REM Print files available (not required, but helpful)
dir
REM Point system to R's Pandoc with Rscript then launch
"C:\Program Files\R\R-4.0.3\bin\Rscript.exe" -e ^
"Sys.setenv(RSTUDIO_PANDOC='C:/Users/me/Documents/RStudio/bin/pandoc'); rmarkdown::run(file = 'myInteractiveMarkdown.Rmd', shiny_args = list(launch.browser = TRUE, host = '127.0.0.1'))"
I found I got two errors initially:
When I didn't point the system env to R's pandoc it gave the error message error pandoc version 1.12.3 or higher is required which I solved using the instructions here
When I set the port in the shiny_args, subsequent executions of the bat file would get an error that the port was already busy