我有一个非常简单的问题但是找到答案似乎有问题,
我知道在ggplot
内使用shiny
时,可以使用aes_string()
通过列的输入更改值。
因此,当我更改Test1,Test2或Test3的selectinput时,我可以在条形图上获取值。
数据
Color Test1 Test2 Test3
Black 23 43 56
Blue 32 34 45
Yellow 33 93 35
代码
shinyServer(function(input, output) {
output$column <- renderPlot({
n1<-nPlot(hrs_0to1 ~ TicketType, data=sum1, type="multiBarChart",height=600,width=1000)
n1$chart(reduceXTicks=FALSE)
n1$xAxis(staggerLabels=TRUE)
print(n1)
})
})
以上不起作用,因为我认为我不能以这种方式使用输入。
请建议一种方法来解决此问题。
更新
ui.R
library(shiny)
library(ggplot2)
shinyUI(fluidPage(
column(12,offset=5,
titlePanel("Template Type by Hours")),
br(),
h6(textOutput("text1")),
fluidRow(
column(4,offset=0,
wellPanel(
selectInput("var","Hours",
choices = colnames(sum1),selected ="hrs_0to1")))),
column(12,offset=0,
plotOutput("column", height=500,width=1000)
),
column(12,
dataTableOutput("table1")
)
))
server.R
library(devtools)
library(rCharts)
library(shiny)
shinyServer(function(input, output) {
output$column <- renderChart2({
n1<-nPlot(x="TicketType",y= input$var, data=sum1, type="multiBarChart")
n1$chart(reduceXTicks=FALSE)
n1$xAxis(staggerLabels=TRUE)
print(n1)
})
})
数据结构
> str(sum1)
'data.frame': 37 obs. of 9 variables:
$ Ttype : Factor w/ 4 levels "Complaint","Others",..: 2 3 2 2 3 2 2 4 2 3 ...
$ TicketType : Factor w/ 37 levels "Address change",..: 4 15 2 36 22 1 12 25 18 21 ...
$ hrs_0to1 : num 24.95 24.13 21.39 7.05 5.25 ...
$ hrs_1to6 : num 9.3 24.63 32.03 3.49 8.11 ...
$ hrs_6to12 : num 19.5 15.13 23.68 2.88 6.96 ...
$ hrs_12to24 : num 6.02 11.35 18.07 3.15 6.12 ...
$ hrs_24to48 : num 1.43 7.94 7.41 2.45 5.01 1.32 2.71 0.23 0 2.03 ...
$ hrs_48to96 : num 0.27 6.29 3.31 1.76 3.56 0.76 2.16 0.12 0 1.67 ...
$ Call_Before_Order: int 0 0 0 100 0 0 0 0 0 0 ...
数据示例
> dput(head(sum1, 20))
structure(list(TicketType = structure(c(1L, 12L, 23L, 32L, 33L,
34L, 35L, 36L, 37L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
13L), .Label = c("Dummy-1", "Dummy-10", "Dummy-11", "Dummy-12",
"Dummy-13", "Dummy-14", "Dummy-15", "Dummy-16", "Dummy-17", "Dummy-18",
"Dummy-19", "Dummy-2", "Dummy-20", "Dummy-21", "Dummy-22", "Dummy-23",
"Dummy-24", "Dummy-25", "Dummy-26", "Dummy-27", "Dummy-28", "Dummy-29",
"Dummy-3", "Dummy-30", "Dummy-31", "Dummy-32", "Dummy-33", "Dummy-34",
"Dummy-35", "Dummy-36", "Dummy-37", "Dummy-4", "Dummy-5", "Dummy-6",
"Dummy-7", "Dummy-8", "Dummy-9"), class = "factor"), hrs_0to1 = c(4.04,
21.39, 0.14, 24.95, 0, 0, 0, 0, 0, 0.02, 0, 3.95, 0, 0, 24.13,
0.23, 0, 2.05, 0, 1.28), hrs_1to6 = c(3.08, 32.03, 0.18, 9.3,
0, 0.06, 0, 0, 0, 0, 0, 3.61, 0.06, 0, 24.63, 4.68, 0, 0.18,
0, 2.25), hrs_6to12 = c(3.06, 23.68, 0.28, 19.5, 0.09, 0.09,
0, 0.19, 0, 0.37, 0, 5.94, 1.3, 0, 15.13, 10.31, 0, 0.46, 0,
2.23), hrs_12to24 = c(2.5, 18.07, 0.09, 6.02, 0.05, 0.32, 0,
0.46, 0, 1.71, 0, 4.17, 10.15, 0.09, 11.35, 13.16, 0, 0.05, 0.05,
3.43), hrs_24to48 = c(1.32, 7.41, 0.11, 1.43, 0.08, 0.64, 0.08,
1.99, 0, 4.74, 0.08, 2.71, 12.83, 0.08, 7.94, 12.68, 0, 0, 0,
6.06), hrs_48to96 = c(0.76, 3.31, 0.24, 0.27, 0, 2.25, 0.27,
1.46, 0.09, 5.38, 0.06, 2.16, 10, 0.09, 6.29, 12.25, 0.06, 0,
0, 6.84), hrs_above96 = c(0, 1.97, 0, 0, 0, 3.95, 0.66, 3.29,
0, 3.29, 0, 0.66, 8.55, 0, 0.66, 14.47, 0, 0, 0, 11.84)), .Names = c("TicketType",
"hrs_0to1", "hrs_1to6", "hrs_6to12", "hrs_12to24", "hrs_24to48",
"hrs_48to96", "hrs_above96"), row.names = c(NA, 20L), class = "data.frame")