繁体   English   中英

基于闪亮的文件动态创建checkBoxGroup

[英]Dynamically create checkBoxGroup based on a file in shiny

我正在创建一个闪亮的应用程序来创建上传数据的图表。 输入文件中的一列是一个类别。 我想为每个独特的类别创建一个复选框,但我认为我缺少一些东西。 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")
    )
)

这是我的服务器.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))

    })
}

我一直在使用的测试文件。

A   10  10
A   1   2
B   0   1
C   5   5
C   0   1
C   5   11
D   1   2

不应使用全局变量,而应使用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))
    })
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM