Switch R script from non-interactive to interactive
Can you just fire up R and source the file instead?
R
source("script.R")
Run R interactively from Rscript
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.
Switching a conditional based on whether R session is interactive or not
This?
if(base::interactive()){
a <- 10
b <- 6
}else{
args = commandArgs(trailingOnly = TRUE)
a <- args[1]
b <- args[2]
}
How can I execute R one-liners from the terminal without exiting R?
With a function you can restart R automatically after using the parameters:
r() {
# First execute commands when given
if [ $# -gt 0 ]; then
R --slave -e "$*"
fi
# Start R
R --slave
}
After testing you can add this function to .bashrc
.
I call the function r
, but make sure r
is not already used or reserved by your system.
You can call the function with r 1+1
, r 1 + 1
of r "1*2"
.
Please note the quotes when you use "1*2"
.
Without quotes the shell will try to expand special characters, and will look for a file "1*2". It would be a lot worse with spaces, r 1 * 2
will replace *
with a lot of filenames.
Related Topics
Why Is Stat = "Identity" Necessary in Geom_Bar in Ggplot
Texture in Barplot for 7 Bars in R
Problems Using Foreach Parallelization
Note in R Cran Check: No Repository Set, So Cyclic Dependency Check Skipped
How to Create a Raster from a Data Frame in R
How to Properly Document S4 "[" and "[<-" Methods Using Roxygen
Colorize Clusters in Dendogram with Ggplot2
Plotting Multiple Curves Same Graph and Same Scale
Calculating the Difference Between Consecutive Rows by Group Using Dplyr
How to Plot Logit and Probit in Ggplot2
How to Adjust Facet Size Manually
Show That Shiny Is Busy (Or Loading) When Changing Tab Panels