1

I created my first R package which has three functions, to query the data in database and return the data frames based on user input. Since the data frames are large instead of printing them in console, I added View() within my function to show user the data extracted based on their input. Code goes like this:

queryData <- function(p, q, r, s, t){
d <- DBI::dbGetQuery(conn = con, statement = "SELECT * FROM dataset" )
  d <- d%>%
    dplyr::filter(org == p) %>%
    dplyr::filter(exp == q) %>%
    dplyr::filter(dis %like% r) %>%
    dplyr::filter(tis %like% s) %>%
    dplyr::filter(Rx %like% t)
  print(paste("No. of datasets that matched your criteria:",nrow(d)))
  View(d)
}

R check was fine, I was able to install the package and run the functions. But it gave me error when I created vignette for the package.

Here is the error message:

Error: processing vignette 'package_vignette.Rmd' failed with diagnostics:
  View() should not be used in examples etc
  --- failed re-building 'package_vignette.Rmd'
  
  SUMMARY: processing the following file failed:
    'package_vignette.Rmd'
  
  Error: Vignette re-building failed.

Any advice on how to fix this issue?

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
Gen
  • 23
  • 5

1 Answers1

2

As the error message mentioned, View() is not made for RMarkdown, which is what the package vignettes are written in. The R Markdown cookbook suggests you can display the data just by calling the object using the built-in knitr::kable(). If it's too long you can show just the first bit by subsetting it. E.g.

knitr::kable(my_table[5,5])

will print only the first 5 rows and columns of the table. There are other packages you can use too (a brief list here), which work differently depending on the desired output format.

Alternatively, you can use paged tables to avoid scrolling:

rmarkdown::paged_table(my_table)
Giulio Centorame
  • 678
  • 4
  • 19
  • 1
    if the data frames are only wide and not long you can use `knitr::kable(head(my_table))`. If you don't mind depending on tidyverse you can `print(as_tibble(my_table)))`, which will abbreviate in both row and column directions when printing – Ben Bolker Aug 11 '22 at 00:52
  • oops, first sentence above should say "long and not wide" – Ben Bolker Aug 11 '22 at 01:24
  • Thank you for sharing the links with me. I have added code chunk for one the function. My question might be silly (I am learning R and how to build packages), do you recommend me changing this View() within the function? I want the user to be able to view the table in interactive way as we see in global environment, they will refer this table to pass parameters in next function. – Gen Aug 11 '22 at 01:27
  • Do you mean the possibility of changing the order by column? Or something else in particular? – Giulio Centorame Aug 11 '22 at 05:58
  • I added another option, `paged_table()`, which is the best you can do that I am aware of with either `rmarkdown`, `kable`, or `kableExtra`. – Giulio Centorame Aug 11 '22 at 06:05