【发布时间】:2018-08-30 08:02:33
【问题描述】:
我目前正在尝试编写一个闪亮的应用程序并接近解决方案,我想达到。但是,有些问题我无法解决...
if(!require(shiny)){
install.packages("shiny")
require(shiny)
}
if(!require(tidyverse)){
install.packages("tidyverse")
require(tidyverse)
}
if(!require(readxl)){
install.packages("readxl")
require(readxl)
}
if(!require(lubridate)){
install.packages("lubridate")
require(lubridate)
}
prodpromonat <- tibble(prodmonat = c("2008-01-01", "2008-02-01", "2008-03-01", "2008-04-01", "2008-05-01", "2008-06-01", "2008-07-01", "2008-08-01"),
n = c("3216", "3268", "2398", "2987", "4003", "3103", "3064", "2786"))
prodpromonat$prodmonat <- as.Date(prodpromonat$prodmonat)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
sliderInput(
"zeitraum",
"Produktionszeitraum",
min = min(prodpromonat$prodmonat),
max = max(prodpromonat$prodmonat),
value = c(max(prodpromonat$prodmonat)-90, max(prodpromonat$prodmonat))
),
fluidRow(
radioButtons(
"farbschema",
"Farbschema",
choices = c("Einfarbig", "Zweifarbig"),
selected = "Einfarbig"
)
),
fluidRow(
uiOutput("farbwahl")
)
),
mainPanel(
tabsetPanel(
tabPanel(
title = "Produktionsmenge" ,
plotOutput(
outputId = "produktionsmenge"
)
)
)
)
))
server <- function(input, output, session){
farbeninput <- reactive({
switch(input$farbschema,
"Zweifarbig" = c(input$farbe1, input$farbe2),
"Einfarbig" = c(input$farbe1, input$farbe1)
)
})
filter_produktionsmenge <- reactive({
min <- filter(prodpromonat, prodmonat >= floor_date(input$zeitraum[1], "month"))
max <- filter(prodpromonat, prodmonat <= floor_date(input$zeitraum[2], "month"))
semi_join(min, max, by = "prodmonat")
})
output$farbwahl <- renderUI({
switch(input$farbschema,
"Zweifarbig" = tagList(textInput("farbe1", "Farbe 1", value = "#808080"), textInput("farbe2", "Farbe 2", value = "#1E90FF")),
"Einfarbig" = textInput("farbe1", "Farbe", value = "#808080")
)
})
output$produktionsmenge <- renderPlot({
ggplot(filter_produktionsmenge(), aes(factor(prodmonat), n)) +
geom_bar(stat="identity", aes(fill = factor(as.numeric(month(prodmonat) %% 2 == 0)))) +
scale_fill_manual(values=rep(farbeninput())) +
xlab("Produktionsmonat") +
ylab("Anzahl produzierter Karosserien") +
theme(legend.position = "none")
})
}
shinyApp(ui, server)
问题 #1:在滑块输入中,我提供所选时间跨度内的每一天,而我只想提供现有月份,最好不提供当天的任何提示。我试图打印“%Y-%m”的每个解决方案都会产生错误,所以我坚持了这些日子......
问题 #2:是否有可能在使用 uiOutput 之前定义颜色向量?现在它几乎可以按我的计划工作,但是在渲染的 UI 完成渲染并显示在侧边栏中之前显示了一个错误。我的猜测是,在 renderUI 完成之前,该向量不存在,因为错误显示为“手动缩放中的值不足。需要 2,但只提供了 0。”。所以我想初始化它。首次更改为双色模式同样有效。
您对如何解决这些问题有任何想法吗?非常感谢!
干杯!
编辑:
之前找到了问题的解决方案。但是,问题 #1 和 #2 仍然存在。
*edit: 从代码中删除一个括号
【问题讨论】:
-
首先,我不知道日期问题。我已经尝试使用来自
zoo包的as.yearmon()进行一些黑客攻击,但没有任何成功。其次,我建议使用设置的提交按钮,因为textInput()未初始化。