0

I added a small example to demonstrate the issue

library("readxl")
library("openxlsx")

df <- data.frame(QN=1, ST = "String", EL=cbind(list(c("Var1", "Var2"))), BL=FALSE)

cat(paste0("\nQ=", as.numeric(df$QN[[1]])))
cat(paste0("\nST=", as.character(df$ST[[1]])))
cat(paste0("\nEL=", as.character(df$EL[[1]])))
cat(paste0("\nBL=", as.logical(df$BL[[1]])))

wb <- createWorkbook("TEST")

addWorksheet(wb, "Data")
writeData(wb, sheet = 1, df)

saveWorkbook(wb, file = "Test.xlsx", overwrite = TRUE)

df2 <- read_excel(sheet = 1, "Test.xlsx")

cat(paste0("\n\nQ2=", as.numeric(df2$QN[[1]])))
cat(paste0("\nST2=", as.character(df2$ST[[1]])))
cat(paste0("\nEL2=", as.character(df2$EL[[1]])))
cat(paste0("\nBL2=", as.logical(df2$BL[[1]])))

Outputs looks like this

Q=1
ST=String
EL=Var1 
EL=Var2
BL=FALSE

Q2=1
ST2=String
EL2=Var1, Var2
BL2=FALSE

When I view the data frame using view(df), I see this:

c("Var1", "Var2") for EL

When I view the second date frame, I see this:

Var1, Var2 for EL

Notice the difference between EL and EL2. I want EL2 to match EL.

--- ORIGINAL QUESTION --- I have a simple data frame that looks like this (I'll list just one element):

cElementNames
c("Roles_1", "Roles_2", "Roles_3")

I save the data frame to Excel using this code:

writeData(wb, sheet = iSheet, df, startCol = 1, startRow = 1)

The column looks like this in the Excel file: Roles_1, Roles_2, Roles_3

I read the Excel file back into a data.frame using this code:

df <- read_excel(sheet = 1, ExcelFile)

The element now looks like this: "Roles_1, Roles_2, Roles_3"

and is not usable in my code.

I am using the data.frame to hold configuration options so that I can generate a report. I would rather read the configuration information from Excel than hard code it in my code.

Since I am new at this, I'll take any advice I can get.

Thanks

NewAtThis
  • 45
  • 5
  • Hi, you could use `str_split` from `library(stringr)`to split the vector. Other than that if you rework your example, so that people can recreate the example file. It will be easier to help. And right now your dataframe `c(...)` looks like a vector to me. – Johannes Stötzer Oct 23 '19 at 15:35
  • The data.frame contains about 15 different elements that vary from vectors, to numbers, booleans and etc. I summarized to just one to illustrate my issue. I use this command to create the data.frame df <- data.frame(cElementNames = cbind(list(c("Roles_1", "Roles_2", "Roles_3")))) When i read from the Excel file, cElementNames is not usable in my existing code. – NewAtThis Oct 23 '19 at 15:40

1 Answers1

0

with your additional information I could do this with base packages.

df2$EL2 <- lapply(strsplit(df2$EL, ","), trimws, "l")
identical(df2$EL2[1], df$EL[1])

Now you have the same content as before.