我正在编写一个R文件,它提示用户上传文件并在用户上传的文件中绘制数据。但是,我不知道如何在我的代码中引用这些列(我正在尝试使用 ggplot2)。
用户将上传的数据将是一个 CSV 文件,看起来类似,但可能会有所不同:
January February March April May
Burgers 4 5 3 5 2
我被困在需要引用列名的 ggplot2 部分。
服务器.R
library(shiny)
library(datasets)
library(ggplot2)
X <- read.csv(file.choose())
# Define server logic required to summarize and view the selected dataset
shinyServer(function(input, output) {
# Generate a summary of the dataset
output$summary <- renderPrint({
dataset <- X
summary(dataset)
})
# Show the first "n" observations
output$view <- renderTable({
head(X, n = input$obs)
})
# create line plot (I took this from https://gist.github.com/pssguy/4171750)
output$plot <- reactivePlot(function() {
print(ggplot(X, aes(x=date,y=count,group=name,colour=name))+
geom_line()+ylab("")+xlab("") +theme_bw() +
theme(legend.position="top",legend.title=element_blank(),legend.text = element_text(colour="blue", size = 14, face = "bold")))
})
})
用户界面.r
library(shiny)
# Define UI for dataset viewer application
shinyUI(pageWithSidebar(
# Application title
headerPanel("Sample Proj"),
# Sidebar with controls to select a dataset and specify the number
# of observations to view
sidebarPanel(
numericInput("obs", "Number of observations to view:", 10)
),
# Show a summary of the dataset and an HTML table with the requested
# number of observations
mainPanel(
tabsetPanel(
tabPanel("Table", tableOutput("view")),
tabPanel("LineGraph", plotOutput("plot"))
)
)
))