Displaying tibble along with return from a function.

Env: Rstudio, Quarto qmd document

Code structure: I have a function that displays a tibble in html format as specified in css. Main script calls intermediate function which calls the above display function.

Problem: In the minimal working example code below, display of tibble is coming as expected when display_tibble_as_html function is not having a return and a<- assignment is not there.
When return(pdata) is added , the html formatting is lost and simple text table is seen.
When a<- is also added, nothing is shown.

Images of mentioned outputs in the three cases attached.

May I know the reason for this behaviour and a way to have it printed in html format itself even with return and assignment.

---
title: "MWE"
execute:
  echo: true
  warning:  true
format:
  html:
    css: style.css
    toc: true
---

```{r}
library(knitr)
library(tibble)

display_tibble_as_html <- function(pdata) {
  kable(pdata, format = "html", table.attr = 'class="scrollable-table"')
  return(pdata)
}

interim_function<-function(data){
display_tibble_as_html(data)
}
data <- as_tibble(mtcars[1:5, 1:5])
a<-interim_function(data)

![expected_1|690x286](upload://quISJvXiJZPUuiSXXS8aJslFwYA.png)
![q_on_assigning|690x213](upload://7rugpwkzmp2eorP7rhvdMxTKAHm.png)
![q_on_return|690x249](upload://4NqmjfAVSSAp8bES6BaZCEQDaN2.png)

I think changing kable() by print(kable()) will solve your problem (?)

print did not solve the issue. I tried with a reactable as well as html by kable. Even then the behaviour is something like

lets say mtcars is the table

within function
mtcars

works well shows in html

within function
mtcars
return something

table not shown in html

within function
mtcars
a<-2

table not shown in html

in short if mtcars is not the last thing in the function the table will not show up when rendered in Quarto.
I think I am missing some basic things about R returns.

Thankyou very much for the print suggestion. Please do suggest if you have some input on the above behaviour of R functions.

This is intended behaviour in R. From the start of Section 19.6.1 of R for Data Science:

" The value returned by the function is usually the last statement it evaluates, but you can choose to return early by using return()."

Functions return the object in the first return() statement it finds, otherwise it will return the result of the last line that is evaluated in the function. You cannot have a function return things from multiple parts of the function code, but you can:

  • Combine multiple objects into a structure (such as a list) and return that as a single object.
  • Perform a side-effect before returning an object. A side effect is where an operation is performed that does not return a value in itself, such as printing a message to the console or writing data to a file.

When a<- is also added, nothing is shown.

This is also expected behaviour in R. We do not echo the value when an object is assigned, although you can create that behaviour by placing the assignment entirely inside parentheses (), e.g.

a <- 2 # Sets a to be 2, but does not print 2
(b <- 3) # Sets b to be 3 and then prints 3

Alternatively, you can recall the value after the assignment

a <- 2
a
## [1]  2

When return(pdata) is added , the html formatting is lost and simple text table is seen.

Looking at your code:

You run the kable line to create the HTML table, but this is not what will be returned from your function because it is not the last line in your function. The last line is an instruction to return pdata which has not been modified or overwritten since the very start of the function where it appears as an input variable. To get the kable(pdata, ...) result, let that be the last line of the function, or place it in a return() call.

Hi Keith,
Thank you very much for providing the detailed reply. It answered a lot of doubts that I had in mind.

Looks like what I need is just the opposite of side effect.

The whole point of the function was, I would need to show a table before and after some change to table. And this will happen to ~40 tables. Hence trying to write a function that prints something and return something else. I hope the below with an example data
conveys the structure and logic.

[I am rendering in Quarto in a Rstudio env.]

---
title: "Example code"
execute:
  echo: true
  warning:  false
format:
  html:
    css: style.css
    toc: true
editor_options: 
  chunk_output_type: console
---
library(dplyr)
library(tidyverse)
library(knitr)

see<-function(pdata){
  kable(pdata, format = "html", table.attr = 'class="scrollable-table"')
}

show_data_before_after_3colselect_and_return<-function(pdata,cyl_count){
  cyl_filt_data<-subset(mtcars, cyl == cyl_count)
  cat ("The complete data with all columns is given below:",cyl_count,"cyls")
  see(cyl_filt_data)
  select_data <- pdata |> select(1,mpg,disp)
  cat ("Selecting only mpg and disp.")
  see(select_data)
  return(select_data)
}

four_data<-show_data_before_after_3colselect_and_return(mtcars,4)
six_data<-show_data_before_after_3colselect_and_return(mtcars,6)
eight_data<-show_data_before_after_3colselect_and_return(mtcars,8)
#do_something_on(four_data,six_data,eight_data)

No table got shown in html. The output has two cat statements pasted together like "The complete data with all columns is given below: 4 cylsSelecting only mpg and disp."

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.