简体   繁体   中英

$ operator is invalid for atomic vectors in shiny using dygraph and reading several data

I am having this trouble with the invalid atomic vector when I want to plot a graph with dygraph in R Shiny. The inputs can be selected by SelectInput and the files are CSV saved in my directory. This is basically the connection between UI and Server that I am trying to make it work.

I have read through the answer and I know it is an error from R that it´s not well tagged, that's why I haven't figure out how to solve it.

ui <- dashboardPage(
  fluidPage(

      fluidRow(
          column(3,
               "sidebar",

               fluidPage(
                 wellPanel(height =500),
                 box(selectInput("period", "Choose Year:",
                                     c("2006" = "2006", 
                                       "2007" = "2007",
                                       "2008" = "2008",
                                       "2009" = "2009"
                                       )),
                     width=NULL),

                wellPanel(height =300)
                 )
          ),

        column (9,
               "main",
           fluidPage(
                wellPanel(width = "100%", height = "100%",
                  dygraphOutput("PerYear")
                 )
               )
          )
      )


server <- function(input, output, session) {
 output$PerYear <- renderDygraph({

reading_csv <- function (year) {

    csvpath <- "./xts_folder/"

     y <-read.csv(paste0(csvpath,year,"_RW.csv"),header=TRUE, sep=",")
     ytime <- mdy_hm(y[["timestampUTC"]], tz="UTC")
     y_xts <- xts(y, order.by = ytime)
     ts.sn.year<-dygraph(y_xts)%>% dyRangeSelector()
      return(ts.sn.year)
   }

  if(input$period == "2006" ){
     y<-2006
     pyear<-reading_csv(year = y) 
     pyear
    }

  else if(input$period == "2007" ){
    y<-2007
    pyear<-reading_csv( year = y) 
    pyear
  }

  else if(input$period == "2008" ){
    y<-2008
    pyear<-reading_csv(year=y) 
    pyear

  }
   else if(input$period == "2009" ){
     y<-2009
     pyear<-reading_csv(year=y) 
     pyear
   }   

  }
  )
}

Maybe there are some mistakes because I erased the rest of the code, but the main part is that connection for Render and Output of the dygraph. The CSV file may look like this:

timestampUTC,max
1/1/2006 0:50,0.7
1/1/2006 1:50,6.4
1/1/2006 2:50,7.5
1/1/2006 3:50,0.3
1/1/2006 4:50,0
1/1/2006 5:50,0.2
1/1/2006 6:50,0.7
1/1/2006 7:50,1.5
1/1/2006 8:50,0.5
1/1/2006 9:50,0.4
1/1/2006 10:50,0.3
1/1/2006 11:50,1.6
1/1/2006 12:50,0.7
1/1/2006 13:50,1.6
1/1/2006 14:50,0.6
1/1/2006 15:50,0.2
1/1/2006 16:50,0.4
1/1/2006 17:50,0.7

For example, and as you may see from the code, they are stored with the same name format: 2006_RW, 2007_RW, etc.

I would really appreciate any help or glimpse, I've tried so many stuff. Thank you!!!

Welcome to SO!

I cleaned you UI up a little.

Please check the following:

# Create files ------------------------------------------------------------

csvpath <- "./xts_folder/"

if(!dir.exists(csvpath)){
  dir.create(csvpath)
}

DF <- data.frame(stringsAsFactors=FALSE,
                 timestampUTC = c("1/1/2006 0:50", "1/1/2006 1:50", "1/1/2006 2:50",
                                  "1/1/2006 3:50", "1/1/2006 4:50", "1/1/2006 5:50",
                                  "1/1/2006 6:50", "1/1/2006 7:50", "1/1/2006 8:50", "1/1/2006 9:50",
                                  "1/1/2006 10:50", "1/1/2006 11:50", "1/1/2006 12:50",
                                  "1/1/2006 13:50", "1/1/2006 14:50", "1/1/2006 15:50",
                                  "1/1/2006 16:50", "1/1/2006 17:50"),
                 max = c(0.7, 6.4, 7.5, 0.3, 0, 0.2, 0.7, 1.5, 0.5, 0.4, 0.3, 1.6,
                         0.7, 1.6, 0.6, 0.2, 0.4, 0.7)
)
DF
write.csv(DF, file = file.path(csvpath, "/2006_RW.csv"), row.names = FALSE, quote = FALSE)

DF$timestampUTC <- gsub("2006", "2007", DF$timestampUTC)
DF$max <- runif(18)
write.csv(DF, file = file.path(csvpath, "/2007_RW.csv"), row.names = FALSE, quote = FALSE)

DF$timestampUTC <- gsub("2007", "2008", DF$timestampUTC)
DF$max <- runif(18)
write.csv(DF, file = file.path(csvpath, "/2008_RW.csv"), row.names = FALSE, quote = FALSE)

DF$timestampUTC <- gsub("2008", "2009", DF$timestampUTC)
DF$max <- runif(18)
write.csv(DF, file = file.path(csvpath, "/2009_RW.csv"), row.names = FALSE, quote = FALSE)


# App ---------------------------------------------------------------------

library(shiny)
library(shinydashboard)
library(dygraphs)
library(lubridate)
library(xts)

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(selectInput(
      "period",
      "Choose Year:",
      c(
        "2006" = "2006",
        "2007" = "2007",
        "2008" = "2008",
        "2009" = "2009"
      )
    )
  ),
  dashboardBody(
    wellPanel(
      width = "100%",
      height = "100%",
      dygraphOutput("PerYear")
    )
  )
)

server <- function(input, output, session) {
  output$PerYear <- renderDygraph({
    reading_csv <- function (year) {
      csvpath <- "./xts_folder/"

      y <- read.csv(paste0(csvpath, year, "_RW.csv"), header = TRUE, sep = ",")
      ytime <- mdy_hm(y[["timestampUTC"]], tz = "UTC")
      y_xts <- xts(y, order.by = ytime)
      ts.sn.year <- dygraph(y_xts) %>% dyRangeSelector()
      return(ts.sn.year)
    }

    if (input$period == "2006") {
      y <- 2006
      pyear <- reading_csv(year = y)
      pyear
    }

    else if (input$period == "2007") {
      y <- 2007
      pyear <- reading_csv(year = y)
      pyear
    }

    else if (input$period == "2008") {
      y <- 2008
      pyear <- reading_csv(year = y)
      pyear

    }
    else if (input$period == "2009") {
      y <- 2009
      pyear <- reading_csv(year = y)
      pyear
    }
  })
}

shinyApp(ui, server)

However, I would' recommend reading in the csv file everytime you want to update the plot. It would be better to read them in only once they are needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM