0

For example, if I run the following lines in R terminal under Ubuntu:

library(quadprog)
Dmat       <- matrix(0,3,3)
diag(Dmat) <- 1
dvec       <- c(0,5,0)
Amat       <- matrix(c(-4,-3,0,2,1,0,0,-2,1),3,3)
bvec       <- c(-8,2,0)
solve.QP(Dmat,dvec,Amat,bvec=bvec)

I will see these output:

$solution
[1] 0.4761905 1.0476190 2.0952381

$value
[1] -2.380952

$unconstrained.solution
[1] 0 5 0

$iterations
[1] 3 0

$Lagrangian
[1] 0.0000000 0.2380952 2.0952381

$iact
[1] 3 2

How to access these return values, like $solution inside the terminal? This question assumes not storing solve.QP()'s output to variable.

visitor99999
  • 163
  • 1
  • 10
  • 2
    Try for example `solve.QP(Dmat,dvec,Amat,bvec=bvec)$solution`? – Martin Gal Jun 05 '20 at 23:34
  • yeah, you can do that. But the question is how to get $solution from the terminal after the function completes? – visitor99999 Jun 05 '20 at 23:37
  • 4
    If that's the last line of code you ran, `.Last.value$solution` should work. Otherwise I think you'd have to assign it to a variable if you want to access its contents. – IceCreamToucan Jun 05 '20 at 23:40
  • ok, .Last.value is the closest answer, i think, unless someone else knows there is a stack of internal variables, like in Python's terminal – visitor99999 Jun 05 '20 at 23:45
  • Python has a stack of internal variables? I thought `_` worked just on the last value (just like this). In `ipython` (and therefore jupyter, I believe) you can use `_1`, `_2`, etc. (My ref: https://stackoverflow.com/q/200020/3358272) – r2evans Jun 05 '20 at 23:47
  • I used [1], [2] etc for internal answer. I think they are the same as _1, _2, etc – visitor99999 Jun 05 '20 at 23:51
  • Can we get some clarification on terminology? Are we talking about an `R CMD ...` or `Rscript file.R` call from a Linux terminal session or just some code invoked in an R console session???? What is the use case? Trying to avoid an X/Y sort of discussion. – IRTFM Jun 06 '20 at 00:51
  • @IRTFM, your comment is the first reference of either of those commands. I thought it was safe to assume *interactive*, which precludes both of those. Your question about use-case is certainly relevant, though. – r2evans Jun 06 '20 at 03:37
  • My impression was based on the use of the term: “terminal” which usually refers to a command line interface. – IRTFM Jun 06 '20 at 06:42
  • @IRTFM, didn't think the question would have these implications :-). The original question was just R console output. I happened to use a Linux terminal to start the R console. – visitor99999 Jun 07 '20 at 01:22

0 Answers0