Sourcing script does not print any output to the console - r

I am using Ubuntu inside Windows XP using VirtualBox, and I installed R properly. I am using a library called psych for this. The following is my code:
impact <- read.table("stats1.ex.02.txt", header=T)
class(impact)
describe(impact)
I am taking Statistics One course on coursera.org and the Prof gives this txt file to work with. When I run this in R, using source("test.R") (where test.R is the filename I gave),
nothing happens. What could be problem here ?

Try using source("test.R", echo=TRUE) or adding print to whatever you want printed after sourcing your script. Thus, your script might be:
impact<- read.table("stats1.ex.02.txt",header=T)
print(class(impact))
print(describe(impact))

Related

R script is not loading (kohonen library) and it is not executing in php code and same code is executing in terminal

I am running R script in the php code in apache webserver, in my script I am giving code for loading (kohonen library). whole R-script will be running through php coding as exec("Rscript script.R");
R code:
library("kohonen")
load(file = "som.rda")
tr<-read.csv("trainnew1.csv")
compile11<-tr
write.table(compile11, file = "kohonen1.txt")
and etc..........
in terminal this code is running, but while giving in php it is not executing and loading the library
for checking the library loading, i wrote simple code for addition by loading library in first line
example
library("kohonen")
a<-3
b<-5
c<-a+b
print(c)
it is not printing c in output file, because of kohonen library
same addition i tried with loading nnet library
example
library("nnet")
a<-3
b<-5
c<-a+b
print(c)
It is printing c in the output file. problem is with loading kohonen library
please suggest me, how to load kohonen libary and then how to fix this problem
I am sorry, I might not have the answer you need. I just want to emphasize another thing. Please never naming a variable with c. It is an R reserved word. You can change to cc or something else. Perhaps this is also the cause of your R session not working.

Don't print the name of the directory to stdout when using here::set_here('path')

I'm using the R package 'here' to define my working directory using the following command at the start of my script:
here::set_here(path='/path/to/my_directory/', verbose = F)
Every time I run the script it prints this to the console:
here() starts at /path/to/my_directory
Is there a way to suppress this output? I tried using the invisible() function but that didn't work...
The message you’re seeing is printed when you’re attaching the ‹here› package. Simply don’t do that (it’s unnecessary anyway) to prevent it.
Otherwise, load it as follows:
suppressPackageStartupMessages(library(here))
… yeah, not exactly elegant.

Running R via terminal in Windows and leaving R session open

Suppose that I have a R script called test.R, stored at C:\, with the following content:
x <- "Hello Stackoverflowers"
print(x)
To run it via terminal one could simply call:
Rscript C:\test.R
And as expected, the result will be:
However, what I wonder is whether there is a way to run test.R via Windows console but after that staying within the executed R session instead of closing and going back to the console cursor? That is, staying inside the R session instead of going back, in the image above, to C:\R\R-3.4.1\bin>.
For instance, when compiling Python code with python.exe I can easily accomplish a similar thing by passing the -i parameter to the python.execall.
How could I do that with R?
Add this to your .Rprofile:
STARTUP_FILE <- Sys.getenv("STARTUP_FILE")
if (file.exsts(STARTUP_FILE)) source(STARTUP_FILE)
and then set the indicated environment variable outside of R and then run R. e.g. from the Windows cmd line:
set STARTUP_FILE=C:\test.R
R
... R session ...
q()
Variations
There are many variations of this. For example, we could make a copy of the .Rprofile file in a specific directory such as ~/test, say, and add this code to that copy
source("~/test/test.R")
in which case R would only run test.R if R were started in that directory.

Alter file.show() behaviour

I'm running file.show() in RStudio on a Mac, like this:
filename <- "/Users/me/reports/myfile.pdf" # replace with file location/name
file.show(file.path(filename), title="My File")
This does two things:
Opens my file.pdf
Opens a blank file in RStudio called "My File".
options()$pdfviewer
returns:
"/usr/bin/open"
How can I stop (2) from happening?
If you want to simply open a PDF file via RStudio on a Mac, the simplest way (as far as I know) is to use the system function:
filename <- "/Users/me/reports/myfile.pdf"
pdf <- getOption("pdfviewer") # same as options()$pdfviewer
cmd <- paste(pdf,filename)
system(cmd)
For example it is used by openPDF in Biobase (see here)
I don't know a way to alter the file.show behaviour in order to prevent a blank file being opened in RStudio. I think it has something to do with The R.app Mac OS X GUI uses its internal pager irrespective of the setting of pager. that you can see on the manual here.
Note: it is different on a Windows machine, use shell.exec(filename) directly.

Is there any way in Linux to show what's going on about the freezing code on the R session?

I am running a set of selected code on R. Like
source("/tmp/r-plugin-honli/Rsource-2704-quantmod.R")
There is no output. Only the prompt '>' flickered there.
I use 'killall' to kill the R session. But I don't know where is wrong on the code. Because R did not give any output. How could I know what's going on about the code.
I'd try two things:
Run the code interactively. As in, open the Rsource-2704 file and run its lines one by one.
If that doesn't replicate the problem or is not possible, you can take Joshua Ulrich's suggestion or use:
R CMD BATCH --vanilla Rsource-2704-quantmod.R out.log
Which will run the code in a batch mode and output the usual console lines to a file called out.log (you can name it whatever you like).
Instead of using print statements, you could also take a look at the browser() command. This drops you into an interactive session at the point where the command is put. This works particularly well when trying to figure out what is happening inside a function, although I don't know if your script contains them.

Resources