Click on a URL from an R string output - r

Suppose I have an output from the cat function of R which is a URL. For example:
cat("https://en.wikipedia.org/wiki/Statistics")
# Output: https://en.wikipedia.org/wiki/Statistics
Is there any command on the cat function or any other thing so that the output https://en.wikipedia.org/wiki/Statistics becomes a clickable URL in the R console?

As you mentioned in the comments you are using RStudio. It is not specified why it has to be the console in R, but I assume there is a good reason to display the links within RStudio and I assume the viewer pane on the right next to the console also works for you.
If that is the case you could do the following:
library(DT) # for datatable() function
library(shiny) # for tags$a() function
data <- data.frame(link = toString(tags$a(href = paste0("http://google.de"), "google")))
datatable(data, escape = FALSE)
Very close to the console ;)

It would depend on how the output is being viewed. If this output is going to an HTML file, then all you need to do is set this as the location for a hyperlink.
https://en.wikipedia.org/wiki/Statistics
But if, for example, you are just viewing the output in notepad, then I don't believe notepad has the functionality for hyperlinks.
I don't believe the R console directly supports hyperlinks or not. But it looks like from this: http://rmarkdown.rstudio.com/lesson-2.html that you can maybe use R Markdown to do what you want.

This is not currently possible in RStudio, unfortunately. However, it is an open issue which you can upvote if you think it should be prioritised.

Related

R Markdown script and R

I am calling one R Markdown script from another R script.Below you can see command
rmarkdown::render((file=paste(path1,"/Dashboard.Rmd",sep="")),params=list(args = myarg))
The script is executed without any problem but is not open automatically.
So can anybody help me how to solve this problem and open this script automatically after running of this command ?
First, your syntax probably isn't doing what you intended. Writing
rmarkdown::render((file=paste(path1,"/Dashboard.Rmd",sep="")),params=list(args = myarg))
will create a new variable named file and use it as the first parameter to rmarkdown::render. A more likely way to get what you want is to write it as
outfile <- rmarkdown::render(paste(path1,"/Dashboard.Rmd",sep=""),
params=list(args = myarg))
This removes the assignment from the first argument, and saves the
result (which is the name of the file that was produced).
Now, on to your question: You need to follow that line with
rstudioapi::viewer(outfile)
to view it in RStudio, or
browseURL(outfile)
elsewhere, because rmarkdown::render doesn't automatically call a previewer.

Catch errors from rmarkdown::render

R newbie question: I am generating PDFs using rmarkdown from the console (not using rstudio). I have written a simple r script to render the rmarkdown file from the console, it basically looks like this:
# my_r_script.R
rmarkdown::render('mydoc.Rmd', output_file = opt$out,
params = list(
something = opt$something,
else = opt$else
)
)
In the rmd file bad errors might happen, e.g. some calculations might crash due to the given parameters. Question How can I access those errors in the R script? Or in other words: I want to know if something went wrong in the RMD file how could I achieve that?
You can wrap the rmarkdown::render statement in a try catch function - there is a great example here.
You should be able to store the output in a variable for further debugging.

Save the output of an r script including its commands

I want to save a part of my r script output including the commands into a text file. I know sink() but it does not include the commands or I could not find a specific option to do that.
Is there any possibility to capture the commands and its ouput within an r session. Simply write an Rmd or capture the output within the console is not the solution at the moment.
You are probably looking for the TeachingDemos package. Documentation can be found here.
Example:
library(TeachingDemos)
txtStart("test.txt")
# Your code
txtStop()
This should write both your command input and output to a file called test.txt.
If you're working interactively, here's one idea. It was this specific problem for which I created the sinkstart() function in the rite package. Basically, this creates a pop-up tcl/tk widget that you can write commands and output to. Here's a screenshot to give you a feel:
There are just two relevant functions: sinkstart() starts the sink; sinkstop() turns it off. You can toggle back and forth to selectively write to the widget. Then you can just save the contents with a right-click or a key shortcut.

Displaying png files from R into spotfire

I want to pass data from Spotfire to R and then display the plot constructed by R.
What is the best way to do this?
I’ve figured out the trick of putting images into Spotfire. It’s not hard if you follow these directions, but it’s done in a way very different from how you guess you would do it in Spotfire, and that’s why it took me awhile to figure out.
Here’s an overview of how to do it. You create a DocumentProperty which is a binary object, you write some Spotfire code that gives a value to that Document Property, and you display that binary object using a Spotfire Property Control of the “Label” type.
The confusing parts are that you DON’T use the Spotfire “Insert Image” tool at all, and that you DON’T use the filename generated inside the R code in Spotfire at all. Once you get used to the idea that the two most obvious ways you think you would approach the problem in Spotfire are entirely useless and wrong, you can make some progress.
I’ll leave out the spiderplot specifics because the code’s pretty long.
Here’s what you do.
1) Create a document Property in Spotfire of type “Binary”, e.g., “imageThatGoesBackToSpotfire”
2) You write some R code that generates an image and writes it to a file:
# get a temporary directory name on the local machine. You wouldn’t need to do this is you were just
# going to run it on your own, but you need to do it if you intend to let anybody else run it on their own machine.
tempfilebase = tempfile()
# take the tempfilebase and prepend it to a filename.
myFilename<-“someFileName.jpg”
myFullFilename <- paste(tempfilebase,myFilename,sep="")
#open a jpeg
jpeg(filename=myFullFileName)
# generate the image, however you normally would in R
plot(input)
# close the file
dev.off
# open a connection to that file.
myConnection<-file(myFullFileName,open=”rb”)
imageThatGoesBackToSpotfire<- data.frame(r=readBin(myConnection, what="raw", n=(file.info(myFullFileName)$size)))
close(myConnection)
3) Run your R script, above. Select some columns that are the “input” to the plot, and make the R script return outputs to the “imageThatGoesBackToSpotfire” DocumentProperties.
4) Create a text area in Spotfire.
5) Insert a Property Control into the text area of type “label”. (Click on the icon that’s circled in the picture below). This opens a dialog,
You need to register a data function with inputs and outputs, and the specific PNG data needs to be returned as a binary label.
Some details: http://spotfire.tibco.com/tips/2014/02/25/dynamically-displaying-images-in-a-text-area/

in R , how to get the output to a file as well as on console at the same time?

I want only the output not the commands, and I want to see the output on the console at the same time.
I tried sink and capture.output , but tried to work through the examples but I can accomplish only one of the task i.e. either to console or to a file.
I am new to R, and was thinking whether there is a function that can help me see the output on the console and save it to a text file as well?
Do sink(file="file.txt", split=TRUE).

Resources