gpt4 book ai didi

r - 根据 Shiny 的文件动态创建 checkBoxGroup

转载 作者:行者123 更新时间:2023-12-01 13:40:06 25 4
gpt4 key购买 nike

我正在制作一个 Shiny 的应用程序来根据上传的数据创建绘图。输入文件中的一列是一个类别。我想为每个独特的类别创建一个复选框,但我认为我遗漏了一些东西。用户界面

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))
})
}

关于r - 根据 Shiny 的文件动态创建 checkBoxGroup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41050116/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com