【问题标题】:Shiny radio button not getting rendered initially when the app starts应用程序启动时,闪亮的单选按钮最初未呈现
【发布时间】:2017-10-15 09:55:47
【问题描述】:

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

  })

})

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

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

似乎某处导致上传文件按钮在应用打开时最初没有激活......

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

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    conditionalPanelsubmitButton 不能很好地协同工作。将您的 submitButton("Filter") 替换为 actionButton("Filter", "")

    编辑:

    根据评论,要仅在单击 actionButton 后生成绘图,您可以将 output$dataChartFiltered 放入 FilterobserveEventisolate 中,用于`输入对象如下:

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

    【讨论】:

    • 成功了...非常感谢您的宝贵见解!
    • 我的过滤器操作按钮似乎是多余的......它甚至在点击它之前渲染......点击过滤器操作按钮后应该进行渲染......如何实现?
    • 对不起,我听不懂,能详细解释一下吗?
    • 或者您可以将output$dataChartFiltered 放在FilterobserveEvent 中,isolate 用于input 对象。
    • @Nishant,我已经编辑了我的答案以显示它是如何完成的。希望对您有所帮助!
    猜你喜欢
    • 2017-10-30
    • 2018-09-05
    • 2018-09-24
    • 1970-01-01
    • 1970-01-01
    • 2021-05-01
    • 2022-01-03
    • 1970-01-01
    • 2019-08-02
    相关资源
    最近更新 更多