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) [[alternative HTML version deleted]]
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
If you intend to use R for work beyond simple scripts and plots, you will have to understand how to use functions in R: the principle that Duncan mentioned -- that objects such as f-table that are local to a function are not available outside the function (with caveats -- see below) is __absolutely fundamental__ to function use in R. You can search for a suitable R function tutorial on the web of course -- there are tons. As you learn about how to use functions in R, you will encounter the concepts of __scoping__ in R and R __environments__. Unfortunately, this does get a bit technical, but it is very useful to understand at least the basics for working with functions; and it explains the below example, which is the 'caveat' I referred to. Feel free to ignore, of course, if you think this is too much at this point. ********* R function scoping/environment example ******** ## create a function that returns a function as its output outer_fun<- function(x){ inner_fun <- function(y)x*y ## the function definition, a one-liner ## the function returns x*y with input y inner_fun ## the last object in a function is automatically returned ## so here a function is returned } ## let's see> infun10 <- outer_fun(10) ## x = 10## note that the function returned by outer_fun(10) was assigned to infun10> x ## but x is not present in the global environment/command line windowError: object 'x' not found> infun10(5) ## but infun10 somehow 'knows' about x[1] 50 ## = 10*5> rm(outer_fun) ## what if we remove outer_fun completely > outer_funError: object 'outer_fun' not found ## yup, it's gone> infun10(2)[1] 20 ## But infun10 still knows about x ## this is because x is in the **environment** of infun10> environment(infun10)$x[1] 10 ## and it can even be changed there:> environment(infun10)$x <- 3 > infun10(2)[1] 6 ## now it's 3*2 instead of 10*2 So there's obviously a lot going on under the hood here. If you decide that you want (or need) to get into at least some of this, please note that this list cannot serve as a tutorial service. We can help, as you know, but we expect that you will put in the effort required. As I said, there are a ton of (good) tutorials out there to choose from. Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Wed, Jan 12, 2022 at 12:07 PM Kai Yang via R-help <r-help at r-project.org> 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) > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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]]
The table you are looking for indeed does not any longer exists. Kai. Anything created within a function generally disappears as soon as it exits as the only pointer to it goes away and garbage collection can eventuially reuse the space. The code you wrote should end with: MyNamedVariable <-?f1(iris, Species) It will then have a pointer to it and will show up as that name in the RSTUDIO window for Environment when it is the selected function of the upper right.? By just writing this: f1(iris, Species) What you effectively did at the console level is this becue of automatic printing: print(f1(iris, Species)) As soon as the printing is done, nothing is pointing at the memory location in the current environment or any environment. There are, of course, ways for your f1() function to create a variable and save it in another environment such as global or parent under a designated name, but the easiest thing to do is to save teh result as shown as the return value of the function. Again, note, that if the place the function is called from is another function, any variables within it will disappear again when it exists, ... Not suggesting you use it, but there is an assignment operator that assigns outside the immediate function using <<- But back to your code, tell us more about the function view() you call several times in f1() and I assume you used it in debugging and it is not to be kept. On my machine the normal function is capitalized as View() so I am guessing you are getting the tidyverse version with view() in all lower case. It does return the object viewed and as that is the last line, it will return the object from the function. But probably not the best or most obvious way to return something as using the more standard View() function returns NULL. -----Original Message----- From: Kai Yang via R-help <r-help at r-project.org> To: R-help Mailing List <r-help at r-project.org> Sent: Wed, Jan 12, 2022 3:07 pm Subject: [R] how to find the table in R studio 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) ??? [[alternative HTML version deleted]] ______________________________________________ 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]]