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
Definitely avoid the assign solution... remembering side effects on the global environment is hard and difficult to troubleshoot, and that also makes "doing that many times" unreasonably difficult. IMO even mentioning such an alternative is a dangerous distraction. I also would advocate not mixing the act of obtaining a result with the act of displaying it (e.g. the view function). A three line script (input, analysis, output) is always easier to adapt to answer new questions than a one-line script is, and is arguably even more understandable. On January 12, 2022 12:22:00 PM PST, Duncan Murdoch <murdoch.duncan at gmail.com> wrote:>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 > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code.-- Sent from my phone. Please excuse my brevity.
Respectfully, this forum gets lots of questions that include non-base R components and especially packages in the tidyverse. Like it or not, the extended R language is far more useful and interesting for many people and especially those who do not wish to constantly reinvent the wheel. And repeatedly, we get people reminding (and sometimes chiding) others for daring to post questions or supply answers on what they see as a pure R list. They have a point. Yes, there are other places (many not being mailing lists like this one) where we can direct the questions but why can't there be an official mailing list alongside this one specifically focused on helping or just discussing R issues related partially to the use of packages. I don't mean for people making a package to share, just users who may be searching for an appropriate package or using a common package, especially the ones in the tidyverse that are NOT GOING AWAY just because some purists ... I prefer a diverse set of ways to do things and base R is NOT enough for me, nor frankly is R with all packages included as I find other languages suit my needs at times for doing various things. If this group is for purists, fine. Can we have another for the rest of us? Live and let live. -----Original Message----- From: Duncan Murdoch <murdoch.duncan at gmail.com> To: Kai Yang <yangkai9999 at yahoo.com>; R-help Mailing List <r-help at r-project.org> Sent: Wed, Jan 12, 2022 3:22 pm Subject: Re: [R] how to find the table in R studio 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 ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]]