ShinyApp:Why选择多个输入时,GGPLOT的结果是否会改变?
ShinyApp:Why do the outcomes change in ggplot when more than one input is selected?
我正在创建一个 ShinyApp 用于对各种工人进行年度绩效评估,并希望根据输入显示一个或多个工人 (W) 的绩效 (salespermonth)选择。但是,当在 sidebarPanel 中选择多个 "W" 时,我遇到相同 "W" 的值发生变化。有谁知道为什么?
data.b
的反应函数似乎有问题,更准确地说是 ID==input$ID
.
的部分
data.frame
示例(ID 从 1-10):
ID Calendartime timewithfirm salespermonth cumm.sales
1 1 2006-01-01 3 800 800
2 1 2006-02-01 3 300 1100
3 1 2006-03-01 3 150 1250
4 1 2006-04-01 3 200 1450
5 1 2006-05-01 3 350 1800
6 1 2006-06-01 3 100 1900
ui.R
library(shiny)
shinyUI(pageWithSidebar(
headerPanel(title=h1("Worker Performance Assessment", align="center")),
sidebarPanel(
uiOutput("chooseW"),
dateRangeInput("daterange", label = h3("Date range"),start = "2006-01-01",end = "2006-12-01",format = "yyyy/mm/dd", separator="-"),
submitButton(text="Update!")
) ,
mainPanel(
plotOutput("salesplot")
)
)
)
server.R
library(shiny)
library(ggplot2)
library(scales) # for formatting date scales in ggplot
shinyServer(function(input, output) {
output$chooseW <- renderUI({
IDs<- dataframe$ID
selectInput("ID", label=h3("Choose a Worker"), IDs, 1 , multiple = TRUE)
})
# reactive function for plot
data.b = reactive({
b= subset(dataframe,Calendartime %in% (input$daterange[1]:input$daterange[2]) & ID == input$ID)
return(b)
})
output$salesplot <- renderPlot({
cc<- data.b()
q= ggplot(cc, aes(x=Calendartime,y=salespermonth, group=ID, colour=ID)) +
geom_line() +scale_x_date(breaks = date_breaks("1 month"),labels = date_format("%b"))
print(q)
})
})
你是对的。问题出在 ID == input$ID
行。您只需要在 server.R 的子集行中将其更改为 ID %in% input$ID
。那么问题就迎刃而解了。这里的要点是 input$ID
不仅仅是一个值,而是一个长度 > 1 的向量,以防您有多项选择。
我正在创建一个 ShinyApp 用于对各种工人进行年度绩效评估,并希望根据输入显示一个或多个工人 (W) 的绩效 (salespermonth)选择。但是,当在 sidebarPanel 中选择多个 "W" 时,我遇到相同 "W" 的值发生变化。有谁知道为什么?
data.b
的反应函数似乎有问题,更准确地说是 ID==input$ID
.
data.frame
示例(ID 从 1-10):
ID Calendartime timewithfirm salespermonth cumm.sales
1 1 2006-01-01 3 800 800
2 1 2006-02-01 3 300 1100
3 1 2006-03-01 3 150 1250
4 1 2006-04-01 3 200 1450
5 1 2006-05-01 3 350 1800
6 1 2006-06-01 3 100 1900
ui.R
library(shiny)
shinyUI(pageWithSidebar(
headerPanel(title=h1("Worker Performance Assessment", align="center")),
sidebarPanel(
uiOutput("chooseW"),
dateRangeInput("daterange", label = h3("Date range"),start = "2006-01-01",end = "2006-12-01",format = "yyyy/mm/dd", separator="-"),
submitButton(text="Update!")
) ,
mainPanel(
plotOutput("salesplot")
)
)
)
server.R
library(shiny)
library(ggplot2)
library(scales) # for formatting date scales in ggplot
shinyServer(function(input, output) {
output$chooseW <- renderUI({
IDs<- dataframe$ID
selectInput("ID", label=h3("Choose a Worker"), IDs, 1 , multiple = TRUE)
})
# reactive function for plot
data.b = reactive({
b= subset(dataframe,Calendartime %in% (input$daterange[1]:input$daterange[2]) & ID == input$ID)
return(b)
})
output$salesplot <- renderPlot({
cc<- data.b()
q= ggplot(cc, aes(x=Calendartime,y=salespermonth, group=ID, colour=ID)) +
geom_line() +scale_x_date(breaks = date_breaks("1 month"),labels = date_format("%b"))
print(q)
})
})
你是对的。问题出在 ID == input$ID
行。您只需要在 server.R 的子集行中将其更改为 ID %in% input$ID
。那么问题就迎刃而解了。这里的要点是 input$ID
不仅仅是一个值,而是一个长度 > 1 的向量,以防您有多项选择。