R 基于csv文件的列名创建选择列表,以便在中打印
我正在尝试构建一个闪亮的应用程序,在这里我可以上传一个csv文件,并根据列名填充ui中左列(slidebar列)上的复选框。根据y轴选择的列和x轴选择的列,需要能够使用ggplot创建图表 我的ui.R如下所示:R 基于csv文件的列名创建选择列表,以便在中打印,r,csv,plot,ggplot2,shiny,R,Csv,Plot,Ggplot2,Shiny,我正在尝试构建一个闪亮的应用程序,在这里我可以上传一个csv文件,并根据列名填充ui中左列(slidebar列)上的复选框。根据y轴选择的列和x轴选择的列,需要能够使用ggplot创建图表 我的ui.R如下所示: shinyUI(pageWithSidebar( headerPanel("CSV Viewer"), sidebarPanel( fileInput('file1', 'Choose CSV File', accept=c('text/cs
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')
)
))
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)
})
})
Server.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')
)
))
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)
})
})
shinyServer(功能(输入、输出){
output$contents这个答案只是解决了csv加载问题,请参阅下面我的下一个答案,了解实际使用ggplot进行打印的答案
因此(在合并到一个文件以使其更易于处理后),我在ui部分添加了一个checkboxGroupInput
,在服务器部分添加了一个相应的updateCheckboxGroupInput
。当数据集发生变化时,我需要它来更新组,所以我稍微重新构造了代码,使数据集
加载部分a反应式
封装了updateCheckboxGroupInput代码>在观察者内
这就是你想要的:
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)
库(闪亮)
图书馆(shinydashboard)
图书馆(单张)
库(数据表)
ui因此,添加另一个答案以适应额外的请求-它不仅读取文件,而且允许您选择要打印的列,它实际上使用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)
库(闪亮)
图书馆(shinydashboard)
图书馆(单张)
库(数据表)
图书馆(GG2)
ui为什么在这个或OP问题上没有语法高亮显示?这很好。一个简单的问题是,如果我想根据复选框组中的选定项创建一个始终作为默认日期字段的ggplot x轴,我如何创建ggplot?我可以这样做,并且很乐意添加它(但晚餐后:)。我会很感激,因为我想让ggplot2投票给一个徽章:)我还想知道您如何设置时间字段的格式(是ui.R还是server.R部分)我看到你把绘图放在服务器上,并调用ui的输出。你会如何根据ui的输入对数据进行子集划分等。例如,我希望我的x轴始终是日期,但y轴值会根据用户从ui中的选择而变化。非常感谢你在这方面的帮助。好吧,这不是你想要的吗?会有一些反馈非常感谢。很抱歉反应太晚,我收到这个“错误:数据中找不到id变量”抱怨列名称好吧,让我看看。是的,当你加载一个文件时有一个错误。让我看看。我在输出$plot部分将mtcars更改为df。我有一个简单的问题。假设我将选择一个列,其中包含日期时间或日期数据。这些数据作为因子输入,我如何将这些数据格式化为日期或日期时间格式,以便他的图表可读。