在四开仪表板中添加复制到剪贴板按钮

问题描述 投票:0回答:1

我想添加一个按钮,用于复制 Quarto 仪表板中用户的输入或输出。我发现

shiny2clipboard
这似乎是一个不错的选择。不幸的是,我尝试运行的代码不会将文本复制到四开仪表板内的剪贴板。当您从链接中的博客运行示例代码时,它确实可以工作,但不能在 Quarto 仪表板中工作。这是一些可重现的代码:

---
title: "Test"
format: dashboard
server: shiny
---

## Test

```{r}
library(shiny)
library(shinyCopy2clipboard)
```

```{r}
#| title: "Text that needs to copy to clipboard"

# Setup
  use_copy()

  # Text Input 1
  textInput("text", "Enter Your Text")

  # Copy Button 1
shinyCopy2clipboard::CopyButton(
    "copybtn",
    label = "Copy to clipboard",
    icon = icon("copy"),
    text = "No Text Found"
  )
```


```{r}
#| context: server
observe({

    req(input$copybtn)
  shinyCopy2clipboard::CopyButtonUpdate(session,
                     id = "copybtn",
                     label = "Copy to clipboard",
                     icon = icon("copy"),
                     text = input$text
    )

  })
```

输出:

enter image description here

正如您在输出中看到的,我尝试将文本从输入字段复制到剪贴板。这是行不通的。所以我想知道是否有人知道如何添加一个按钮来将文本复制到四开仪表板内的剪贴板?

r clipboard quarto
1个回答
0
投票

这应该足够了(定义一个具有合适的

actionButton
事件的
onclick
):

---
title: "Test"
format: dashboard
server: shiny
---

## Test

```{r}
library(shiny)
```

```{r}
#| title: "Text that needs to copy to clipboard"

# Text Input 1
textInput("text", "Enter Your Text")

actionButton(
  "copy_link", 
  "Copy to clipboard", 
  onclick = "
    txt = document.getElementById('text').value;
    navigator.clipboard.writeText(txt);"
)
```

```{r}
#| context: server
```
© www.soinside.com 2019 - 2024. All rights reserved.