我正在尝试创建可以接收数据集的R Shiny应用程序,从输入值中选择要绘制的项目。当我尝试根据反应函数中的输入值过滤数据集时,我遇到了问题。我也在ggplot函数中遇到错误。我得到"对象类型'关闭'不是子集表格"并且" ggplot不知道如何处理反应函数"。我用()尝试了tmpdf,甚至没有。似乎没什么用。
Server.R
require(dplyr)
require(ggplot2)
shinyServer(function(input, output) {
#This function is repsonsible for loading in the selected file
filedata <- reactive({
infile <- input$datafile
if (is.null(infile)) {
# User has not uploaded a file yet
return(NULL)
}
read.csv(infile$datapath)
})
#This function is repsonsible for loading the AS Item nos in the selected file
output$asitmno <- renderUI({
df <-filedata()
if (is.null(df)) return(NULL)
itmchoices <- unique(df$ASItemNo)
selectInput("asitmno", "AS Item No:", choices = itmchoices)
})
#This function is triggered when the action button is pressed
getplot <- reactive({
if (input$getplot == 0) return(NULL)
df=filedata()
itm=input$asitmno
if (is.null(df)) return(NULL)
#This function filters the dataset for the given item
tmpdf <- reactive({
if (is.null(df)) return(NULL)
df$MonthDate<-as.Date(df$MonthDate, "%m/%d/%Y")
df<-df[input$ASItemNo %in% input$asitmno]
})
#This function plots the prices for the selected item
output$plot <- renderPlot({
p<-ggplot(tmpdf(), aes(y = stdcom, x = MonthDate, color = "Commodity Price")) + geom_line() +geom_line(data = tmpdf, aes(y = stitm, x = MonthDate, color = "Item Price",text = paste("R2 value:",round(cor0*100,2),"%"))) + ylab('Price') + xlab('MonthDate')+ggtitle("Plot of Item Price vs Commodity Price")
print(p)
})
})
ui.R
shinyUI(pageWithSidebar(
headerPanel("Commodity Price Vs item Price Plots"),
sidebarPanel(
#Selector for file upload
fileInput('datafile', 'Choose CSV file',
accept=c('text/csv', 'text/comma-separated-values,text/plain')),
#These column selectors are dynamically created when the file is loaded
uiOutput("asitmno"),
#The action button prevents an action firing before we're ready
actionButton("getplot", "Get Plot")
),
mainPanel(
plotOutput("plot")
)
))