2015-03-25 43 views
2

在我闪亮的应用程序中,我想更改我想构建的ggplot barChart。 selectinput应该允许更改月份(请参阅下面的数据集),因此我的情节应该相应地改变。无法在我闪亮的应用程序中使用反应元素

问题:isssue是,我无法使用我的反应函数,甚至只是在ggplot函数中使用简单的input$monthid

数据集:

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") 

    }) 
}) 
+0

它说:'错误:未定义列selected' – Shoaibkhanz 2015-03-27 12:34:02

+0

我相信这个问题是我的'$输入'monthid是行而不是列。 – Shoaibkhanz 2015-03-27 12:37:24

+0

我创建了2个反应元素,它可以工作,但情节不是按比例缩放的,你可以重现这一点,并尝试'm <-reactive(c <-as.data.frame(b [b $ Month == input $ monthid, ]) ç }) ORD <反应性({ 或<-m() 或<-as.data.frame(或[2]) }) 输出$情节< - renderPlot( { #可能我正在做一个子集错误在x中的aes参数 ggplot(data = b,aes_string(x = m(),y = ord()))+ geom_bar(stat =“identity”)' – Shoaibkhanz 2015-03-27 12:47:59

回答

1

这里有一个最低工作前充足的,你可以复制和粘贴就在你的会话中运行,但以单杆柱状图并没有真正做了很大的意义(与长相丑陋,如果你问我):

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") 
    }) 
    } 
) 

它看起来有点像这样:

因为这并不好看海事组织,你可以做突出当前选择的酒吧的东西,例如:

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") 
    }) 
    } 
) 

这将如下所示:

+0

我知道最初的图表看起来真的很丑,但我试图看看反应元素是如何工作以产生情节的,我仍在学习,谢谢你的精炼p很多,我很喜欢它。 – Shoaibkhanz 2015-03-27 14:26:08

相关问题