Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically create checkBoxGroup based on a file in shiny

I'm making a shiny app to create plots from uploaded data. One column in the input file is a category. I'd like to create a check box for each unique category, but I think there's something I'm missing. ui.R

fluidPage(
    titlePanel("Dynamic Check Boxes"),
    fluidRow(

    fileInput('file1', 'Upload a file'),

    plotOutput('plot1'),

    # This is where I'm trying to put the check boxes
    uiOutput("ui")
    )
)

Here's my server.R

categories = c()
test_data = NULL

function(input, output) {

    # Trying to generate the check boxes
    output$ui <- renderUI({
        if (is.null(input$input_type))
            return()
        checkboxGroupInput('test', 'checkboxes', categories)
    })

    output$plot1 <- renderPlot({

        inFile <- input$file1

        if (is.null(inFile))
            return(NULL)

        test_data <<- read.table(inFile$datapath, head = F)
        categories <<- unique(test_data$V1)

        ggplot(test_data, aes(V2, V3)) + geom_point(aes(colour=V1))

    })
}

The test file I've been using.

A   10  10
A   1   2
B   0   1
C   5   5
C   0   1
C   5   11
D   1   2
like image 951
ate50eggs Avatar asked Dec 08 '16 22:12

ate50eggs


1 Answers

Rather than using global variables, you should use reactive:

function(input, output) {
    # read the file
    test_data <- reactive({
        inFile <- input$file1
        if (is.null(inFile))
            return(data.frame(V1=character(0), V2=integer(0), V3=integer(0)))
        read.table(inFile$datapath, head = F)
        })

    # Trying to generate the check boxes
    output$ui <- renderUI(checkBoxGroup('test', 'checkboxes', unique(test_data()$V1)))

    output$plot1 <- renderPlot({
        ggplot(test_data(), aes(V2, V3)) + geom_point(aes(colour=V1))
    })
}
like image 193
HubertL Avatar answered Nov 18 '22 19:11

HubertL