2015-03-25 91 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

相關問題