我正在尝试创建一个关于突尼斯选举的闪亮应用程序,我把政党作为一个小部件,所以用户可以选择一个或多个政党,应用程序将显示每个政党的选票数量的绘图,我已经成功地选择了一个政党的情况下,但当用户选择一个以上,它失败了:(下面是关于情节的代码R:
selectizeInput("parti", label = "Parti politique", choices= levels(data$Q99), selected = "Nidaa Tounes",multiple=TRUE)
#server.R
library(shiny)
library(Rcmdr)
library(ggplot2)
library(ggalt)
data <- readXL("C:/Users/boti/Desktop/regression/bfinal.xlsx",rownames=FALSE, header=TRUE, na="NA", sheet="imp", stringsAsFactors=TRUE)
shinyServer(function(input, output) {
dataa<-reactive({as.data.frame(data)})
#Parti politique
partii=reactive({
as.character(input$parti)
})
output$plot1=renderPlot({
n=as.data.frame(table(dataa()[,95]))
n$Var1[n$Var1==partii()]
ggplot(n, aes(x =n$Var1[n$Var1==as.list(partii())] , y = n$Freq[n$Var1==as.list(partii())])) + geom_bar(stat = "identity", fill="Orange") + labs(title="Vote") +labs(x="Partis Politiques", y="Nombre de votes")
})
})
这个例子使用mpg数据集来做我猜你想做的事情。使用selectize输入,用户选择一个或多个制造商,条形图显示所选制造商的汽车数量:
ui。R
library(shiny)
library(ggplot2)
shinyUI(fluidPage(
titlePanel("Example"),
fluidRow(
column(3,
selectizeInput("mpgSelect",
label = "Select manufacturer",
choices = levels(as.factor(mpg$manufacturer)),
selected = "audi",
multiple=TRUE))),
fluidRow(
mainPanel(plotOutput('coolplot'),width = '40%'))
))
服务器。R
library(shiny)
library(ggplot2)
library(dplyr)
shinyServer(function(input, output) {
mpgSubset <- reactive({
validate(
need(input$mpgSelect != "", 'Please choose at least one feature.')
)
filter(mpg, manufacturer %in% input$mpgSelect)
})
output$coolplot<-renderPlot({
gg <- ggplot(mpgSubset(), aes(x = manufacturer, y = ..count..))
gg <- gg + geom_bar()
print(gg)
})
})