Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shiny print reactive df to console (glimpse(myreactive_df) for debugging purposes?

Tags:

r

shiny

I'm trying to debug my Shiny app and would like to view a reactive dataframe with e.g. glimpse(df).

Initially I tried to create a breakpoint and then view the environment by my reactive df is a value not an object when used inside server.r. I also tried browser() but was not sure what it would do.

I did some searching on SO and tried various things using sink(), renderPrint() but had no success.

How can I print the contents of glimpse(some_reactive_df()) to the console when I run my app?

like image 879
Doug Fir Avatar asked Feb 03 '18 00:02

Doug Fir


1 Answers

Calling print() from within the reactive({}) expression will do that.

library(shiny)

library(dplyr)

shinyApp(
    ui <- fluidPage(
        selectizeInput("cyl_select", "Choose ya mtcars$cyl ", choices = unique(mtcars$cyl)),

        tableOutput("checker") # a needed output in ui.R, doesn't have to be table
    ),
    server <- function(input, output) {

        d <- reactive({ 
            d <- dplyr::filter(mtcars, cyl %in% input$cyl_select)
            print(glimpse(d)) # print from within
            return(d)
            })

    output$checker <- renderTable({
        glimpse(d()) # something that relies on the reactive, same thing here for simplicty
    })
    })

Assuming you provide Shiny a reason to run (and re-run) your reactive of interest, by having it be involved with a rendering in server() and linked output in ui(). This is usually the case for my debugging scenarios but it won't work unless the reactive is being used elsewhere in app.R.

like image 95
Nate Avatar answered Oct 07 '22 01:10

Nate