简体   繁体   English

R 目标包错误 - 最后一个错误:<text> :1:2: 意外的 '/'</text>

[英]R targets packages error - Last error: <text>:1:2: unexpected '/'

When I run the code, there is a error:当我运行代码时,出现错误:

✖ error target quantities_plot_base
• end pipeline [3.558 seconds]
Error:

! Error running targets::tar_make()
  
Target errors: targets::tar_meta(fields = error, complete_only = TRUE)
  
Tips: https://books.ropensci.org/targets/debugging.html
  
Last error: <text>:1:2: unexpected '/'

1: +/

     ^

The target is the following code in the list:目标是列表中的以下代码:

tar_target(
    name = quantities_plot_base,
    command = powerscale_seq_summ_plot(sensitivity_sequence_base)
  )

And the powerscale_seq_summ_plot in the function file is like: function 文件中的 powerscale_seq_summ_plot 是这样的:

powerscale_seq_summ_plot <- function(powerscale_seq) {

  powerscale_plot_quantities(powerscale_seq, variables = "b_wrist", quantities = c("mean", "sd"), mcse = TRUE) +
    ggplot2::facet_wrap(
      . ~ quantity,
      scales = "free",
      ncol = 3,
      labeller = ggplot2::as_labeller(
        c(
          "b_wrist" = "",
          "sd" = "SD",
          "mean" = "Mean",
          "cjs_dist" = "$\\text{CJS}_{\\text{dist}}$"
        )
      )
    ) +
    guides(colour = "none") +
    xlab("Power-scaling $\\alpha$") +
    scale_color_manual(values = rep("black", 3)) +
    scale_shape_manual(values = c("prior" = 15, "likelihood" = 22), labels = c("Prior power-scaling", "Likelihood power-scaling"), name = NULL) +
    scale_linetype_manual(values = "dashed", labels = "$\\pm2$ MCSE", name = NULL) +
    cowplot::theme_half_open() +
    theme(
      legend.position = "bottom",
      legend.text = element_text(size = 10),
      axis.text = element_text(size = 10),
      axis.title = element_text(size = 10),
      strip.text = element_text(size = 10),
      strip.background = element_blank(),
      legend.text.align = 0,
      axis.line.y = element_blank(),
      axis.ticks.y = element_line(colour = "black"),
      axis.line.x = element_blank(),
      axis.ticks.x = element_line(colour = "black"),
      legend.title = element_text(size = 10),
      aspect.ratio = 1
    ) +
    cowplot::panel_border(color = "black", size = 1)
}

Error Traceback plot I am not sure which '+' conducts the error.错误回溯 plot我不确定是哪个“+”导致了错误。

options code for $ $ tikzLatex, it doesn't work on my Rstudio, is there any wrong? $ $ tikzLatex 的选项代码,它在我的 Rstudio 上不起作用,有什么问题吗?

options(
  tizkDocumentDelcaration = "\\documentclass[10pt]{article}",
  tikzLatexPackages = c(
    getOption("tikzLatexPackages"),
    "\\usepackage{tikz}",
    "\\usepackage{amsmath}",
    "\\usepackage{amsfonts}",
    "\\usepackage{bm}",
    "\\usepackage{lmodern}",
    "\\usepackage{multirow}",
    "\\usepackage[T1]{fontenc}",
    "\\usepackage{textcomp}",
    "\\usepackage{microtype}",
    "\\DeclareMathOperator{\\normal}{normal}",
    "\\DeclareMathOperator{\\Bernoulli}{Bernoulli}",
    "\\DeclareMathOperator{\\gammadist}{gamma}",
    "\\DeclareMathOperator{\\expdist}{exponential}",
    "\\DeclareMathOperator{\\betadist}{beta}",
    "\\DeclareMathOperator{\\Cauchy}{Cauchy}"
  )
)

The code is from the GitHub link .代码来自 GitHub链接 If you are interested in the detail, you can check the case 'bodyfat'.如果您对细节感兴趣,可以查看案例“bodyfat”。

The problem is with the priorsense package. If you removed mcse = TRUE it would work.问题出在priorsense package 上。如果您删除了 mcse mcse = TRUE ,它将起作用。 Since I figured out what's wrong, I've written to the maintainers of the package. They have almost nothing open, which seems like a really good sign.自从我弄清楚出了什么问题,我就写信给 package 的维护者。他们几乎没有打开任何东西,这似乎是一个非常好的迹象。 (As in they fix issues that users raise...I'm assuming...) (因为他们解决了用户提出的问题......我假设......)

While this answer will get you the plot, you'll find that there are other errors in your code.虽然此答案将为您提供 plot,但您会发现代码中还有其他错误。 (I'll go into that more at the end.) However, some of the errors I see may be resolved if I used your data. (最后我将 go 详细说明。)但是,如果我使用您的数据,我看到的一些错误可能会得到解决。

There are a few things you could do before this issue is fixed in that R package. One possibility is cloning their package in github, fixing the code, and then you would install your repo (which replaces your current priorsense library).在 R package 中修复此问题之前,您可以做一些事情。一种可能是在 github 中克隆他们的 package,修复代码,然后安装您的 repo(它会替换您当前的priorsense库)。 An easier method is to add the two functions below in your code to your script file before you call the plot. (These replace the two functions you need to make this work in the priorsense library. These two functions are almost identical to what is literally in the priorsense library with a few changes—I reindented the lines (for easier reading); I removed and replaced deprecated functions; I fixed the code causing the error.一种更简单的方法是在调用 plot 之前,将代码中的以下两个函数添加到脚本文件中。(这些函数替换了使它在priorsense库中工作所需的两个函数。这两个函数几乎与字面意思相同在priorsense库中进行了一些更改——我重新缩进了这些行(以便于阅读);我删除并替换了不推荐使用的函数;我修复了导致错误的代码。

You will not need to change any of the code that you've written.您不需要更改您编写的任何代码。 (I'm assuming...you didn't share your data, so there could be other errors.) (我假设......你没有分享你的数据,所以可能还有其他错误。)

library(posterior) # this is needed for these functions to work
          # ggplot2 is also needed, but you're already calling it
powerscale_summary_plot <- function (
    x, variables, quantities = NULL, scale = FALSE, base_mcse = NULL, ...)
{
  if (is.null(quantities)) {
    quantities <- setdiff(colnames(x[[1]]), c("variable", "alpha", "component",
                                              "pareto_k", "pareto_kf", "n_eff"))
  }
  x[[1]] <- x[[1]][x[[1]][["variable"]] %in% variables, ]
  summaries <- stats::reshape(data = x[[1]], varying = quantities, 
                              direction = "long", times = quantities, 
                              v.names = "value", timevar = "quantity")
  summaries$quantity <- factor(summaries$quantity, levels = quantities)
  summaries$pareto_k_value <- ifelse(summaries$pareto_k > 0.5, 
                                     "> 0.5", "< 0.5")
  summaries$pareto_k_value <- factor(summaries$pareto_k_value, 
                                     levels = c("< 0.5", "> 0.5"))
  points <- summaries[summaries$alpha == min(summaries$alpha) | 
                        summaries$alpha == max(summaries$alpha), ]
  p <- ggplot(data = summaries, mapping = aes(x = alpha, y = value)) + 
    geom_line(aes(color = pareto_k_value, group = component)) + 
    facet_wrap(facets = variable ~ quantity, scales = "free", 
               ncol = length(quantities)) + 
    geom_point(aes(x = alpha, y = value, shape = component, 
                   color = pareto_k_value), 
               fill = "white", size = 3, data = points) + 
    scale_shape_manual(values = c(likelihood = 22, prior = 15)) + 
    scale_color_viridis_d(drop = FALSE) + 
    guides(color = guide_legend(title = "pareto-k", 
                                override.aes = list(shape = 15))) + 
    ylab("") + 
    scale_x_continuous(
      trans = "log2", 
      limits = c(min(summaries$alpha) - 0.01, max(summaries$alpha) + 0.01), 
      breaks = c(min(summaries$alpha), 1, max(summaries$alpha)), 
      labels = round(c(min(summaries$alpha), 1, max(summaries$alpha)), 
                     digits = 3)) + 
    ggtitle(
      label = "Power-scaling sensitivity", 
      subtitle = paste0("Posterior quantities depending on amount of power-scaling ",
                        "(alpha).\nHorizontal lines indicate low sensitivity.\nSteeper ",
                        "lines indicate greater sensitivity.\nEstimates with Pareto-k ",
                        "values > 0.5 may be inaccurate.")) + 
    theme(aspect.ratio = 1)
  if (!is.null(base_mcse)) {
    bmcse <<- base_mcse
    p <- p + scale_linetype_manual(values = "dashed", name = NULL) + 
      geom_hline(aes(yintercept = `mcse_min`, linetype = "+/-2MCSE"),
                 data = base_mcse, color = "black") + 
      geom_hline(aes(yintercept = `mcse_max`, linetype = "+/-2MCSE"), 
                 data = base_mcse, color = "black")
  }
  
  return(p)
}


powerscale_plot_quantities <- function (
    x, variables,quantities = c("mean", "median", "sd", "mad", "quantile2"), 
    div_measure = "cjs_dist", resample = FALSE, measure_args = NULL, mcse = FALSE, ...)
{
  summ <- summarise_draws(x, ... = quantities, resample = resample, 
                          div_measures = div_measure, 
                          measure_args = measure_args)
  variables <- variables(
    subset_draws(x[[1]], variable = variables))
  if (mcse) {
    quants <- setdiff(colnames(summ[[1]]), 
                      c("variable", "alpha", "component", "pareto_k", 
                        "pareto_kf", "n_eff"))
    base_quantities <- summ[[1]][which(summ[[1]]$alpha == 1), ]
    base_quantities <- unique(base_quantities[c("variable", quants)])
    base_mcse <- summarise_draws(x$base_draws, 
                                 default_mcse_measures())
    base_mcse <- base_mcse[which(base_mcse$variable %in% 
                                   variables), ]
    base_mcse <- as.data.frame(base_mcse)
    base_mcse <- stats::reshape(data = base_mcse, 
                                varying = c("mcse_mean", "mcse_median", 
                                            "mcse_sd", "mcse_q5", "mcse_q95"),
                                direction = "long", 
                                times = c("mean", "median", "sd", "q5", "q95"),
                                v.names = "mcse", timevar = "quantity", 
                                idvar = "variable")
    base_q <- as.data.frame(base_quantities)
    base_q <- stats::reshape(data = base_q, varying = quants, 
                             direction = "long", times = quants, 
                             v.names = "value", 
                             timevar = "quantity", idvar = "variable")
    base_mcse <- merge(base_q, base_mcse)
    base_mcse$mcse_min <- base_mcse$value - 2 * base_mcse$mcse
    base_mcse$mcse_max <- base_mcse$value + 2 * base_mcse$mcse
  }
  else {
    base_mcse <- NULL
  }
  return(powerscale_summary_plot(summ, variables = variables, 
                                 base_mcse = base_mcse, ...))
}

I used their example fit from their Github site to test this.我使用他们 Github 网站上的示例拟合来对此进行测试。

powerscale_plot_quantities(pss, variables = "mu", 
                           quantities = c("mean", "sd"), mcse = TRUE)

在此处输入图像描述

Using your function (With b_wrist swapped for mu ; I made the variable call another argument in the function).使用您的 function(将b_wrist换成mu ;我让变量调用函数中的另一个参数)。

powerscale_seq_summ_plot(pss, "mu")
# Scale for colour is already present.
# Adding another scale for colour, which will replace the existing scale.
# Scale for shape is already present.
# Adding another scale for shape, which will replace the existing scale.
# Scale for linetype is already present.
# Adding another scale for linetype, which will replace the existing scale. 

在此处输入图像描述

I don't know of any libraries that let you use $ encapsulated expressions that ggplot2 will understand.我不知道有任何库可以让您使用ggplot2可以理解的$封装表达式。 If these work for you, have at it, Otherwise.如果这些对你有用,那就试试吧,否则。 here are some solutions to the label errors I see in the plot above.以下是我在上面的 plot 中看到的 label 错误的一些解决方案。

For "cjs_dist" = "$\\text{CJS}_{\\text{dist}}$" I don't know what you're trying to do here.对于"cjs_dist" = "$\\text{CJS}_{\\text{dist}}$"我不知道你想在这里做什么。 Are you just trying to show "CJS_dist" instead of "cjs_dist"?您只是想显示“CJS_dist”而不是“cjs_dist”吗? If so, then be literal: "cjs_dist" = "CJS_dist" .如果是这样,那就是字面意思: "cjs_dist" = "CJS_dist"

For xlab("Power-scaling $\\alpha$") + use xlab(expression(Power-scaling~alpha)) +对于xlab("Power-scaling $\\alpha$") +使用xlab(expression(Power-scaling~alpha)) +

For scale_l.netype_manual(values = "dashed", labels = "$\\pm2$ MCSE", name = NULL) + replace the labels with "±2 MCSE"对于scale_l.netype_manual(values = "dashed", labels = "$\\pm2$ MCSE", name = NULL) +用 "±2 MCSE" 替换标签

When I made these changes in your UDF (user-defined function) this is what I rendered.当我在您的 UDF(用户定义函数)中进行这些更改时,这就是我呈现的内容。

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM