造型闪亮的动作按钮,具有从lapply函数和用户输入创建的可变名称

发布于 2025-02-05 18:10:37 字数 2564 浏览 3 评论 0原文

我有一个数字输入,可以根据用户选择的编号创建一定数量的文本框和颜色选择菜单。这也创建了一定数量的动作按钮。我想做的是根据输入文本框中的条件名称对按钮进行更新,并且按钮的背景颜色是用户在颜色的颜色中所选择的颜色。

这是我最适合可重复性目的尝试的裸露版本(真实的东西看起来更性感):

library(shiny)
library(shinythemes)
library(sortable)
library(colourpicker)
library(glue)

ui = fluidPage(
  numericInput("num_conds", 
                     label = h3("Enter the number of treatments/ conditions"),
                     min = 1,
                     max = 20,
                     value = 1),

  uiOutput("boxes_conds"),
  uiOutput("cond_colors"),
  htmlOutput("cond_buttons", align = 'center')
)

server = function(input, output, session) {

  output$value = renderPrint({ input$num_conds })
  output$value = renderPrint({ input$num_fish })

####Condition boxes for UI text input####
  output$boxes_conds = renderUI ({
    num_conds = as.integer(input$num_conds)
    
      lapply(1:num_conds, function(i) {
        cond_names = textInput(paste0("condID", i), 
                        label = paste0("Treatment/ Conditions: ", i), 
                        placeholder = "Enter condition...")
      })
  })

####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) {
    
      style = paste0(collapse = " ",
                  glue("background-color:{input$colors[[i]]};
                  color:#ffffff;
                  border-color:#000000"))
    
    
      cond_buttons = actionButton(paste0("cond_buttons", i), 
                    label = input$condID[[i]],
                   style = style
                   )
    })
  })
  
  output$cond_text <- renderText({
    num_conds = as.integer(input$num_conds)
    
    #lapply(1:num_conds, function(i) {
      start = glue('actionButton("{cond_buttons[[i]]}", "{input$condID[[i]]}",')
      style = glue('background-color:{input$colors[[i]]};"')
    
    glue('{start}\n {"             "}style = "{style})')
  })})
}

# Run the application 
shinyApp(ui = ui, server = server)

现在,我没有任何错误。制作了正确数量的按钮,但是它们不采用颜色选择中的颜色或按钮中的文本不会随文本输入框中的文本而变化。

有人可以帮助我理解我在这里做错了什么吗?还是我想做的事情是必要的?

I have a number input that creates a certain number of text boxes and color selection menus, depending on the number chosen by the user. This also creates a certain number of action buttons. What I want to do is have the buttons be updated based on the condition names that are typed into the text boxes, and the background colors of the buttons be the colors that are selected by the user in the colourPicker.

Here is the bare-bones version of my best attempt for reproducibility purposes (the real thing looks sexier):

library(shiny)
library(shinythemes)
library(sortable)
library(colourpicker)
library(glue)

ui = fluidPage(
  numericInput("num_conds", 
                     label = h3("Enter the number of treatments/ conditions"),
                     min = 1,
                     max = 20,
                     value = 1),

  uiOutput("boxes_conds"),
  uiOutput("cond_colors"),
  htmlOutput("cond_buttons", align = 'center')
)

server = function(input, output, session) {

  output$value = renderPrint({ input$num_conds })
  output$value = renderPrint({ input$num_fish })

####Condition boxes for UI text input####
  output$boxes_conds = renderUI ({
    num_conds = as.integer(input$num_conds)
    
      lapply(1:num_conds, function(i) {
        cond_names = textInput(paste0("condID", i), 
                        label = paste0("Treatment/ Conditions: ", i), 
                        placeholder = "Enter condition...")
      })
  })

####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) {
    
      style = paste0(collapse = " ",
                  glue("background-color:{input$colors[[i]]};
                  color:#ffffff;
                  border-color:#000000"))
    
    
      cond_buttons = actionButton(paste0("cond_buttons", i), 
                    label = input$condID[[i]],
                   style = style
                   )
    })
  })
  
  output$cond_text <- renderText({
    num_conds = as.integer(input$num_conds)
    
    #lapply(1:num_conds, function(i) {
      start = glue('actionButton("{cond_buttons[[i]]}", "{input$condID[[i]]}",')
      style = glue('background-color:{input$colors[[i]]};"')
    
    glue('{start}\n {"             "}style = "{style})')
  })})
}

# Run the application 
shinyApp(ui = ui, server = server)

Right now, I don't get any errors. The correct number of buttons are made, but they do not adopt the colors from the color selection or the text in the buttons doesn't change with the text from the text input boxes.

Can someone help me understand what I am doing wrong here? Or if the way that I am trying to do things is necessary?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独孤求败 2025-02-12 18:10:37

问题在于,包含颜色代码和标签的输入被命名为colors1,colors2,...condid1,condid2,...时,您使用eg 输入$颜色[[i]]返回null。而是做输入[[paste0(“颜色”,i)]] and input [[paste0(“ condid”,i)]]

library(shiny)
library(shinythemes)
library(sortable)
library(colourpicker)
library(glue)

ui <- fluidPage(
  numericInput("num_conds",
    label = h3("Enter the number of treatments/ conditions"),
    min = 1,
    max = 20,
    value = 1
  ),
  uiOutput("boxes_conds"),
  uiOutput("cond_colors"),
  htmlOutput("cond_buttons", align = "center")
)

server <- function(input, output, session) {
  output$value <- renderPrint({
    input$num_conds
  })
  output$value <- renderPrint({
    input$num_fish
  })

  #### Condition boxes for UI text input####
  output$boxes_conds <- renderUI({
    num_conds <- as.integer(input$num_conds)

    lapply(1:num_conds, function(i) {
      cond_names <- textInput(paste0("condID", i),
        label = paste0("Treatment/ Conditions: ", i),
        placeholder = "Enter condition..."
      )
    })
  })

  #### 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)]]
      cond_buttons <- actionButton(paste0("cond_buttons", i),
        label = label,
        style = style
      )
    })
  })

  output$cond_text <- renderText({
    num_conds <- as.integer(input$num_conds)

    # lapply(1:num_conds, function(i) {
    start <- glue('actionButton("{cond_buttons[[i]]}", "{input$condID[[i]]}",')
    style <- glue('background-color:{input$colors[[i]]};"')

    glue('{start}\n {"             "}style = "{style})')
  })
}

# Run the application
shinyApp(ui = ui, server = server)

The issue is that the inputs containing the color codes and the labels are named colors1, colors2, ... and condID1, condID2, ... while you use e.g. input$colors[[i]] which returns NULL. Instead do input[[paste0("colors", i)]] and input[[paste0("condID", i)]]:

library(shiny)
library(shinythemes)
library(sortable)
library(colourpicker)
library(glue)

ui <- fluidPage(
  numericInput("num_conds",
    label = h3("Enter the number of treatments/ conditions"),
    min = 1,
    max = 20,
    value = 1
  ),
  uiOutput("boxes_conds"),
  uiOutput("cond_colors"),
  htmlOutput("cond_buttons", align = "center")
)

server <- function(input, output, session) {
  output$value <- renderPrint({
    input$num_conds
  })
  output$value <- renderPrint({
    input$num_fish
  })

  #### Condition boxes for UI text input####
  output$boxes_conds <- renderUI({
    num_conds <- as.integer(input$num_conds)

    lapply(1:num_conds, function(i) {
      cond_names <- textInput(paste0("condID", i),
        label = paste0("Treatment/ Conditions: ", i),
        placeholder = "Enter condition..."
      )
    })
  })

  #### 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)]]
      cond_buttons <- actionButton(paste0("cond_buttons", i),
        label = label,
        style = style
      )
    })
  })

  output$cond_text <- renderText({
    num_conds <- as.integer(input$num_conds)

    # lapply(1:num_conds, function(i) {
    start <- glue('actionButton("{cond_buttons[[i]]}", "{input$condID[[i]]}",')
    style <- glue('background-color:{input$colors[[i]]};"')

    glue('{start}\n {"             "}style = "{style})')
  })
}

# Run the application
shinyApp(ui = ui, server = server)

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文