I am quite unexperienced in R/Rstudio, but I am currently working on creating a package for my job where I have run into a problem I can't figure out. I have created several functions already where I use return() to get the dataframe created by the code to appear in my environment. However, in this one I only get the first 38 rows of the dataframe shown in the R console.
The code for the function:
widen <- function(projectpath) {
project.df <- readr::read_csv(file = projectpath)
projectwide.df <- project.df %>%
dplyr::select(-c(1, Detection_limit)) %>%
tidyr::pivot_wider(names_from = Element, values_from = Concentration)
projectwide.df <- as.data.frame(projectwide.df)
return(projectwide.df)
}
I tried this with and without the as.data.frame() and also tried only data.frame, but neither worked. It did work, however, when I ran the code by itself (not as a function) when testing it. Of course, then I did not have to use the return() function, which is where the problem seems to be.
At first my problem was that the data appeared as a tibble rather than a dataframe, but I believe this is no longer my problem as this appears above the dataframe in the console, and if I have understood my previous output correctly, this means that it is in fact a dataframe:
cols(
X1 = col_double(),
Sample = col_character(),
Date.x = col_character(),
Filter_type = col_character(),
Filter_size = col_double(),
Filter_box_nr = col_double(),
Filter_blank = col_character(),
Volume = col_double(),
Date.y = col_date(format = ""),
Day = col_double(),
Treatment = col_character(),
Element = col_character(),
Concentration = col_double(),
Detection_limit = col_double()
)
Here is one of my other functions where I use return(), it works here:
importxrf <- function(datapath, infopath) {
datafile.df <- importdata(datapath = datapath)
infofile.df <- importinfo(infopath = infopath)
projectfile.df <- dplyr::inner_join(datafile.df, infofile.df, by = "Sample")
notinprojectfile.df <- dplyr::anti_join(datafile.df, infofile.df, by = "Sample")
if(nrow(notinprojectfile.df) > 0) {
warning("WARNING! There are samples that do not match between your raw data file and information file.")
}
return(projectfile.df)
}
As far as I can see, the only difference between these two functions is the use of as.data.frame(), but as I mentioned, it does not work without this either. If anyone can help me figure this out, I would be very grateful! Thanks
EDIT: Here is a version of the code where I create the dataframe inside the code so it is reproducible. This just shows the first 5 rows of an actual dataset I have.
widennn <- function() {
Sample <- c("COM001", "COM001", "COM001", "COM001", "COM001")
Element <- c("C", "N", "O", "Na", "Mg")
Concentration <- c(-4.19727307987776, 0.292013243234358, 0.328051062623146, -0.0555794187038898, 0.0353942596959773)
Detection_limit <- c(1.22193802149026, 0.312338639119395, 0.0322146560280234, 0.0362539069926691, 0.00465264605182871)
firstrows.df <- data.frame(Sample, Element, Concentration, Detection_limit)
projectwide.df <- firstrows.df %>%
dplyr::select(-c(Detection_limit)) %>%
tidyr::pivot_wider(names_from = Element, values_from = Concentration)
projectwide.df <- as.data.frame(projectwide.df)
return(projectwide.df)
}