1

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

在此处输入图像描述

示例代码:

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

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.).

有什么办法可以做我想要做的,或者有更好的方法吗?我没主意了。

4

1 回答 1

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)

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

于 2018-08-14T15:10:07.743 回答