당신은 코드의이 부분에서 조심해야 :
data_f <- filter(data_products,
product==input$type)
이 예제에서, 당신의 선택에 따라 input$type
0, 1 또는 2 요소를 포함 할 수 있습니다. 하나의 요소가 "a"
또는 "b"
인 경우 모든 항목이 정상이지만 다른 경우에는 오류 또는 경고가 표시됩니다.
위젯에서 값을 선택하지 않은 경우 input$type
은 NULL
을 반환합니다. 따라서 논리 비교가 실패하고 오류가 발생합니다. 이를 피하기 위해 누락 된 입력을 사용하기 전에 req
또는 validate
기능을 사용할 수 있습니다.이 기능은 "입력이 필요함"으로 읽을 수 있습니다. Here 누락 된 입력을 반짝이는 처리에 대해 자세히 읽을 수 있습니다.
당신은
==
여러 comparisions 작동하지 않기 때문에 모두
"a"
및
"b"
product==input$type
경고를 반환 할 것입니다 선택한 경우. 이 대신에
%in%
으로 변경하십시오.
당신은 내가 checkboxGroupInput
전체 예에 selectInput
을 변경 체크 박스 원하기 때문에 :
library(shiny)
library(dygraphs)
library(dplyr)
library(xts)
# I pasted your example data to exces and then readed it into R with these
# two lines of the code. It seems that product has to be a factor, because you
# use 'levels(data_products$product)'
# data_products <- as.data.frame(read_excel("~/Downloads/data.xlsx"))[-1]
# data_products$product <- as.factor(data_products$product)
ui <- dashboardPage(
skin="black",
dashboardHeader(title = "title"),
dashboardSidebar(
sidebarMenu(
menuItem("Results", tabName = "Result1", icon = icon("th"))
)),
dashboardBody(
tabItems(
tabItem(tabName = "Result1",
fluidRow(
dygraphOutput("Graph")
),
sidebarPanel(
uiOutput("output1")
)
)
)
)
)
server <- function(input, output) {
output$Graph <- renderDygraph({
req(input$type) # require that input$type is available
data_f <- filter(data_products,
product %in% input$type) # use "%in%" instead of "=="
xts(data_f$sold, as.Date(data_f$date, format = "%Y-%m-%d")) %>%
dygraph()
})
output$output1 <- renderUI({
# selectizeInput("type","Choose product",
# choices=levels(data_products$product), multiple=TRUE)
checkboxGroupInput("type", "Choose product",
choices = levels(data_products$product),
selected = levels(data_products$product))
})
}
shinyApp(ui, server)
이 편집을 :
a
과 b
이 선택되었을 때 두 줄을 원한다면, 데이터의 형식을 변경해야합니다. 길고 길게 가야합니다. 그 이유는 xts
및 dygraph
으로 이변 변수 시계열을 쉽게 만들 수 있기 때문에 두 개의 별도 라인을 그릴 수 있기 때문입니다.
해들리 위크 햄 (Hadley Wickham)의 reshape2
패키지를 사용하여 길게에서 와이드로 쉽게 이동할 수 있습니다.
# Copy data from your example
data_products <- read.table(con<-file("clipboard"),header=T)
data_products$product <- as.factor(data_products$product)
# Reshape
data_products <- dcast(data_products, date ~ product)
데이터 집합은 다음과 같이 지금 같습니다
date a b
1 2015-01-01 1 20
2 2015-02-01 2 15
3 2015-03-01 3 10
4 2015-04-01 4 5
5 2015-05-01 5 1
인해 데이터의 새로운 특성이 약간 서버 측에서 코드를 변경해야합니다.코드에 주석을 남겼습니다.
ui <- dashboardPage(
skin = "black",
dashboardHeader(title = "title"),
dashboardSidebar(
sidebarMenu(
menuItem("Results", tabName = "Result1", icon = icon("th"))
)),
dashboardBody(
tabItems(
tabItem(tabName = "Result1",
fluidRow(
dygraphOutput("Graph")
),
sidebarPanel(
uiOutput("output1")
)
)
)
)
)
server <- function(input, output) {
output$Graph <- renderDygraph({
req(input$type) # require that input$type is available
# Due to the wide format we have to select columns
data_f <- data_products[, c("date", input$type)]
# univariate or bivariate time series
xts(data_f[-1], as.Date(data_f$date, format = "%Y-%m-%d")) %>%
dygraph()
})
output$output1 <- renderUI({
# Since we now have data in wide format, the choices are
# the names of columns (expect date)
checkboxGroupInput("type", "Choose product",
choices = names(data_products)[-1])
})
}
shinyApp(ui, server)
당신이 생각하기에 가장 좋은 접근 방법이며 어떤 오류가 있으시면 알려주십시오. –