Amer
Amer

Reputation: 2251

Render data frame into a Table output in shiny

I would like to render the data frame calculated in table.data into a table output in shiny. I tried using randerDataTable and renderTable but that didn't work. I must have been missing something. I would appreciate guidance on how to render the data frame into a table output underneath the plot. Here is a running example.

library(shiny)
library(data.table)
library(flexsurv)
#load some data
data(pbc, package="randomForestSRC")
data <- as.data.table(na.omit(pbc))
data[, years := days/365.25]

fit <- flexsurvspline(Surv(years, status) ~ albumin, data=data, k=1, scale='hazard')

ui <- fluidPage(
    fluidRow(
        column(12, h1('TITLE'), align='middle')
    ),
    hr(),
    sidebarLayout(
        sidebarPanel(
            sliderInput('COVAL', 'Covariate value', min=1, max=5, value=3, step=1), 
            align = 'left', 
        ),
        mainPanel(
            plotOutput('PLOT', height = 600, width = 600),
            tableOutput('TABLE')
        )#mainpanel
    )#sidebarLayout
)#fluidPage


server <- function(input, output) {
    
    table.data <- reactive({
        ndata <- data.frame(albumin = input[["COVAL"]])
        prob=summary(fit,type='survival',newdata=ndata, t=12)
        prob=as.data.frame(matrix(unlist(prob),nrow = length(prob),byrow = TRUE))
        table.data <- as.data.frame(prob)
        return(table.data)
    })#reactive
    
    output$PLOT <- renderPlot({
        ndata <- data.frame(albumin = input[["COVAL"]])
        plot(fit, est=FALSE, ci=FALSE, col.obs = 'white')
        lines(fit, newdata = ndata, ci=FALSE, col = 'green')
    })
    
    #renderTable({table.data()}) #not working
    renderDataTable(table.data())  #not working
    
}

shinyApp(ui, server)

Upvotes: 0

Views: 2759

Answers (1)

St&#233;phane Laurent
St&#233;phane Laurent

Reputation: 84519

You omit the output[["TABLE"]]:

output[["TABLE"]] <- renderTable(table.data()) 

Upvotes: 4

Related Questions