【问题标题】:Shiny UI and Server Loops闪亮的 UI 和服务器循环
【发布时间】:2018-08-14 23:10:07
【问题描述】:

我正在尝试创建如下所示的内容:

示例代码:

图书馆(闪亮) 库(闪亮的仪表板)

data <- data_frame(
  category = c(rep("Superhero outfits", 5), "Game of Thrones houses", rep("My favourite bars", 2), "I am groot"),
  keywords = c("superman + red + cape", "Batman + black + cape", "Hulk + green + purple", "Spiderman + web", "Groot + wood + plant", "wolf + stark",
               "chocolate + nuts", "protein + cookie", "I + groot"),
  author = c(rep("Preter Parker", 3), rep("Bruce Wayne", 5), "Groot"),
  date = c(rep(Sys.Date()))
)

shinyApp(
  ui = dashboardPage(
    dashboardHeader(title = "MyApp"),
    dashboardSidebar(),
    dashboardBody(
       column(
         selectizeInput("selectCategory", NULL, choices = unique(data$category)),
         uiOutput("displayChoicesUI"),
         width = 4
     ) 
    )
  ),
  server = function(input, output) {

    choices_list <- reactive({
      if(!is.null(input$selectCategory)){
      data %>%
      filter(category == input$selectCategory) %>%
      mutate(ID = row_number())
      }
    })

    output$displayChoicesUI <- renderUI({
      if(!is.null(input$selectCategory)){

        lapply(1:nrow(choices_list), function(i){
          column(
            width = 12,
            column(
              width = 8,
              verbatimTextOutput(paste0("text_", choices_list$ID[i]))
            ),
            column(
              width = 2,
              actionLink(paste0("delete_", choices_list$ID[i]), HTML("<i class='fa fa-times-circle'></i>"))
            ),
            column(
              width = 2,
              actionLink(paste0("edit_", choices_list$ID[i]), HTML("<i class='fa fa-edit'></i>"))
            )
          )
        })
      }
    })

    lapply(1:????, function(i){
                      output[[paste0("text_", i)]] <- renderText({
                        HTML(paste0(choices_list()$keywods[i], br(), choices_list()$author, ", ", choices_list()$date))
                      })
                    })

    lapply(1:????, function(i){
          observeEvent(input[[paste0("delete_", i)]], {
          })
    })

    lapply(1:????, function(i){
      observeEvent(input[[paste0("edit_", i)]], {
      })
    })


  }
)

问题是,由于我不知道该类别中会有多少关键字,我如何循环遍历它以创建反应性 - 输出关键字和作者并使按钮起作用。

迭代次数是一个反应值,但是我不能在 lapply 中添加一个反应值。像lapply(1:nrow(choices_list()), function(i){}) 这样做某事给我Evaluation error: Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.).

有什么方法可以让我达到预期目的,或者有更好的方法吗?我没有想法。

【问题讨论】:

    标签: r shiny shiny-reactivity


    【解决方案1】:

    将响应值添加到您的服务器,然后您可以遍历它们并在响应事件时更改它们。您可以绑定应用到操作按钮。例如添加:

    到用户界面:

    actionButton("press","Press me",icon=icon("smile", lib = "font-awesome"))
    

    到服务器:

    categorySuperHero <- reactiveValues(data.frame(words=c("cape","superman")))
    observeEvent(input$search,{
      categorySuperHero$words <- rbind(categorySuperHero$words,input$search) 
       ## assuming you search one word, otherwise you can break the string and loop through it
    })
    observeEvent(input$press,{
       x <- apply(categorySuperHero$words,1,function(x){"ur function"})
    })
    

    如果您不希望该用户需要按下按钮,您可以模拟(在某些情况下)按下操作按钮:

    click(input$press)
    

    我希望我正确理解了您的问题并且这会有所帮助。

    【讨论】:

    • 这不是我的想法(或者我不明白你的意思:)),但它让我知道了如何解决我的部分问题
    • 一旦您将反应值保存在某个变量中,您就可以轻松更新小部件并将该反应变量用作输入(我正在考虑用于类别的选择小部件)。您可以添加“updateSelectInput”(谷歌搜索),对于选择,您可以传递该反应变量。
    猜你喜欢
    • 2016-05-23
    • 2018-02-16
    • 2014-11-26
    • 2017-01-15
    • 2016-10-26
    • 2021-11-07
    • 2021-03-10
    • 1970-01-01
    • 2018-04-21
    相关资源
    最近更新 更多