我正在尝试构建一个闪亮的应用程序,我可以在其中上传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
进行绘图。另外还为此添加了答案。
答案 0 :(得分:5)
这个答案只是解决了csv加载问题,请参阅下面的下一个答案,其中一个实际上是用ggplot进行绘图。
所以(在合并到一个文件中以便更容易处理之后),我向ui部分添加了checkboxGroupInput
,并向服务器部分添加了相应的updateCheckboxGroupInput
。我需要它来在数据集发生变化时更新组,因此我稍微重新构建了代码,使data_set
加载部分reactive
封装updateCheckboxGroupInput
observer
内的library(shiny)
library(shinydashboard)
library(leaflet)
library(data.table)
ui <- 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),
checkboxGroupInput("inCheckboxGroup",
"Checkbox group input:",
c("label 1" = "option1",
"label 2" = "option2")),
radioButtons('sep', 'Separator',
c(Comma=',',
Semicolon=';',
Tab='\t'),
','),
radioButtons('quote', 'Quote',
c(None='',
'Double Quote'='"',
'Single Quote'="'"),
'"'),
uiOutput("choose_columns")
),
mainPanel(
tableOutput('contents')
)
)
server <- function(input, output,session) {
dsnames <- c()
data_set <- reactive({
req(input$file1)
inFile <- input$file1
data_set<-read.csv(inFile$datapath, header=input$header,
sep=input$sep, quote=input$quote)
})
output$contents <- renderTable({
data_set()
})
observe({
req(input$file1)
dsnames <- names(data_set())
cb_options <- list()
cb_options[ dsnames] <- dsnames
updateCheckboxGroupInput(session, "inCheckboxGroup",
label = "Check Box Group",
choices = cb_options,
selected = "")
})
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)
})
}
shinyApp(ui, server)
。
所以这就是你想要的:
import config
以下是截图:
答案 1 :(得分:5)
所以添加另一个答案以容纳一个额外的请求 - 它不仅读取文件,而且允许您选择用于绘图的列,它实际上在带有ggplot2
的单独选项卡中绘制:
library(shiny)
library(shinydashboard)
library(leaflet)
library(data.table)
library(ggplot2)
ui <- 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),
fluidRow(
column(6,radioButtons("xaxisGrp","X-Axis:", c("1"="1","2"="2"))),
column(6,checkboxGroupInput("yaxisGrp","Y-axis:", c("1"="1","2"="2")))
),
radioButtons('sep', 'Separator',
c(Comma=',', Semicolon=';',Tab='\t'), ','),
radioButtons('quote', 'Quote',
c(None='','Double Quote'='"','Single Quote'="'"),'"'),
uiOutput("choose_columns")
),
mainPanel(
tabsetPanel(
tabPanel("Plot",plotOutput("plot")),
tabPanel("Data", tableOutput('contents'))
)
)
)
server <- function(input, output,session) {
dsnames <- c()
data_set <- reactive({
inFile <- input$file1
if (is.null(inFile))
return(mtcars)
data_set<-read.csv(inFile$datapath, header=input$header,
sep=input$sep, quote=input$quote)
})
output$contents <- renderTable({data_set()})
observe({
dsnames <- names(data_set())
cb_options <- list()
cb_options[ dsnames] <- dsnames
updateRadioButtons(session, "xaxisGrp",
label = "X-Axis",
choices = cb_options,
selected = "")
updateCheckboxGroupInput(session, "yaxisGrp",
label = "Y-Axis",
choices = cb_options,
selected = "")
})
output$choose_dataset <- renderUI({
selectInput("dataset", "Data set", as.list(data_sets))
})
output$plot = renderPlot(
{
df <- data_set()
gp <- NULL
if (!is.null(df)){
xv <- input$xaxisGrp
yv <- input$yaxisGrp
if (!is.null(xv) & !is.null(yv)){
if (sum(xv %in% names(df))>0){ # supress error when changing files
mdf <- melt(df,id.vars=xv,measure.vars=yv)
gp <- ggplot(data=mdf) +
geom_point(aes_string(x=xv,y="value",color="variable"))
}
}
}
return(gp)
}
)
output$choose_columns <- renderUI({
if(is.null(input$dataset))
return()
colnames <- names(contents)
checkboxGroupInput("columns", "Choose columns",
choices = colnames,
selected = colnames)
})
}
shinyApp(ui, server)