dplyr table display style change [duplicate] - r

Using the latest version of tibble the output of wide tibbles is not properly displayed when setting width = Inf.
Based on my tests with previous versions wide tibbles were printed nicely until versions later than 1.3.0. This is what I would like the output to be printed like:
...but this is what it looks like using the latest version of tibble:
I tinkered around with the old sources but to no avail. I would like to incorporate this in a package so the solution should pass R CMD check. When I just copied a load of functions from tibble v1.3.0 I managed to restore the old behavior but could not pass the check.
There's an open issue on Github related to this problem but it's apparently 'not high priority'. Is there a way to print tibbles properly with the new version?

Try out this function:
print_width_inf <- function(df, n = 6) {
df %>%
head(n = n) %>%
as.data.frame() %>%
tibble:::shrink_mat(width = Inf, rows = NA, n = n, star = FALSE) %>%
`[[`("table") %>%
print()
}

This seems to have change, now one can just use:
options(tibble.width = Inf)

Related

Javascript error using DT package in RStudio

This is my first post, and I'm probably doing something silly to create this error ... I am running R 4.2.1 in RStudio, version 2022.07.1, built 554 ("Spotted Wakerobin"). Using a built-in dataset, here is a reproducible example:
table(esoph$agegp, esoph$alcgp, dnn = c("age", "alc")) |>
DT::datatable(
options = list(
scrollY = FALSE)
)
I am getting a Javascript Alert. In case the image doesn't appear, it says,
DataTables warning: table id=DataTables_Table_0 - Requested unknown parameter '4' for row 0, column 4. For more information about this error, please see http://datatables.net/tn/4
I read that page, and I wonder if it means there is a combination of values that does not exist in the dataset. Appreciate any help. Thank you.
I asked a work colleague about this problem, and she came up with this solution: Converting the table to a data frame, then pivoting wider:
table(agegp = esoph$agegp, alcgp = esoph$alcgp) |>
as.data.frame() |>
tidyr::pivot_wider(names_from = alcgp, values_from = Freq) |>
DT::datatable(options = list(scrollY = FALSE))
Thank you to all who replied.

ENP function in mutate

currently, I am cleaning my dataset (Comparative Manifesto Project) and try to compute the effective number of parties using the enp function from the electoral package (https://www.rdocumentation.org/packages/electoral/versions/0.1.2/topics/enp). However, I am running in some issues.
When I run this code:
cmp_1990 %>%
mutate(enp_vote = round(pervote, digits = 2)) %>%
mutate(enp_vote = as.numeric(enp_vote)) %>%
relocate(enp_vote, .before = parfam) %>%
mutate(enp_vote = enp(votes = cmp_1990$enp_vote)) %>%
relocate(enp, .before = parfam)
I get the error message:
Fehler: Can't subset columns that don't exist.
x Column `enp` doesn't exist.
I suppose, r thinks of the function enp as single column even though I have installed and used library on the package.
I tried it with differently rounded numbers and by using the enp command outside of the rest of the command but up until now nothing worked. Oh and the cmp_1990$enp_vote command was necessary as otherwise the enp function thought of enp_vote as categorical and not numerical value.
Sorry by the way if my code doesnt look like the nicest, its my first time using r haha.
Thanks very much in advance!

flextable package assigns changes without <-

I'm not sure if this is the correct forum to post this, but I have noticed some strange behavior with the flextable package in R, and was wondering if anyone can shed any light.
In the documentation for flextable it shows objects being modified when they are re-assigned to themselves, eg:
ft <- regulartable(head(iris))
ft <- color(ft, color = "orange", part = "body" )
However, my code is modifying the actual table even without re-assigning it, just using piping %>%:
myft <- regulartable(head(iris))
myft %>% align(j = 1, align = "left")
myft # changed
I don't think piping is the issue as it doesn't have the same effect with other packages, eg:
library(plyr)
df <- head(iris)
df %>% mutate(Sum=Sepal.Width*2)
df # unchanged
Is this a bug in flextable? Or is this by design?
It's true that you can mutate formats without assigning the object. But that's not a behavior you can rely on. This is an unwanted design ;) and should be corrected in the next versions so it is safer to assign the result if you want your code to work with future versions.

Wrap long url line in R markdown

I've read the various posts on this, but I still haven't found a solution. Here's some example code:
library(dplyr)
library(lubridate)
urlfile<-'https://raw.githubusercontent.com/blakeobeans/Predicting-Service-Calls/master/Data/nc.csv'
dates<-read.csv(urlfile, header=FALSE)
dates$V1 <- mdy(dates$V1)
dates <- dates %>%
rename("data.time" = V1) %>%
filter("2017-10-01" >= data.time & data.time >= "2017-06-01") %>%
group_by(data.time) %>%
summarise(n = n())
When I output to the pdf...
The same thing happens if I have notes in the code running out of the grey bar.
I've tried using the following line of code at the beginning:
knitr::opts_chunk$set(tidy.opts=list(width.cutoff=60),tidy=TRUE)
But that doesn't help.
I had a similar problem when putting package on CRAN (they give a note if Rd file line exceeds 90 characters (NOTE: lines wider than 90 characters)). One of the arguments to my function was url to a github dataset. Solution was to split url into separate arguments. For example:
urlRemote <- "https://raw.githubusercontent.com/"
pathGithub <- "blakeobeans/Predicting-Service-Calls/master/Data/"
fileName <- "nc.csv"
And you can use it in your code like this:
paste0(urlRemote, pathGithub, fileName) %>%
read.csv(header = FALSE)
This solution has an advantage when you want to use multiple files from the same repository as you can use paste0(urlRemote, pathGithub, fileName1), paste0(urlRemote, pathGithub, fileName2), etc.

Print tibble with column breaks as in v1.3.0

Using the latest version of tibble the output of wide tibbles is not properly displayed when setting width = Inf.
Based on my tests with previous versions wide tibbles were printed nicely until versions later than 1.3.0. This is what I would like the output to be printed like:
...but this is what it looks like using the latest version of tibble:
I tinkered around with the old sources but to no avail. I would like to incorporate this in a package so the solution should pass R CMD check. When I just copied a load of functions from tibble v1.3.0 I managed to restore the old behavior but could not pass the check.
There's an open issue on Github related to this problem but it's apparently 'not high priority'. Is there a way to print tibbles properly with the new version?
Try out this function:
print_width_inf <- function(df, n = 6) {
df %>%
head(n = n) %>%
as.data.frame() %>%
tibble:::shrink_mat(width = Inf, rows = NA, n = n, star = FALSE) %>%
`[[`("table") %>%
print()
}
This seems to have change, now one can just use:
options(tibble.width = Inf)

Resources