Selected file to graph

Hi,

I have a shiny app in which I can choose the file to graph, and 2 columns.
The code in my UI is :

                selectInput("graphe_fichier_choix",
                             label = "Choix du fichier de données",
                             choices = c("Météo 1 min", "Météo 1 h", "Disdromètre 1 min"), 
                             selected = "Météo 1 min"),
                 box(title = "Variable 1",
                     width = NULL,
                     background = "olive",
                     selectInput("Graphe_var1",
                                    label = "Choix",
                                    choices = "", 
                                    selected = ""
                                    ),
                     checkboxInput("Esc_var1", label = "Escalier", value = FALSE)
                     ),
                 box(title = "Variable 2",
                     width = NULL,
                     background = "blue",
                     selectInput("Graphe_var2",
                                  label = "Choix",
                                  choices = "", 
                                  selected = ""),
                     checkboxInput("Esc_var2", label = "Escalier", value = FALSE)
                     )

And in my SERVER :

#Selection du fichier a partir du menu deroulant
if(input$graphe_fichier_choix == "Météo 1 min"){
    df <- select_rea()
} else if(input$graphe_fichier_choix == "Météo 1 h"){
    df <- select_rea_Heure()
} else if(input$graphe_fichier_choix == "Disdromètre 1 min"){
    df <- select_Parsivel_min_rea()
}

#On definit d'abord les choix des menus déroulants
choix <- colnames(df)
choix <- choix[-1:-2] #on enleve les 2 premières colonnes
  updateSelectInput(session, 
                  "Graphe_var1", 
                  choices = choix, 
                  selected = head(choix,3)
)
updateSelectInput(session, 
                  "Graphe_var2", 
                  choices = choix, 
                  selected = head(choix,4)
)
  })

# traçage du graphe
  output$DygraphExplore <- renderDygraph({
if(input$graphe_fichier_choix == "Météo 1 min"){
  df <- select_rea()
} else if(input$graphe_fichier_choix == "Météo 1 h"){
  df <- select_rea_Heure()
} else if(input$graphe_fichier_choix == "Disdromètre 1 min"){
  df <- select_Parsivel_min_rea()
}

  data1_xts <- prepadata_xts(df,input$Graphe_var1)
  data2_xts <- prepadata_xts(df,input$Graphe_var2)

colnames(data1_xts) <- as.character(input$Graphe_var1)      #Premier graphe
colnames(data2_xts) <- as.character(input$Graphe_var2)      #Deuxième graphe
data_xts <- cbind(data1_xts,data2_xts)                      #Les 2 courbes sur le même graphe


#création dygraph
graphe <- dygraph(data_xts, main = "", ylab = "", group = "toto") %>%
  #dyRangeSelector(dateWindow = selectionrange) %>%
  dySeries(input$Graphe_var1, axis = "y", stepPlot = input$Esc_var1, color="green")%>%
  dySeries(input$Graphe_var2, axis = "y2", stepPlot = input$Esc_var2, color ="blue")%>%
  dyOptions(useDataTimezone = TRUE,fillGraph = input$fillgraph,drawGrid = input$showgrid,
            retainDateWindow = TRUE,
            mobileDisableYTouch = TRUE)%>%
  dyEvent(x= event_debut, label = event_type)%>%
  dyAxis("x",
         valueFormatter = 'function(ms) { return moment(ms).format("YYYY-MM-DD hh:mm "); }'
  )%>%
  dyLegend(labelsSeparateLines= br())%>%
  dyRangeSelector()

return(graphe)

When I change the file to graph, I have this error message :
Warning: Error in : Can't subset columns that don't exist.
Which is normal as column names are not the same in the files.
The graph is finally plotted, but how can I avoid this ?

Thanks for your answers.

add conditional logic (i.e. if else) to check that the columns you would operate on exist, and if they don't skip out of the function early.

Thank you for your answer.
I thought about that, but then, the graph is never displayed...

is that because you never have the columns needed ? why not ?
maybe address that...

When I select the first file, 2 columns are selected by default (the 3rd and 4th).
When I select another file, these 2 columns have not the same name.
So during the load of the new column name , the graph is not display immediately, and I have the error message Warning: Error in : Can't subset columns that don't exist. x Column AirTemp_Avg doesn't exist.
At the end, it works, but I wish I didn't get this message.

Seems like there is a lack of understanding is to how it eventually works ?

You can try the shiny debugging guide and use debug() or browser() to research you application yourself.
Otherwise if you would like support from the forum you are encouraged to make a reprex.

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.