我有一个 Shiny 的应用程序,带有数字输入,可以控制弹出的 ColourPickers 和 rclipButtons 的数量。按钮的颜色与通过 ColorPicker 选择的颜色相同。 我想要做的是将每个 ColourPicker 的颜色设置为不同的颜色,当我单击按钮时,ColourPicker 中的十六进制代码将复制到我的剪贴板(如图所示):
我希望能够将该十六进制代码粘贴到“粘贴到此处”框中。我遇到的问题是,我拥有的代码只允许我从第一个框中复制十六进制代码。如果我单击其他框,则不会复制任何内容。如果我更改按钮 1 的颜色,然后单击它,则会复制并粘贴新的相应颜色。
现在我已经设置了(或者至少正在尝试),以便每个 ColourPicker 的十六进制代码也输入到 data.frame 中,并且 data.frame 由与正确索引匹配的 rclipButtons 读取了解要复制哪些十六进制代码。
我认为问题在于 data.frame 没有随着我的数字输入的数字的增加而增长,但我不知道为什么。有人可以帮忙吗?谢谢!
这是我的代码:
library(shiny)
library(shinythemes)
library(sortable)
library(colourpicker)
library(glue)
library(png)
library(dplyr)
library(DT)
library(rclipboard)
ui <- fluidPage(
rclipboardSetup(),
numericInput("num_conds",
label = h3("Enter the number of treatments/ conditions"),
min = 1,
max = 20,
value = 1),
uiOutput("cond_colors"),
htmlOutput("cond_buttons", align = 'center'),
# UI ouputs for the copy-to-clipboard buttons
uiOutput("clip"),
# A text input for testing the clipboard content.
textInput("paste", "Paste here:")
)
server <- function(input, output, session){
#####Number output for number of conditions#####
output$value = renderPrint({ input$num_conds })
#### Color selection for UI input####
output$cond_colors <- renderUI({
num_conds <- as.integer(input$num_conds)
lapply(1:num_conds, function(i) {
colourInput(paste0("colors", i),
label = (paste0("Select a color for condition ", i)),
show = c("both"),
value = "black",
palette = c("limited"),
)
})
})
#### Create action buttons for conditions to be selected####
output$cond_buttons <- renderUI({
num_conds = as.integer(input$num_conds)
lapply(1:num_conds, function(i) {
bg = input[[paste0("colors", i)]]
style = paste0(
collapse = " ",
glue("background-color:{bg};
color:#ffffff;
border-color:#000000")
)
label = input[[paste0("condID", i)]]
hex_df = data.frame(input[[paste0('colors', i)]])
cond_buttons = rclipButton(paste0("cond_buttons", i),
label = label,
style = style,
clipText = hex_df[1,i],
icon = NULL)
})
})
}
shinyApp(ui = ui, server = server)
最佳答案
一个快速解决方法是完全删除 hex_df
,并更改 clipText
参数:
clipText = input[[paste0('colors', i)]]
关于r - 使用 Shiny 中的 rclipButton 从数据框中复制文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72624486/