尽管我的图表无法正常工作,但我正在尝试获取Shiny仪表板。
我有一个data.frame,它由日期格式的Date
列和4个数字列类型组成,这些类型为ISA, NonISA, FixedRateInterest and VariableRateInterest
,并用0填充了N / A值。
我有一个带有日期范围滑块的复选框,我可以渲染图形,但是当我尝试在DateRangeslider中添加它时,它将不起作用并抛出以下错误:
Unknown input:data.frame
这是我创建仪表板的代码。谁能帮我了解为什么它不起作用?我知道这与ggplot行有关,但不确定是什么。
谢谢
isa <- read.csv("isa.csv")
isa$Date <- paste0(isa$Date, "01")
isa$Date <- as.character(isa$Date)
isa$Date <- as.Date(isa$Date, format = "%Y%m%d")
date <- isa$Date
# Define UI ----
ui <- fluidPage(
titlePanel("Customer"),
sidebarLayout(
sidebarPanel(
br(),
br(),
br(),
br(),
br(),
br(),
br(),
br(),
br(),
br(),
br(),
br(),
br(),
br(),
br(),
br(),
br(),
img(src = "Logo.jpg", height = 100, width = 150)),
fluidRow(
h1("Choose Dataset"),
br(),
br(),
column(1,
checkboxGroupInput(inputId = "product",
h3("Product"),
choices = c('ISA',
"Non-ISA",
"Fixed",
"Variable"),
selected = 1)),
br(),
br(),
br(),
br(),
br(),
br(),
br(),
br(),
br(),
br(),
br(),
column(2,
dateRangeInput(inputId = "dates", h3("Date Range"),
start = min(isa$Date),
end = max(isa$Date),
min = min(isa$Date),
max = max(isa$Date),
separator = "-", format = "yyyy/mm/dd")
))),
br(),
br(),
br(),
mainPanel(
plotOutput("barplot")
)
)
)
# Define server logic ----
server <- function(input, output) {
dataInput <- reactive({
switch(input$product,
"ISA" = ISA,
"Non-ISA" = isa$NonISA,
"Fixed" = isa$FixedRateInterest,
"Variable" = isa$VariableRateInterest)
似乎在这里失败了
})
dateInputx <- reactive({
isa[isa$Date >= input$dates[1] & isa$Date <= input$dates[2],]
})
output$barplot <- renderPlot({
并且无法识别ggplot上的x轴
ggplot(data = dataInput(), aes_string(x= dateInputx(), y = dataInput())) +
geom_line(stat ="identity")
})
}
# Run the app ----
shinyApp(ui = ui, server = server)
答案 0 :(得分:1)
您在data
中的ggplot
参数应该是数据框,而不是列。然后,将aes_string()
与相应的列名一起使用。我尝试在下面制作一个可复制的示例:
library(shiny)
library(dplyr)
library(ggplot2)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
selectInput('ycol','Y axis',choices = c('City Mileage','Highway Mileage')) #
),
mainPanel(
plotOutput('plot')
)
)
)
server <- function(input,output){
data(mpg) # Replace mpg with your dataframe of choice(isa in your case)
dataInput <- reactive({ # Required y column name as in dataframe
switch(input$ycol,
"City Mileage" = 'cty',
"Highway Mileage" = 'hwy')
})
output$plot <- renderPlot({
# Use aes_string for column names stored in strings
p <- ggplot(data=mpg,aes_string(x='displ',y=dataInput()))+
geom_line()
p
})
}
shinyApp(ui,server)