我正在尝试构建一个闪亮的应用程序,可以上传CSV文件,并基于列名称在UI中的左侧(滑杆列)上填充复选框。根据选择的y轴列和x轴列,需要能够使用ggplot创建图表。
我的ui.R如下所示:
我的ui.R如下所示:
shinyUI(pageWithSidebar(
headerPanel("CSV Viewer"),
sidebarPanel(
fileInput('file1', 'Choose CSV File',
accept=c('text/csv', 'text/comma-separated-values,text/plain', '.csv')),
tags$hr(),
checkboxInput('header', 'Header', TRUE),
radioButtons('sep', 'Separator',
c(Comma=',',
Semicolon=';',
Tab='\t'),
'Comma'),
radioButtons('quote', 'Quote',
c(None='',
'Double Quote'='"',
'Single Quote'="'"),
'Double Quote'),
checkboxGroupInput("variable", "Variable:", choices = names(data_set))
),
mainPanel(
tableOutput('contents')
)
))
Server.R看起来像这样:
shinyServer(function(input, output) {
output$contents <- renderTable({
# input$file1 will be NULL initially. After the user selects and uploads a
# file, it will be a data frame with 'name', 'size', 'type', and 'datapath'
# columns. The 'datapath' column will contain the local filenames where the
# data can be found.
inFile <- input$file1
if (is.null(inFile))
return(NULL)
data_set<-read.csv(inFile$datapath, header=input$header, sep=input$sep, quote=input$quote)
})
output$choose_dataset <- renderUI({
selectInput("dataset", "Data set", as.list(data_sets))
})
# Check boxes
output$choose_columns <- renderUI({
# If missing input, return to avoid error later in function
if(is.null(input$dataset))
return()
# Get the data set with the appropriate name
colnames <- names(contents)
# Create the checkboxes and select them all by default
checkboxGroupInput("columns", "Choose columns",
choices = colnames,
selected = colnames)
})
})
我无法在滑动条中加载数据集中的列名?有什么指针可以帮助我做到这一点。我正在加载一个csv文件,一旦文件加载完成,我需要能够使用数据集的列名填充滑块。
更新-编辑:
根据OP的要求(请参见接受答案中的评论),添加了一个从csv中读取并选择绘图轴的请求。也添加了一个额外的答案。
checkboxGroupInput
包含两个标签和选项的定义,但是:1)它们直到加载文件后才会显示出来2)加载文件后,方框的数量可以是任意的?为什么不限制为你设置的两个呢? - jzadra