无法在我的闪亮应用中使用反应元素 [英] Unable to use reactive element in my shiny app
问题描述
在我闪亮的应用程序中,我想更改我想构建的ggplot barChart。 selectinput
应该允许更改月份(请参阅下面的数据集),因此我的情节应该相应地改变。
问题:isssue是,我无法在ggplot函数中使用我的反应函数或甚至简单的输入$ monthid
。
数据集:
月份订单
1月2日984524
1月1151303
3月575000
> (b)
结构(清单(Month = c(Feb,Jan,Mar),Orders = c(984524L,
1151303L,575000L)),.Names = c Month,Orders),class =data.frame,row.names = c(NA,
-3L))
ui.R
library(shinythemes)
b <-read.csv(b.csv,header = TRUE,sep =,,stringsAsFactors = TRUE)
shinyUI(fluidPage(theme = shinytheme(flatly),
sidebarLayout(
sidebarPanel(
selectInput(inputId =monthid,label =Month,choices = b $ Month,selected = b $ Month [1])),
mainPanel(plotOutput(plot))
))
)
$
$ b server.R
library(shiny)
library(shinythemes)
library(ggplot2)
b <-read.csv(b.csv,header = TRUE,sep = ,,stringsAsFactors = TRUE)
shinyServer(函数(输入,输出){
#制作反应物体
m <-reactive({
as.character(输入$ monthid)
})
输出$ plot< - renderPlot ({
#可能我在aes参数
ggplot(data = b,aes(x = b [,m()],y = b $ ))+ geom_bar(stat =identity)
})
})
这是一个最小的工作示例,您可以在会话中正确复制和粘贴以运行,但带有单个栏的条形图并没有多大意义(如果你问我,看上去很丑):
库(闪亮)
shinyApp(
ui = fluidPage(
sidebarLayout(
sidebarPanel(
selectInput(
inputId =monthid,
label =Month,
choices = b $ Month,
selected = b $ Month [1]
)
),
mainPanel(plotOutput(plot))
)
) ,
server = funct ion(input,output){
DF < - reactive({
b [b $ Month == input $ monthid,,drop = FALSE]
})
output $ plot < - renderPlot({
ggplot(DF(),aes(x = Month,y = Orders))+
geom_bar(stat =identity)
})
}
)
看起来有点像这样:
因为这不会看起来不错的国际海事组织,你可以做一些突出显示当前选中的栏,例如:
b $ highlight< - factor 未选中,levels = c(未选中,选中))
shinyApp(
ui = fluidPage(
sidebarLayout(
sidebarPanel(
selectInput(
inputId =monthid,
label =Month,
choices = b $ Month,
selected = b $ Month [1]
)
),
mainPanel(plotOutput(plot))
)
),
server = function(input,output){
DF < - reactive({
b [b $ Month == input $ monthid,highlight]< ; - Selected
b
})
output $ plot < - renderPlot({
ggplot(DF(),aes(x = Month,y = Orders,fill =突出显示))+
geom_bar(stat =identity)
})
}
)
这看起来如下:
In my shiny app,I want to change the ggplot barChart that I wish to construct. selectinput
should allow to change the month (see dataset below) and so my plot should change accordingly.
problem: The isssue is, i am unable to use my reactive function or even just simple input$monthid
within ggplot function.
Dataset:
Month Orders
1 Feb 984524
2 Jan 1151303
3 Mar 575000
> dput(b)
structure(list(Month = c("Feb", "Jan", "Mar"), Orders = c(984524L,
1151303L, 575000L)), .Names = c("Month", "Orders"), class = "data.frame", row.names = c(NA,
-3L))
ui.R
library(shiny)
library(shinythemes)
b<-read.csv("b.csv",header=TRUE,sep=",",stringsAsFactors=TRUE)
shinyUI(fluidPage(theme= shinytheme("flatly"),
sidebarLayout(
sidebarPanel(
selectInput(inputId = "monthid", label = "Month",choices = b$Month,selected = b$Month[1])),
mainPanel(plotOutput("plot"))
))
)
server.R
library(shiny)
library(shinythemes)
library(ggplot2)
b<-read.csv("b.csv",header=TRUE,sep=",",stringsAsFactors=TRUE)
shinyServer(function(input, output) {
#making a reactive object
m<-reactive ({
as.character(input$monthid)
})
output$plot<- renderPlot({
#probably I am making a subset error in x inside aes parameter
ggplot(data = b, aes(x = b[,m()] ,y = b$Orders)) + geom_bar(stat="identity")
})
})
Here's a minimal working example you can copy and paste right in your session to run, but a bar chart with a single bar doesn't really make a lot of sense (and looks ugly if you ask me):
library(shiny)
shinyApp(
ui = fluidPage(
sidebarLayout(
sidebarPanel(
selectInput(
inputId = "monthid",
label = "Month",
choices = b$Month,
selected = b$Month[1]
)
),
mainPanel(plotOutput("plot"))
)
),
server = function(input, output) {
DF <- reactive({
b[b$Month == input$monthid, , drop = FALSE]
})
output$plot <- renderPlot({
ggplot(DF(), aes(x = Month, y = Orders)) +
geom_bar(stat = "identity")
})
}
)
It looks somewhat like this:
Since that doesn't look nice IMO, you could do something with highlighting the currently selected bar, for example:
b$highlight <- factor("Not Selected", levels = c("Not selected", "Selected"))
shinyApp(
ui = fluidPage(
sidebarLayout(
sidebarPanel(
selectInput(
inputId = "monthid",
label = "Month",
choices = b$Month,
selected = b$Month[1]
)
),
mainPanel(plotOutput("plot"))
)
),
server = function(input, output) {
DF <- reactive({
b[b$Month == input$monthid, "highlight"] <- "Selected"
b
})
output$plot <- renderPlot({
ggplot(DF(), aes(x = Month, y = Orders, fill = highlight)) +
geom_bar(stat = "identity")
})
}
)
This would look as follows:
这篇关于无法在我的闪亮应用中使用反应元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!