0

For starters, I am pretty new in R programming (or programming in general) - so it is possible I made some pretty straightforward mistakes. I need this function to generate feedback for subjects of my experiment - before the core procedure they will fill in the HEXACO questionnaire. I've created a function for STEN calculation and implementation into a data frame. Now, I want a function to generate a result for every person's scores, taking the ID and STEN value for every trait from the row in a data frame.

PRINT.HEXACO <- function(data) {

for(row in 1:nrow(data)) {

## read values from "data" of every trait STEN and person ID.

id <- data[row, "id"]
HH_sten <- data[row, "HH_sten"]
EM_sten <- data[row, "EM_sten"]
EX_sten <- data[row, "EX_sten"]
AG_sten <- data[row, "AG_sten"]
CS_sten <- data[row, "CS_sten"]
OP_sten <- data[row, "OP_sten"]

    ## Create STEN dataframe needed for plot *
    STEN_df <- data.frame(names = c("H-H", "EM", "EX", "AG", "CS", "OP"),
                          values = c(HH_sten, EM_sten, EX_sten, AG_sten, CS_sten, OP_sten))

    ## Make it a .gather object needed for plot *

    STEN_df.gather <- gather(STEN_df, key = "Scale", val = "Val")

    ## Create a plot! *

    STEN_plot <- ggplot(STEN_df.gather, aes(x = Scale, y = Val)) +
        theme_minimal() + 
        scale_x_discrete(labels = c("H-H", "EM", "EX", "AG", "CS", "OP"), name="Personality traits") + 
        scale_y_continuous(name=NULL, limits = c(0,10), breaks = (0:10))

## Taking template.doc for styles
    HEXACO_report <- read_docx("template.doc") %>%
        body_add_par(value = "HEXACO personality profile", style = "Title") %>%
        body_add_par(value=" ") %>%

## Printing ID of person

        body_add_par(value = paste("Person's ID:",id,sep = " ")) %>%

        ## generating a ggplot of HEXACO profile!   

        body_add_gg(value = STEN_plot, style = "centered") %>%

        body_add_par(value=" ") %>%

        ##Information about person's STEN in first scale, and if it's low/high
        body_add_par(value = paste("Honesty - Humility: STEN", HH_sten, "-", sep = " "), style = "Heading 1")%>%
        if(HH_sten <= 2) {
            slip_in_text(value = "very low", style = "Heading 1")
        } else if(HH_sten <= 4) {
            slip_in_text(value = "low", style = "Heading 1")
        } else if(HH_sten <= 6) {
            slip_in_text(value = "middle", style = "Heading 1")
        } else if(HH_sten <= 8) {
            slip_in_text(value = "high", style = "Heading 1")
        } else { 
            slip_in_text(value = "very high", style = "Heading 1")}%>%

        body_add_par(value=" ") %>%

        ##description for Honesty - Humility

        body_add_par(value="Description for high values of HH", style = "No Spacing") %>%

        body_add_par(value=" ") %>%

        body_add_par(value="Description for low values of HH", style = "No Spacing") %>%

## In there goes the same code for all other scales of the questionnaire

## And finally creating the report

        print(target = paste0("/raporty/Raport_",id,".doc"))
    }
}

After running the function on a testing data frame I got a strange error message:

Error in zip::unzip(zipfile = newfile, exdir = folder) : zip error: Cannot open zip fileC:\Users\Michał\AppData\Local\Temp\RtmpainnhZ\file1bcc2b7bcd6.doc>for reading in file zip.c:238 In addition: Warning message:

Error in zip::unzip(zipfile = newfile, exdir = folder) : zip error: Cannot open zip fileC:\Users\Michał\AppData\Local\Temp\RtmpainnhZ\file1bcc2b7bcd6.doc>for reading in file zip.c:238

I think, that the unzip function is used while generating the .doc by the officer package. But I don't know what may be causing the error, as I've never even used zip::unzip function before, and I think my current knowledge in R may be insufficient for this problem. Maybe someone had a similar problem before, or just know by heart the solution for this one? I'm asking for some help.

Here is head of my test data frame:

TestDataSO <- data.frame(id = c(1:6), 
HH_sten = c(6,4,8,6,3,2), 
EM_sten = c(8,8,4,3,2,6), 
EX_sten = c(3,9,7,8,4,4), 
AG_sten = c(3,5,5,3,7,7), 
CS_sten = c(7,4,10,8,3,2), 
OP_sten = c(5,7,4,5,4,8))
M_Kos
  • 78
  • 8
  • read_docx accepts only docx files (no doc) – David Gohel Jan 28 '20 at 16:40
  • Thanks! I've decided to drop styles and template.docx completely for the time being, as it generated error anyway (don't have MsOffice to create correct docx). Now I'm getting error on the level of plot creation. I've put everything that shuld go into y scale in as.numeric(), added geom_col(), as it was missing before. I'm receiving "Discrete value supplied to continuous scale" error all the time. – M_Kos Feb 02 '20 at 11:58

0 Answers0