2017-10-15 4 views
0

私はいくつかのtimeseries解析を行い、アプリがサンプルを開始すると、timeseriesデータがアップロードされるか、またはユーザーがローカルディレクトリからcsvデータセットをアップロードできる。アプリが起動したときに光沢のあるラジオボタンが最初にレンダリングされない

サンプルデータセット:

df 
     month passengers 
1 01-01-2000 2072798 
2 01-02-2000 2118150 
3 01-03-2000 2384907 
4 01-04-2000 2260620 
5 01-05-2000 2386165 
6 01-06-2000 2635018 
7 01-07-2000 2788843 
8 01-08-2000 2942082 
9 01-09-2000 2477000 
10 01-10-2000 2527969 
11 01-11-2000 2161170 
12 01-12-2000 2175314 
13 01-01-2001 2307525 
14 01-02-2001 2196415 
15 01-03-2001 2545863 

library(signal) 
library(shiny) 
library(AnomalyDetection) #devtools::install_github("twitter/AnomalyDetection") 
library(ggplot2) 
# Define UI for application that draws a histogram 
library(shinydashboard) 
library(shinycssloaders) 
library(googleVis) 

shinyUI(dashboardPage(skin = "green", 

         dashboardHeader(title = "Anomaly Detection in Time series", 
             titleWidth = 350), 

         dashboardSidebar(
         sidebarUserPanel("Nishant Upadhyay", 
             image = "nishantcofyshop.jpg" 
         ), 

         sidebarMenu(
          menuItem("Data", tabName = "data", icon = icon("database")), 
          menuItem("Filters", tabName = "filter", icon = icon("filter")), 
          menuItem("Anomalies", tabName = "anomaly", icon = icon("check")), 
          #menuItem("Save Data", tabName = "save", icon = icon("save")) 
          menuItem("About The App", tabName = "Help", icon = icon("info-circle")) 
         ) 
        ), 


         dashboardBody(
         tabItems(

          tabItem(tabName = "data", 

            fluidRow(
            box(
             title = "Data scatter Chart", 
             status = "primary", 
             solidHeader = T, 
             collapsible = T, 
             width = 12, 
             shinycssloaders::withSpinner(htmlOutput("dataChart"),type = getOption("spinner.type", default = 8),color = "red") 
            ) 

           ), 

            fluidRow(
            box(

             radioButtons(
             "data_input","", 
             choices = list("Load sample data" = 1, 
                 "Upload csv file" = 2 
             ) 
            ), 

             conditionalPanel(
             condition = "input.data_input=='1'", 
             h5("Sample dataset of Lebron James basketball shots over the years") 
            ), 

             conditionalPanel(
             condition = "input.data_input=='2'", 
             fileInput('file1', 'Choose file to upload', 
                accept = c(
                'text/csv', 
                'text/comma-separated-values', 
                'text/tab-separated-values', 
                'text/plain', 
                '.csv', 
                '.tsv' 
               )), 
             checkboxInput('header', 'Header', TRUE), 
             radioButtons('sep', 'Separator', 
                c(Comma=',', 
                 Semicolon=';', 
                 Tab='\t'),','), 
             radioButtons('quote', 'Quote', 
                c('None'='', 
                 'Double Quote'='"', 
                 'Single Quote'="'"), 
                '') 
            ), 
             title = "Select Dataset", 
             status = "info", 
             solidHeader = T, 
             collapsible = T 
            ), 

            box(
             title = "Data", 
             status = "info", 
             solidHeader = T, 
             collapsible = T, 
             shinycssloaders::withSpinner(htmlOutput('contents'),type = getOption("spinner.type", default = 8),color = "red") 
            )# end of box 

           )## end of Fluid row 

         ), ## end of tab item 

          tabItem(
          tabName = "filter", 
          fluidRow(
           box(
           title = "Data Chart", 
           status = "primary", 
           solidHeader = T, 
           collapsible = T, 
           width = 12, 
           shinycssloaders::withSpinner(htmlOutput('dataChartFiltered'),type = getOption("spinner.type", default = 8),color = "red") 
          ) 
          ), 


          fluidRow(
           box(
           title = "Filters", 
           status = "info", 
           solidHeader = T, 
           collapsible = T, 
           width = 4, 
           radioButtons("filt", NULL, 
              c("None" = "none", 
               "Butterworth" = "butt", 
               "Type-II Chebyshev" = "cheby2")), 
           submitButton("Filter") 
          ), 
           box(
           title = "Butterworth", 
           status = "info", 
           solidHeader = T, 
           collapsible = T, 
           width = 4, 
           textInput("buttern", label = "Filter Order", value = "3"), 
           textInput("butterf", label = "Critical Frequencies", value = "0.1"), 
           radioButtons("buttert", "Type", 
              c("Low-Pass" = "low", 
               "High-Pass" = "high")) 
          ), 
           box(
           title = "Chebyshev", 
           status = "info", 
           solidHeader = T, 
           collapsible = T, 
           width = 4, 
           textInput("chebyn", label = "Filter Order", value = "5"), 
           textInput("chebyd", label = "dB of Pass Band", value = "20"), 
           textInput("chebyf", label = "Critical Frequencies", value = "0.2"), 
           radioButtons("chebyt", "Type", 
              c("Low-Pass" = "low", 
               "High-Pass" = "high")) 
          ) 
          ) 
         ) 

         ) ## end of tab items 
        ) ## end of Dashboard 
) 
) 

shinyServer(function(input, output){ 

    dataframe<-reactive({ 
    if (input$data_input == 1) { 
     tab <- read.csv("df.csv",header = T,stringsAsFactors = F) 
    } else if (input$data_input == 2) { 
     inFile <- input$file1 
     if (is.null(inFile)) 
     return(data.frame(x = "Select your datafile")) 

     tab = read.csv(inFile$datapath, header = input$header, 
        sep = input$sep, quote = input$quote) 

    } 

    tt <- tryCatch(as.POSIXct(tab[,1]),error=function(e) e, warning=function(w) w) 
    if (is(tt,"warning") | is(tt,"error")) { 
     tab$Old = tab[,1] 
     tab[,1] = as.POSIXct(1:nrow(tab), origin = Sys.time()) 
    } else { 
     tab[,1] = as.POSIXct(tab[,1]) 
    } 

    tab 
    }) 

    output$dataChart <- renderGvis({ 
    if (!is.null(dataframe())) 
     gvisLineChart(dataframe()[,c(1,2)], xvar = colnames(dataframe())[1], yvar = colnames(dataframe())[2], 
        options = list(
         crosshair.trigger = 'selection', 
         enableInteractivity = TRUE, 
         hAxis.maxTextLines = 10, 
         tooltip.trigger = 'none' 
        )) 
    }) 

    output$contents <- renderGvis({ 
    if (!is.null(dataframe())) 
     gvisTable(dataframe(), 
       options = list(page='enable')) 
    }) 

    output$dataChartFiltered <- renderGvis({ 
    if (input$filt == "none") { 
     return(NULL) 
    } else if (input$filt == "butt") { 
     bf <- butter(as.numeric(input$buttern), as.numeric(input$butterf), type = input$buttert) 
     filtered = data.frame(timestamp = dataframe()[,1], 
          count = as.numeric(filter(bf, dataframe()[,2]))) 
     gvisLineChart(filtered, xvar = colnames(filtered)[1], yvar = colnames(filtered)[2], 
        options = list(
         crosshair.trigger = 'selection', 
         enableInteractivity = TRUE, 
         hAxis.maxTextLines = 10, 
         tooltip.trigger = 'none' 
        )) 
    } else if (input$filt == "cheby2") { 
     ch <- cheby2(as.numeric(input$chebyn), as.numeric(input$chebyd), 
        as.numeric(input$chebyf), type = input$chebyt) 
     filtered = data.frame(timestamp = dataframe()[,1], 
          count = as.numeric(filter(ch, dataframe()[,2]))) 
     gvisLineChart(filtered, xvar = colnames(filtered)[1], yvar = colnames(filtered)[2], 
        options = list(
         crosshair.trigger = 'selection', 
         enableInteractivity = TRUE, 
         hAxis.maxTextLines = 10, 
         tooltip.trigger = 'none' 
        )) 
    } 

    }) 

}) 

私が直面しています問題は、光沢のあるアプリが実行されると、このデータは、ディレクトリ内のアプリケーションフォルダに配置されたように、サンプルデータが正しくロードされていることである(1 Rの組み込みデータセットを使用するか、最初に与えたデータを使用することができます)、その後すべてのステップが適切に実行されます。

ローカルのディレクトリから他のcsvファイルをアップロードする場合は、アップロードボタンの選択が有効になりません。実際には、サイドバーパネルの2番目のメニュー項目、つまりフィルタ(フィルタボックスの下にある)フィルタボタンをクリックして、サイドバーパネルの[データ]メニューに戻ると、アップロード用のcsvファイルボタンがアクティブになり、ローカルのcsvファイルをブラウズできるようになりますディレクトリにアップロードし、アプリに同じものをアップロードして、すべて正常に動作します。

問題を整理するのに役立つ必要があります...コードの大きな塊を投稿して申し訳ありません

....アプリが開いたときに、アップロードファイルのボタンを作る条件が最初にアクティブになっていませんどこかにいるようです。 ...

答えて

1

conditionalPanelsubmitButtonはうまく機能しません。 submitButton("Filter")actionButton("Filter", "")に置き換えてください。

EDIT:コメントを1として

、唯一actionButton後に生成されるプロットのは、次のように入力オブジェクト `のためisolateFilterobserveEventoutput$dataChartFilteredを置くことができますクリックすると:

observeEvent(input$Filter,{ 
    output$dataChartFiltered <- renderGvis({ 
    if (isolate(input$filt) == "none") { 
     return(NULL) 
    } else if (isolate(input$filt) == "butt") { 
     bf <- butter(as.numeric(isolate(input$buttern)), as.numeric(isolate(input$butterf)), type = isolate(input$buttert)) 
     filtered = data.frame(timestamp = dataframe()[,1], 
          count = as.numeric(filter(bf, dataframe()[,2]))) 
     gvisLineChart(filtered, xvar = colnames(filtered)[1], yvar = colnames(filtered)[2], 
        options = list(
         crosshair.trigger = 'selection', 
         enableInteractivity = TRUE, 
         hAxis.maxTextLines = 10, 
         tooltip.trigger = 'none' 
        )) 
    } else if (isolate(input$filt) == "cheby2") { 
     ch <- cheby2(as.numeric(isolate(input$chebyn)), as.numeric(isolate(input$chebyd)), 
        as.numeric(isolate(input$chebyf)), type = isolate(input$chebyt)) 
     filtered = data.frame(timestamp = dataframe()[,1], 
          count = as.numeric(filter(ch, dataframe()[,2]))) 
     gvisLineChart(filtered, xvar = colnames(filtered)[1], yvar = colnames(filtered)[2], 
        options = list(
         crosshair.trigger = 'selection', 
         enableInteractivity = TRUE, 
         hAxis.maxTextLines = 10, 
         tooltip.trigger = 'none' 
        )) 
    } 

    }) 

}) 
+0

これはうまくいった...あなたの貴重な洞察に感謝します! – Nishant

+0

それは私のフィルタアクションボタンが冗長であるようです....それをクリックする前にレンダリング...レンダリングは、フィルタアクションボタンをクリックした後に起こるはずです...どのように達成することができますか? – Nishant

+0

申し訳ありませんが、私はあなたを理解していない、詳細に説明できますか? – SBista

関連する問題