erreur app shiny

I tried to add a column in the results table called "besoin_fr," but it's giving me an error saying "Warning: Error in <-: attribute 'names' [34] must be of the same length as vector [33]."

 } else {
          colnames(new_results) <- c(
            "Date",
            paste("Temp\U00E9rature salle propre (Tlocal [\U00B0 C])"),
            paste("Humidit\U00E9 Relative salle propre (HRlocal [%])"),
            paste("Temp\U00E9rature de soufflage (T_souf [\U00B0 C])"),
            # "Temperature exterieure (T_ext [\U00B0C])",
            paste("Temp\U00E9rature reseau d'eau chaude (T_eau_ch [\U00B0 C])"),
            paste("Temp\U00E9rature reseau d'eau froide (T_eau_fr [\U00B0 C])"),
            paste("Besoin de chaleur ou de froid (Besoin [kWh])"),
            paste("Conso thermique chaud (conso_ch (kWh))"),
            paste("Conso thermique chaud par heure (conso_ch (kWh))"),
            paste("Conso gaz chaudi\U00E8re par heure (kWh)"),
            paste("Conso thermique froid (conso_fr (kWh))"),
            paste("Conso thermique froid par heure (conso_fr [kWh])"),
            paste("Conso \U00E9lec froid par heure [kWh]"),
            paste("Zones"),
            paste("Temp\U00E9rature de m\U00E9lange (T_mel [\U00B0 C])"),
            paste("Humidit\U00E9 relative du m\U00E9lange (HR_mel [%])"),
            paste("Quantit\U00E9 d'eau condens\U00E9", "e par heure (cond_eau [dm3])", sep = ""),
            paste("Quantit\U00E9 d'eau evapor\U00E9", "e par heure (conso_eau [dm3])", sep = ""),
            paste("Quantit\U00E9 d'eau evapor\U00E9", "e par heure zone1 (conso_eau_z1 [dm3])", sep = ""),
            paste("Conso \U00E9lec ventilateur par heure (conso_ventilateur [kWh])"),
            paste("D\U00E9bit d'air neuf (m_a_n [m3/h])"),
            paste("D\U00E9bit d'air repris (m_a_r [m3/h])"),
            paste("Consigne (Tmin [\U00B0 C])"),
            paste("Consigne (Tmax [\U00B0 C])"),
            paste("Consigne (HRmin [%])"),
            paste("Consigne (HRmax [%])"),
            paste("Consignes Apports (ConsApports [kWh])"),
            paste("r m\U00E9lange (r_mel [g eau/kg air sec])"),
            paste("Quantit\U00E9 cumul\U00E9", "e d'eau evapor\U00E9", "e (conso_eau [dm3])", sep = ""),
            paste("Quantit\U00E9 cumul\U00E9", "e d'eau condens\U00E9", "e (cond_eau [dm3])", sep = ""),
            paste("Quantit\U00E9 cumul\U00E9", "e d'eau evapor\U00E9", "e zone 1 (conso_eau_z1 [dm3])", sep = ""),
            paste("Conso \U00E9lec cumul\U00E9", "e ventilateur (conso_ventilateur [kWh])", sep = ""),
            paste("Conso cumul\U00E9", "e humidificateur (conso_humid [kWh])", sep = ""),
            paste("Besoin de froid zone8 (Besoin_fr [kWh])")
          )
        }

        Data_consigne0 <- Table_bilan0()$bilan_consigne0
        Data_consigne0$`Nombre d'heures` <- format(Data_consigne0$`Nombre d'heures`, digits = 9, decimal.mark = ".", big.mark = " ")
        Data_cons0 <- Table_bilan0()$bilan_conso0
        Data_cons0$Valeur <- format(Data_cons0$Valeur, digits = 9, decimal.mark = ".", big.mark = " ")
        Data_zone0 <- Table_bilan0()$bilan_zone0
        Data_zone1 <- Table_bilan0()$bilan_zone1
        Data_zone0$`Nombre d'heures` <- format(Data_zone0$`Nombre d'heures`, digits = 9, decimal.mark = ".", big.mark = " ")
        Data_zone1$`Nombre d'heures` <- format(Data_zone1$`Nombre d'heures`, digits = 9, decimal.mark = ".", big.mark = " ")

        Data_MDE <- data.frame(MDE = c(paste("R\U00E9", "cup\U00E9rateur de chaleur", sep = ""), "Free cooling"))
        Data_MDE$etat <- c(input$Mode_recup_ener0, input$Free_Cooling0)

        wb <- XLConnect::loadWorkbook(filename = file, create = TRUE)


        XLConnect::createSheet(wb, name = "Resultats")
        cs <- XLConnect::createCellStyle(wb, name = "myDateStyle")
        XLConnect::setDataFormat(cs, format = "dd/mm/yyyy HH:MM")
        XLConnect::setCellStyle(wb, sheet = "Resultats", row = 1, col = 1, cellstyle = cs)
        XLConnect::createSheet(wb, name = "Resultats")
        XLConnect::writeWorksheet(wb, data = new_results, sheet = "Resultats")
        setColumnWidth(wb, sheet = "Resultats", column = 1, width = 5000)
        setColumnWidth(wb, sheet = "Resultats", column = 2, width = 3000)
        setColumnWidth(wb, sheet = "Resultats", column = 3, width = 3000)
        setColumnWidth(wb, sheet = "Resultats", column = 4, width = 3000)
        setColumnWidth(wb, sheet = "Resultats", column = 5, width = 3000)
        setColumnWidth(wb, sheet = "Resultats", column = 6, width = 3000)
        setColumnWidth(wb, sheet = "Resultats", column = 7, width = 3000)
        setColumnWidth(wb, sheet = "Resultats", column = 8, width = 3000)
        setColumnWidth(wb, sheet = "Resultats", column = 9, width = 3000)
        setColumnWidth(wb, sheet = "Resultats", column = 10, width = 3000)
        setColumnWidth(wb, sheet = "Resultats", column = 11, width = 3000)
        setColumnWidth(wb, sheet = "Resultats", column = 12, width = 3000)
        setColumnWidth(wb, sheet = "Resultats", column = 13, width = 3000)
        setColumnWidth(wb, sheet = "Resultats", column = 14, width = 3000)
        setColumnWidth(wb, sheet = "Resultats", column = 15, width = 2500)
        setColumnWidth(wb, sheet = "Resultats", column = 16, width = 3000)
        setColumnWidth(wb, sheet = "Resultats", column = 17, width = 3000)
        setColumnWidth(wb, sheet = "Resultats", column = 18, width = 3000)
        setColumnWidth(wb, sheet = "Resultats", column = 19, width = 3000)
        setColumnWidth(wb, sheet = "Resultats", column = 20, width = 3000)
        setColumnWidth(wb, sheet = "Resultats", column = 21, width = 3000)
        setColumnWidth(wb, sheet = "Resultats", column = 22, width = 3000)
        setColumnWidth(wb, sheet = "Resultats", column = 23, width = 3000)
        setColumnWidth(wb, sheet = "Resultats", column = 24, width = 3000)
        setColumnWidth(wb, sheet = "Resultats", column = 25, width = 2800)
        setColumnWidth(wb, sheet = "Resultats", column = 26, width = 2800)
        setColumnWidth(wb, sheet = "Resultats", column = 27, width = 2800)
        setColumnWidth(wb, sheet = "Resultats", column = 28, width = 3000)
        setColumnWidth(wb, sheet = "Resultats", column = 29, width = 3000)
        setColumnWidth(wb, sheet = "Resultats", column = 30, width = 3000)
        XLConnect::createSheet(wb, name = "Donnees Meteo")
**`strong text`**

What you're doing with:

colnames(new_results) <- c(new_names)

is to rename existing columns. Here the dataframe new_results has 33 columns, and you are trying to give it 34 names, so R complains (you can't rename a column that doesn't exist yet).

If your goal is to add a column, you can't just add the name, you have to specify the data that goes inside the new column. For example:

new_results$`Besoin de froid zone8 (Besoin_fr [kWh])` <- numeric(length = nrow(new_results))
besoin_fr<-numeric(nombre_de_pas)
}
    if (Humidificateur == "Humidificateur vapeur electricite" || Humidificateur=="Humidificateur vapeur gaz" ){
      Resultats = Fct_traitement_vapeur (Matrice_soufflage["C","Temp"], Matrice_soufflage["C","r"], T_adp, r_adp, Caract_melange$r, Caract_melange$Temp, 
                                         Matrice_soufflage["D","r"], Matrice_soufflage["D","Temp"], Caract_melange$h, Matrice_soufflage["B","h"], Matrice_soufflage["B","r"],
                                         Besoin, m_mel, zone, P_e_ch, Besc, Besf, T_s_e_ch, T_e_e_ch, m_e_ch, rend_batterie, eff_humid, Matrice_soufflage["B","Temp"], 
                                         m_eau_conso, m_eau_conso_z1, m_eau_cond, conso_humid, Matrice_soufflage["A","r"], Caract_melange$HR, Matrice_soufflage["A","HR"], 
                                         Matrice_soufflage["C","h"], Matrice_soufflage["B","HR"], P_e_fr, h_adp, m_e_fr, T_s_e_fr, T_e_e_fr)
      conso_humid = Resultats$conso_humid
    }
    Besf = Resultats$Besf #Besoin froid
    Besc = Resultats$Besc # Besoin chaud
    P_e_fr = Resultats$P_e_fr 
    P_e_ch = Resultats$P_e_ch
    T_s_e_fr = Resultats$T_s_e_fr
    T_s_e_ch = Resultats$T_s_e_ch
    Tsouf = Resultats$Tsouf #Température de souflage (°C)
    rsouf = Resultats$rsouf
    hsouf = Resultats$hsouf
    m_eau_conso = Resultats$m_eau_conso # masse eau évaporée
    m_eau_conso_z1 = Resultats$m_eau_conso_z1 # masse d'eau évaporée dans la zone 1
    m_eau_cond = Resultats$m_eau_cond # masse d'eau condensée
    zone = Resultats$zone
    Besoin = Resultats$Besoin
    Besoin= Resultats$Besoin_fr
 besoin_fr[[i]]<-Besoin_fr
 
i have already  created a vector to store values within another function in R

Sorry I don't understand the question. And I don't understand the code in your post, if it's supposed to make the question clearer (though, note that R is case sensitive, so besoin_fr and Besoin_fr are different variables).

Can you try to make a minimal example? Isolate the exact problem you have, it would be easier to help.

besoin_fr is a vector where I store all the values of Besoin_fr calculated by a function. My goal is to add a column in the results file and visualize all the values Besoin_fr
I have added this to the function code So that it can return it to me:

Besoin<- m_mix * (Mix_character - h3)
Besoin_fr <- Besoin
and in the end of function i made:
sortie_centrale <- data.frame(
"Besf" = Besf,
"Besc" = Besc,
"P_e_fr" = P_e_fr,
"P_e_ch" = P_e_ch,
"T_s_e_fr" = T_s_e_fr,
"T_s_e_ch" = T_s_e_ch,
"Tsouf" = Caract_soufflage$Temp,
"rsouf" = Caract_soufflage$r,
"hsouf" = Caract_soufflage$h,
"HRsouf" = Caract_soufflage$HR,
"m_eau_conso" = m_eau_conso,
"m_eau_conso_z1" = m_eau_conso_z1,
"m_eau_cond" = m_eau_cond,
"conso_humid" = conso_humid,
"zone" = zone,
"Besoin" = Besoin,
"Besoin_fr"=Besoin_fr,
In another function, I have added:

Resultats = Fct_traitement_vapeur (Matrice_soufflage["C","Temp"], Matrice_soufflage["C","r"], T_adp, r_adp, Caract_melange$r, Caract_melange$Temp, 
                                         Matrice_soufflage["D","r"], Matrice_soufflage["D","Temp"], Caract_melange$h, Matrice_soufflage["B","h"], Matrice_soufflage["B","r"],
                                         Besoin, m_mel, zone, P_e_ch, Besc, Besf, T_s_e_ch, T_e_e_ch, m_e_ch, rend_batterie, eff_humid, Matrice_soufflage["B","Temp"], 
                                         m_eau_conso, m_eau_conso_z1, m_eau_cond, conso_humid, Matrice_soufflage["A","r"], Caract_melange$HR, Matrice_soufflage["A","HR"], 
                                         Matrice_soufflage["C","h"], Matrice_soufflage["B","HR"], P_e_fr, h_adp, m_e_fr, T_s_e_fr, T_e_e_fr,Besoin_fr)
and also 
Besoin_fr= Resultats$Besoin_fr
besoin_fr[[i]]<-Besoin_fr                               (##To store each value at each instance)
After that, I added the 30th column to the Excel file:
 
> ```
> XLConnect::createSheet(wb, name = "Resultats")
>         cs <- XLConnect::createCellStyle(wb, name = "myDateStyle")
>         XLConnect::setDataFormat(cs, format = "dd/mm/yyyy HH:MM")
>         XLConnect::setCellStyle(wb, sheet = "Resultats", row = 1, col = 1, cellstyle = cs)
>         XLConnect::createSheet(wb, name = "Resultats")
>  setColumnWidth(wb, sheet = "Resultats", column = 29, width = 3000)
>         setColumnWidth(wb, sheet = "Resultats", column = 30, width = 3000)
>         XLConnect::createSheet(wb, name = "Donnees Meteo")

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.