2017-01-03 16 views
1

我已經使用從csv文件讀取的數據創建了barplot。Barplot在Shiny中顯示1 bar太少和奇怪的細線

最初創建一個過濾器函數來過濾來自RGui的數據並將其應用於barplot()中。 它能夠過濾出數據列表並顯示5個預期的小節。

添加相同的barplot(多個代碼更改),barplot將始終顯示1個較少的條,並且還會在非常細的條中顯示其餘數據。

(見下圖)

Server.R

slt <- read.csv("data/slt_inc.csv") 
#extract only the number column 
slt_vec <- c(slt$num_of_inc) 
#extract only the date column 
slt_date <- c(slt$date) 

output$incidentPlot <- renderPlot({barplot(slt_vec, names.arg=slt$date, main="SLT Incidents", xlab="Date", ylab="# of Incidents", col="green", as.Date(slt_date, origin="2016-10-24") >= input$dateRange[1] & as.Date(slt_date, origin="2016-10-24") <= input$dateRange[2])}) 

ui.R

dateRangeInput("dateRange", "Date Selection: ", start="2016-10-24", end=NULL, format="yyyy-mm-dd", min="2016-10-24", language = "en", width="100%"), 
fluidRow(column(6, verbatimTextOutput("date"))), 

任何想法如何解決這個問題,?

另一個可能相關的問題。

使用另一時間範圍12月28日至12月30日

CSV文件數據

  • 2016年12月28日,星期三,2
  • 2016年12月29日,星期四,1
  • 2016-12-30,週五,1

它應該顯示至少2條或期望3,但一旦我篩選12月28日至12月30日什麼都沒有顯示。

enter image description here

+0

嘗試刪除'origin'語句。他們不代表你的想法。也可以嘗試過濾數據框中的日期,然後提取後面繪製的列。這很容易。另外你的'c(..)'是多餘的,'c(..)'構造不是這樣使用的。 –

+0

儘管這是一個有趣的故事,但是我太困了,無法追查下來,弄清楚爲什麼會發生這種情況。 –

+0

最初我在as.Date()中沒有「origin」參數。 錯誤消息中提到了「必須提供原點」一行。一旦我添加了「原點」,它就開始工作。 我會過濾數據框中的日期。 至於C(...)我會認爲這將是更好的做法,而不是使用列名稱?所以在這種情況下,它就像slt $ num_of_inc。我對麼? 再次感謝。 – blitz

回答

2

好吧,兩件事情。首先,有趣的細線是因爲您打算用於子集數據的邏輯向量實際上被解釋爲barwidth(第二個未命名的參數)。例如,這barplot函數調用:

barplot(c(5,4,6,3,1,2,4), c(T,T,F,F,F,F,F)) 

獲取你這個情節:

enter image description here

更多信息,請參見?barplot

其次我僞造數據,並把一個小例子來告訴你我會怎麼處理這個任務:

library(shiny) 

u <- shinyUI(fluidPage(fluidRow(
     dateRangeInput("dateRange","Date Selection: ", 
         start = "2016-10-24",end = NULL, 
        format = "yyyy-mm-dd",min = "2016-10-24", 
        language = "en",width = "100%"), 
     fluidRow(column(6,verbatimTextOutput("date"))), 
     plotOutput('incidentPlot') 
))) 

s <- shinyServer(function(input,output) { 

    # fake up the data 
    dd1 <- c(24,25,26,28,28) 
    sltval1 <- c(5,1,6,7,2) 
    dayval1 <- as.Date(sprintf("2016-10-%2.2d",dd1)) 
    dd2 <- c(7,8,9,12,13,14,15,16,19,20,21,22,23,26,27,28,29,30) 
    sltval2 <- c(6,5,1,3,6,6,1,2,3,6,4,1,0,0,1,2,1,1) 
    dayval2 <- as.Date(sprintf("2016-12-%2.2d",dd2)) 
    slt <- data.frame(date = c(dayval1,dayval2),num_of_inc = c(sltval1,sltval2)) 
    slt$dow <- weekdays(slt$date) 
    # end of data fakery 

    output$date <- renderPrint({ print(input$dateRange) }) 

    output$incidentPlot <- renderPlot({ 
    dmin <- as.Date(input$dateRange[1]) 
    dmax <- as.Date(input$dateRange[2]) 
    bdf <- slt[ dmin<= slt$date & slt$date <= dmax, ] 
    barplot(bdf$num_of_inc,names.arg = bdf$date, col = "green", 
      main="SLT Incidents",xlab="Date",ylab="# of Incidents") 
    }) 
}) 
shinyApp(ui = u,server = s) 

enter image description here

最後,你可能需要如何使用更多的理解和子集數據框和向量,如果您要輕鬆編寫有效的R代碼。 R的數據處理和子集化方法與其他語言不同,期望您可以在沒有一點研究的情況下進行挑選。

例如,我會推薦仔細閱讀至少一些在線書籍Advanced-R,尤其是關於數據結構和子集的兩個開始章節 - 或者其他等效的東西。

+0

它看起來很有效。我將需要對我的代碼進行一些修改。謝謝您的幫助。目前學習更多關於R. – blitz