有没有办法在 RStudio RMarkdown 中制作文本 "not copyable"?

Is there a way to make text "not copyable" in RStudio RMarkdown?

我将在几周内教授几个 R 编码研讨会。学生喜欢提前收到资料,我喜欢为他们提供 RMarkdown HTML 文件作为讲义。

在class,我更喜欢同学不要“copy and paste”的代码。相反,我以较慢的速度进行讲座,以便学生可以练习编写代码,我认为这确实有助于促进学习。

当我输出.html文件时,有没有办法在YAML部分指定文本不是“copyable”?

我知道我可以通过应用程序将输出保存为 .pdf 和 运行,但我更希望输出不是“paged”并具有交互性table 的内容,如果可能的话。

我根据此处建议的起点找到了一个快速破解方法:https://css-tricks.com/almanac/properties/u/user-select/

library(shiny)

css <- ".nocopy {
  -webkit-user-select: none;  /* Chrome all / Safari all */
  -moz-user-select: none;     /* Firefox all */
  -ms-user-select: none;      /* IE 10+ */
  user-select: none;          /* Likely future */      
}"
func <- "func <- function(x, y, ...) {
  x + y
}"

ui <- fluidPage(
  tags$style(HTML(css)),
  "Copyable:",
  tags$pre(func),
  tags$hr(),
  tags$p("Not copyable:", class = "nocopy"),
  tags$pre(func, class = "nocopy")
)

server <- function(input, output, session) {}

shinyApp(ui, server)

正如这表明的那样,它可以应用于各种 HTML 元素,包括 <p><pre>。我没有测试其他元素。


几乎所有 web-dev 都会告诉您,不过,这是微不足道的。使用 FF(大多数浏览器具有类似功能):

  • Ctrl-I工具的快捷方式 > Web 开发人员 &; 切换工具
  • Ctrl-Shift-C ("Pick and element from the page")
  • 单击要突出显示并复制的元素
  • 在页面底部的 code-editor 中,将 class="nocopy" 更改为 class=""(或其他任何内容)

"Not copyable:" 文本上执行此操作的结果可在此处查看:

(我展示这些步骤是为了清楚地表明我认为这是一种浅薄的劝阻懒惰的尝试。那些选择 more-active-laziness (??) 的人这样做没有问题。如果他们真的下定决心了,他们直接把.nocopy的定义删掉就可以影响整个页面了。不过话说回来,也不能强迫学生按照你的意思去学:-)


编辑:

这在 Rmd 文件中同样容易工作:

文件:Nova.css

.nocopy {
  -webkit-user-select: none;  /* Chrome all / Safari all */
  -moz-user-select: none;     /* Firefox all */
  -ms-user-select: none;      /* IE 10+ */
  user-select: none;          /* Likely future */
}

文件:Nova.Rmd

---
output: html_document
css: Nova.css
---

Copyable:

```{r chunk1}
func1 <- function(x, y, ...) {
  x + y
}
```

<p class="nocopy">Not copyable:</p>

```{r chunk2, class.source = "nocopy"}
func2 <- function(x, y, ...) {
  x + y
}
```

您可以在此处使用两个 knitr optionsclass.source=class.output=。我假设您希望源是 "protected",但您可能也希望输出。