ENH: Have the ability to run a script

Open
#138 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
25/100
Issue type
Feature
Clarity
Needs clarification
Activity status
Stale
Tech stack
r
Domain
api

Research direction

Begin with related rstudioapi issue #112 and the terminalSend, terminalCreate, terminalActivate, and documentNew calls shown here. Clarify whether automated execution must keep plots and output in RStudio's Console or Viewer rather than external terminal devices, then define an acceptance test for that behavior.

Written by the indexing model from the issue text.

Description

Description of the problem

@kasperdanielhansen

May be related in some small way to https://github.com/rstudio/rstudioapi/issues/112.

The goal is to be able to have a set of R commands that you either a) create with the script or b) already have, and have that execute in order to show how the output would look in RStudio. The goal is to create a way to essentially run the commands as you would live, but in an automated way

Current attempt

One of the issues if you're running commands from a source editor to insert text in another editor, you have to be careful about which editor ID you're using. Moreover, using this solution is not appealing as you have the script command to generate the text inside the editor showing up in the console.

The idea then was to try this with the Terminal:

library(rstudioapi)
execution = function(code, 
                     location = Inf, 
                     term_id = NULL,
                     id = NULL, time = 1,
                     add_newline = TRUE) {
    if (add_newline) {
        code = paste0(code, "\n")
    }
    insertText(
        location,
        text = code, 
        id = id)
    Sys.sleep(time)
    terminalActivate()
    rstudioapi::terminalSend(text = code, id = term_id)
    setCursorPosition(
        position = Inf,
        id = id)
    Sys.sleep(time)
}
# create new document
documentNew("# we will write here\n", type = "r",
            position = document_position(0, 0))
# wait for it to open
Sys.sleep(1)
# get the id
document = getSourceEditorContext()
id = document$id
setCursorPosition(
    position = Inf,
    id = id)
Sys.sleep(1)

# open up a terminal
term_id = terminalCreate()
terminalActivate()
terminalClear(term_id)
# Run R
terminalSend(id = term_id, "R\n")
terminalActivate()

# execute the commands in Terminal while pasting in editor
execution("# Hello", id = id, term_id = term_id)
execution("x = 5", id = id, term_id = term_id)
execution("y = 4", id = id, term_id = term_id)
execution("x + y", id = id, term_id = term_id)

# wait until execution reasonably done
Sys.sleep(10)
# kill R
terminalSend(id = term_id, "q('no')\n")
# kill the terminal
sapply(terminalList(), terminalKill)

Issues with Current Attempt

The issue is that any plots and output is not really in the Console/RStudio Viewer but external image panes/devices (such as Quartz devs) as it's done from the terminal. If you try it in reverse, it doesn't work as the Terminal R doesn't inherit the RStudio functionality.

Would this be possible to do via webshot and an RStudio server session maybe? @wch

Dominant language
R
Stars
175
Forks
37
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from rstudio/rstudioapi

All issues in rstudio/rstudioapi

Similar issues

More R issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.