应用程序启动时,最初无法呈现闪亮的单选按钮

时间:2017-10-15 09:55:48

标签: r shiny

我正在做一些时间序列分析,并创建了一个闪亮的应用程序,当应用程序启动时,样本时间序列数据被上传或用户可以从他的本地目录上传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'
                    ))
    }

  })

})

我面临的问题是,一旦执行闪亮的应用程序,样本数据将正确加载,因为此数据放在目录中的app文件夹中(可以使用R内置数据集或使用我给出的数据在开始时),随后所有步骤都正确执行。

但是如果我想从本地目录上传一些其他csv文件,即使选择了上传按钮选项也不会被激活。但实际上,如果进入侧边栏面板中的第二个菜单项即过滤器选项卡并单击过滤器按钮(在“过滤器”框下)然后如果我再次返回侧栏面板中的“数据”菜单,我可以看到现在我的上传csv文件按钮已激活,现在我可以浏览本地目录中的csv文件并将其上传到应用程序,现在一切正常。

在应用程序打开时,似乎某个条件使得上传文件按钮最初没有激活....

需要帮助来解决问题...很抱歉发布大量代码....

1 个答案:

答案 0 :(得分:1)

conditionalPanelsubmitButton无法很好地协同工作。将submitButton("Filter")替换为actionButton("Filter", "")

修改

根据评论,对于仅在点击actionButton后生成的情节,您可以将output$dataChartFiltered放在observeEvent Filter isolateobserveEvent(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' )) } }) }) 对于`输入对象如下:

import java.util.Scanner; 
class Test{ 
public static void main(String args[]){
Scanner sc=new Scanner(System.in); 
 System.out.println("Pick option:\n 1-Option one\n 2-Option two");
 int x= sc.nextInt();
if (x=1);
{
System.out.println("You select option one");
}
else if(x=2);{
        System.out.println("You select option two");
}
else{
System.out.println("Please select option one or two by typing 1 or 2");}                 

} 
}