R Shiny App,$< - 。data.frame中的错误:替换有1行,数据有0

时间:2018-01-25 17:39:14

标签: r shiny

我是R和Shiny的新手。我正在编写一个R Shiny应用程序,它从特定格式的数据文件创建时间序列数据的数据框。加载数据文件后,数据文件的内容显示在"文件内容"标签。选择工作站后,时间序列将绘制在" Chloride Time Series"标签。该脚本应该允许操作数据框(因此可以通过单击或刷涂图中的点来排除或包括分析中所需的多个点)。

不幸的是,点击图表后,应用程序崩溃了,我收到了这个神秘的错误:警告:$< - 。data.frame:replace有1行,数据有0 堆栈跟踪(最里面的第一个):     68:$< - .data.frame     67:$< -     66:nearPoints     65:observeEventHandler [C:\ Users \ bp \ OneDrive \ blah blah blah]      1:runApp 错误:[on_request_read]连接由同行重置

该错误源自代码,以排除我几乎逐字逐句从工作应用程序中提取的数据。

完整代码如下:

        #
#
#check for packages and install if not installed
packages <- c("shiny", "lubridate", "shinyFiles", "readtext","ggplot2", "reshape2", "dplyr")
if (length(setdiff(packages, rownames(installed.packages()))) > 0)
{
  install.packages(setdiff(packages, rownames(installed.packages())))  
}
#require packages above
require("lubridate")
require("shinyFiles")
require("readtext")
require("shiny") 
require("ggplot2")
require("reshape2")
require("dplyr")

# Define UI for application that reads the file and displays the details of the file that was uploaded
ui <- fluidPage(headerPanel("Seasonal Mann-Kendall Regression and Linear Regression Tests"),

                  sidebarLayout(
                                  sidebarPanel(
                                                fileInput("file", 
                                                          "Browse to file",
                                                          multiple=TRUE,
                                                          accept=c("text/csv","text/comma-serparated-values,text/plain",".csv", ".txt"),
                                                          placeholder ="No file selected"
                                                          ), #end shinyFilesButton
                                                          helpText("Max file size is 5 MB"), 

                                                #check box in case data is loaded with a header
                                                checkboxInput("header","Header",FALSE),


                                                selectInput("stations","Select Stations:  ",
                                                                      choices =  NULL,#doesn't know what the station names are yet.
                                                                      selected = NULL,
                                                                      multiple = FALSE,#to not select multiple stations
                                                                      selectize = TRUE #will bring up a list of stations to select
                                                            )#end selectInput  


                                              ),#end side bar panel

                                  #will display data in file when the file is read in
                                  mainPanel(
                                            tabsetPanel(id = "inTabset",
                                                        tabPanel(title = "File Contents", tableOutput("contents")),
                                                        tabPanel(title = "Chloride Time Series", 
                                                                 plotOutput("plot1", 
                                                                            click = "plot1_click",
                                                                              brush = brushOpts(
                                                                              id = "plot1_brush"
                                                                              )#end brushOpts
                                                                            ),#end plotouput
                                                                 actionButton("exclude_toggle", "Toggle Points"),
                                                                 actionButton("exclude_reset", "Reset")                                            

                                                                 )#end tab panel

                                                        )#end of tabsetpanel
                                            )#end main panel

                              )#end sidebarlayout

                )#end the fluidpage ui


server <- function(input, output, session) {



          output$contents <-renderTable({
      #Step 1: SELECT THE TEXT FILE
                                    inFile <-input$file

                                    if (is.null(inFile))
                                      return(NULL)

                                    read.table(inFile$datapath,
                                               header = input$header,
                                               sep="",
                                               quote="",
                                               dec = "."

                                              )#end read.table


                                        })#end of output$contents assignment

            #for the update select input invoke observe function
                        observe({

                          #define cupstat to be the first column of the file data, not the data frame

                          x<-input$file
                          if (is.null(x))
                            return(NULL)

                          #read in the table and store it in a variable named datTable
                          datTable<-read.table(x$datapath, 
                                                header = FALSE,
                                                sep = "",
                                                dec = "."
                                              )#end read.table

                              #get the station IDs             
                              cupStat<-unique(datTable[,1]
                                             )#end unique   



                               #update the select input called STATIONs  
                              updateSelectInput(session,"stations",
                              label = "Select Station",
                              selected = input$stations,#assign the value selected by the user to this select input, otherwise the app will revert back to NULL
                              choices = cupStat

                                               )#end updateselectinput


                              #UPDATE the CHLORIDE TIME SERIES PLOT OUTPUT TAB TITLED "CHLORIDE TIME SERIES" 
                                #STEP 2:  GET A DATA FRAME FROM datTable that corresponds to the station selected in 
                                #the selectInput station
                                stationData<-filter(datTable,datTable[,1]==input$stations)

                                #STEP3:  STRIP THE DATES AND THE CHLORIDE CONCENTRATIONS FROM THAT DATA FRAME TO MAKE A another data frame
                                xCl<-ymd(stationData[,3], locale = "English") #dates
                                yCl<-stationData[,4] #chloride concentration

                                chlorideTS<-data.frame(xCl,yCl)


                                ##############################################################################################
                                #for storing which rows from chlorideTS have been excluded


                                vals <- reactiveValues(
                                  keeprows = rep(TRUE, nrow(chlorideTS))
                                )

                                output$plot1 <- renderPlot({
                                  # Plot the kept and excluded points as two separate data sets
                                  keep    <- chlorideTS[ vals$keeprows, , drop = FALSE]
                                  exclude <- chlorideTS[!vals$keeprows, , drop = FALSE]

                                  ggplot(keep, aes(chlorideTS[,1], chlorideTS[,2])) + geom_point() +
                                    geom_smooth(method = lm, fullrange = TRUE, color = "black") +
                                    geom_point(data = exclude, shape = 21, fill = NA, color = "black", alpha = 0.25)

                                })
                                #***********************************Error Message likely derived from the code below**************************
                                # source for code below:  https://shiny.rstudio.com/gallery/plot-interaction-exclude.html

                                # Toggle points that are clicked
                                observeEvent(input$plot1_click, {
                                  res <- nearPoints(chlorideTS, input$plot1_click, allRows = TRUE)

                                  vals$keeprows <- xor(vals$keeprows, res$selected_)
                                })

                                # Toggle points that are brushed, when button is clicked
                                observeEvent(input$exclude_toggle, {
                                  res <- brushedPoints(chlorideTS, input$plot1_brush, allRows = TRUE)

                                  vals$keeprows <- xor(vals$keeprows, res$selected_)
                                })

                                # Reset all points
                                observeEvent(input$exclude_reset, {
                                  vals$keeprows <- rep(TRUE, nrow(chlorideTS))
                                })



                                })#end observe for updateSelectInput


                                }#end of server function


shinyApp(ui, server)

我怀疑错误是由一个在观察函数中的反应性表达引起的,但是,我没有证据表明情况就是这样。无论如何,如果有人对此代码有任何建议,我会很高兴听到他们。谢谢。

0 个答案:

没有答案