elag
November 6, 2019, 10:10am
1
Relatively new to R as it's in our uni course so I'm just getting started, but I've noticed that our tutor's version displays the values of named objects when run and I wish that could be the case for me too because it makes it easier to quickly view the output instead of having to look through all objects.
an example of what I have to do to get objects to show up in console when running from a script:
t.stat<-diffy1y2/SE
t.stat
[1] 4.341755
using version 1.2.5001 on mac
If your goal is to avoid typing the second line of code
t.stat<-1.96
t.stat
or
t.stat <- 1.96
print(t.stat)
you can enclose your first statement in parentheses, as in
(t.stat<-1.96)
and it will print the result
1 Like
elag
November 6, 2019, 11:13am
3
This works, thanks! Though I thought there was a setting I could enable that did this automatically without writing the parentheses but this works as a temporary thing.
As a general rule, if you assign something to an object, the default is not to print.
If you do not finish a pipe with an assignment, it will print.
For example
library(tidyverse)
iris %>%
select(starts_with("Sepal")) %>%
head()
#> Sepal.Length Sepal.Width
#> 1 5.1 3.5
#> 2 4.9 3.0
#> 3 4.7 3.2
#> 4 4.6 3.1
#> 5 5.0 3.6
#> 6 5.4 3.9
Created on 2019-11-06 by the reprex package (v0.3.0)
will print the resulting dataset
But this version
library(tidyverse)
iris %>%
select(starts_with("Sepal")) %>%
head() ->
sepals
Created on 2019-11-06 by the reprex package (v0.3.0)
will not.
But this (with parentheses) will print
library(tidyverse)
(iris %>%
select(starts_with("Sepal")) %>%
head() ->
sepals)
#> Sepal.Length Sepal.Width
#> 1 5.1 3.5
#> 2 4.9 3.0
#> 3 4.7 3.2
#> 4 4.6 3.1
#> 5 5.0 3.6
#> 6 5.4 3.9
Created on 2019-11-06 by the reprex package (v0.3.0)
2 Likes
nwerth
November 6, 2019, 2:35pm
5
Just for impish fun that nobody should attempt in anything important:
# Not a good idea!
`<-` <- function(x, value) {
assign("x", substitute(x))
if (is.symbol(x)) {
assign("x", deparse(x))
}
assign("env", parent.frame())
assign(x, value, envir = env)
print(value)
invisible(value)
}
a <- 2
# [1] 2
a
# [1] 2
b <- 1 + 2 + 3
# [1] 6
b
# [1] 6
system
Closed
November 27, 2019, 2:45pm
6
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.