A minimal example is given at the bottom and it does work as expected. Issue is when the returned table is assigned (see below), all the printing disappears. (I am rendering as html in Quarto.)
a<-create_reactable(1:10)
dosomething(a)
b<-create_reactable(20:30)
dosomething(b)
c<-create_reactable(30:40)
dosomething(c)
What I am trying to do is having a little function that does some processing, prints the processed table and returns the processed table. (This function should later be used for calling from a loop)
{r}
# Load the reactable package
library(reactable)
# Define the function
create_reactable <- function(values) {
# Create a data frame with values and their squares
df <- data.frame(
Column1 = values,
Column3 = values^2
)
# Create the reactable
table <- reactable(df)
# Print the reactable
print(reactable(df))
# Return the reactable
return(table)
}
# Call the function for different ranges
create_reactable(1:10)
create_reactable(20:30)
create_reactable(30:40)
I did come to realize that R handle return a little differently. However could not reach a solution that works with printing, return and assignment of return as outlined.
Please advise.
As you can see here, this is due to the way Quarto handles printing objects with the knitr
package.
You can obtain your printed reactable objects by using the knit_print()
function (after loading knitr
with library(knitr)
) instead of print
.
As a general recommendation (see the discussion in answers of this post), you should probably print the objects once created, not directly in the function to create them, by calling knit_print
outside the function:
a = create_reactable(1:10)
knit_print(a)