rgridgridextra

How to vary the data in a table using gridExtra table


I want to vary the values of m and n in the following table. Specifically in the second column. I attach an image of how it looks like. How can I do it?

enter image description here

These are the codes:

library(grid)
library(gridExtra)

n <- round(runif(1, 3, 10))

m <- round(runif(1, 11, 18))

intervalos17 <- c( paste0("x<",n) ,paste0("{", n,  c( paste0( "<=x}<" )) ,m ), paste0("x>=",m))
 
 
prueba17 <- c(0, "frac(x-n, m-n)", 1  )




<<echo=FALSE, results=hide, fig=TRUE,width=5.5,height=3>>=

tablasolucion <-data.frame (intervalos17 , prueba17)
colnames (tablasolucion) <-c(  "x" ,  'F(x)' )

tt <- ttheme_default(core = list(fg_params = list(parse=TRUE)),colhead=list(fg_params = list(parse=TRUE)))

grid.newpage()
grid.table(tablasolucion, rows = NULL, theme=tt)
@

enter image description here


Solution

  • Like this?

    out

    library(grid)
    library(gridExtra)
    
    # Generate random n and m
    n <- round(runif(1, 3, 10))
    m <- round(runif(1, 11, 18))
    
    # Create intervals with numeric values
    intervalos17 <- c(paste0("x<", n),paste0(n, "<=x<", m),paste0("x>=", m))
    
    # Create the fraction with ACTUAL n and m values
    prueba17 <- c(0, paste0("frac(x-", n, ", ", m-n, ")"),1)
    
    # Create and display the table
    tablasolucion <- data.frame(intervalos17, prueba17)
    colnames(tablasolucion) <- c("x", "F(x)")
    
    tt <- ttheme_default(core = list(fg_params = list(parse=TRUE)), colhead = list(fg_params = list(parse=TRUE)))
    
    grid.newpage()
    grid.table(tablasolucion, rows = NULL, theme=tt)