Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Long code chunks when knitting to pdf #381

Open
meesew opened this issue Sep 28, 2022 · 1 comment
Open

Long code chunks when knitting to pdf #381

meesew opened this issue Sep 28, 2022 · 1 comment

Comments

@meesew
Copy link

meesew commented Sep 28, 2022

I have tried all other solutions online, including those posted by the author of the knitr package. For some reason, every time I knit to pdf, it will not format the code correctly, and all the code falls off the page. I have been working on this for two weeks and have spent about 2-3 hours a day trying to solve it. Does anyone have an idea of why it isn't working?

I tried the styler package options, formatR options, etc. Below you can see the code using the solution for setting linewidth options in knitr, https://github.com/yihui/knitr-examples/blob/master/077-wrap-output.Rmd. I tried uploading the data file for anyone to reproduce the problem, but I can't upload an .rds file.

--- title: "Online Supplement" author: "Anonymous for Review" date: "r Sys.Date()`"
output:
pdf_document: default

set.seed(42)
library(car)
library(lattice)
library(effects)
library(emmeans)
library(psych)
library(tidyverse)
library(flexplot)
library(lme4)
library(kableExtra)
library(stargazer)
library(papaja)
library(confintr)
library(ppcor)
library(jtools)
library(interactions)
library(patchwork)
library(ggstance)
library(ggExtra)
library(gghalves)
library(colorspace)
library(viridis)
library(formatR)
library(styler)

pcor_ci.test <-
function (x, y, z, method = c("pearson", "kendall", "spearman"), conf.level = 0.95, ...) {
    d1 <- deparse(substitute(x))
    d2 <- deparse(substitute(y))
    d3 <- deparse(substitute(z))
    data.name <- paste0(d1, " and ", d2, "; controlling: ", d3)
    method <- match.arg(method)
    Method <- paste0("Partial correlation (", method, ")")
    alternative <- "true partial correlation is not equal to 0"

    x <- as.vector(x)
    y <- as.vector(y)
    z <- as.data.frame(z)
    xyz <- data.frame(x, y, z)
    pcor <- ppcor::pcor(xyz, method = method)
    estimate <- pcor$est[1, 2]
    p.value <- pcor$p.value[1, 2]
    parameter <- c(n = pcor$n, gp = pcor$gp)
    statistic <- c(Stat = pcor$statistic[1, 2])

    fit1 <- lm(x ~ z, data = xyz)
    fit2 <- lm(y ~ z, data = xyz)
    cortest <- cor.test(resid(fit1), resid(fit2), method = method, conf.level = conf.level, ...)
    ci <- cortest$conf.int

    ht <- list(
        statistic = statistic,
        parameter = parameter,
        p.value = p.value,
        estimate = c(partial.cor = estimate),
        alternative = alternative,
        method = Method,
        data.name = data.name,
        conf.int = ci
    )
    class(ht) <- "htest"
    ht
}

theme_minimalism <- function(){ theme_minimal() + theme(panel.grid.major.y=element_blank(),
panel.grid.minor.y=element_blank(),
panel.grid.major.x=element_blank(),
panel.grid.minor.x=element_blank(),
text=element_text(size=14),
axis.text=element_text(size=12),
axis.title=element_text(size=14, face="bold"))
}
library(knitr)
hook_output = knit_hooks$get('output')
knit_hooks$set(output = function(x, options) {
  # this hook is used only when the linewidth option is not NULL
  if (!is.null(n <- options$linewidth)) {
    x = xfun::split_lines(x)
    # any lines wider than n should be wrapped
    if (any(nchar(x) > n)) x = strwrap(x, width = n)
    x = paste(x, collapse = '\n')
  }
  hook_output(x, options)
})

Study 1a

Data Prep

study1a <- read_rds("study1a.rds")
keys_list_study1a <- list(
  infoavoid = c("Q103_4", "Q103_5", "-Q103_6", "Q103_7", "-Q103_8", "Q103_9", "-Q103_10", "-Q103_11"),
  derog = c("-Q63_1", "Q63_5", "Q63_8", "Q63_9", "Q63_10"),
  heartavoid = c("Q104_4", "Q104_5", "-Q104_6", "Q104_7", "-Q104_8", "Q104_9", "-Q104_10", "-Q104_11")
) #keys created identify items to be scored for each scale.
my_scales_study1a <- scoreItems(keys_list_study1a,
                                study1a,
                                totals = FALSE,
                                min = 1, max = 7, impute = "none") #Score items using keys list to create a new variable representing one score, the mean, for each scale for each person
#print(my_scales_study1a, short = TRUE)
study1a_scores <- my_scales_study1a$scores  
study1a <- as_tibble(cbind.data.frame(study1a, study1a_scores))#add new variables to data set
study1a$derog[study1a$TestingDecision == 1] <- NA

x <- study1a %>% filter(TestingDecision == 0) # Data frame for correlations between predisposition to avoid and feedback derogation includes only those who received feedback (did not avoid)

`

@meesew
Copy link
Author

meesew commented Sep 28, 2022

I also tried the solution here: https://bookdown.org/yihui/rmarkdown-cookbook/text-width.html and it did not work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant