[R] how to find the table in R studio

Duncan Murdoch murdoch@dunc@n @end|ng |rom gm@||@com
Wed Jan 12 21:22:00 CET 2022


On 12/01/2022 3:07 p.m., Kai Yang via R-help wrote:
> Hi all,
> I created a function in R. It will be generate a table "temp". I can view it in R studio, but I cannot find it on the top right window in R studio. Can someone tell me how to find it in there? Same thing for f_table.
> Thank you,
> Kai
> library(tidyverse)
> 
> f1 <- function(indata , subgrp1){
>    subgrp1 <- enquo(subgrp1)
>    indata0 <- indata
>    temp    <- indata0 %>% select(!!subgrp1) %>% arrange(!!subgrp1) %>%
>      group_by(!!subgrp1) %>%
>      mutate(numbering =row_number(), max=max(numbering))
>    view(temp)
>    f_table <- table(temp$Species)
>    view(f_table)
> }
> 
> f1(iris, Species)
> 

Someone is sure to point out that this isn't an RStudio support list, 
but your issue is with R, not with RStudio.  You created the table in 
f1, but you never returned it.  The variable f_table is local to the 
function.  You'd need the following code to do what you want:

f1 <- function(indata , subgrp1){
   subgrp1 <- enquo(subgrp1)
   indata0 <- indata
   temp    <- indata0 %>% select(!!subgrp1) %>% arrange(!!subgrp1) %>%
     group_by(!!subgrp1) %>%
     mutate(numbering =row_number(), max=max(numbering))
   view(temp)
   f_table <- table(temp$Species)
   view(f_table)
   f_table
}

f_table <- f1(iris, Species)

It's not so easy to also make temp available.  You can do it with 
assign(), but I think you'd be better off splitting f1 into two 
functions, one to create temp, and one to create f_table.

Duncan Murdoch



More information about the R-help mailing list