我正在尝试构建一个闪亮的应用程序,我可以在其中上传 csv 文件并根据列名称填充 ui 中左列(滑动栏列)上的复选框。根据为 y 轴选择的列和为 x 轴选择的列,需要能够使用 ggplot 创建图表。
我的 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 并选择用于绘图的轴ggplot
。还为此添加了一个额外的答案。