如何在eventReactive环境中显示矢量的文本内容?

时间:2019-08-29 11:01:03

标签: r shiny lapply

我试图获得一个带有不同字符串的向量,以显示缩写词的组合,就像它们现在出现在饼图中一样。每当我尝试显示InputList的输出时,都会发生错误。

缩写是通过“地理”和“世界地区”的不同输入字段创建的。然后将它们与下划线放在一起以得到最终的字符串。然后,这些字符串(每增加一行输入将显示一个字符串)应显示为文本/列表。

这是我的代码的MWE:

# library(packages,etc.)

GeographyList <- c("Africa","Asia","Europe")
WorldRegionList <- c("Region1", "Region2")

ui <- fluidPage(#....design etc.,

  # this is just a demo to show the input values
  mainPanel(
    uiOutput("inputwidgets"),

    actionButton('number', 
                 'Add row'),
    actionButton('delete_number', 
                 'Delete row'),
    actionButton("update", "Update View"),

    h4("allocation"),
    plotOutput("allocation"),
    textOutput("labels"))



)

server <- function(input, output) {

  # (For remove button) Reactive value that is triggered by add and remove button
  reac <- reactiveValues()


  observeEvent(c(input$number,input$delete_number), {
    # you need to add 1 to not start with 0
    add <- input$number+1
    # restriction for delete_number > number
    delete <- if(input$delete_number > input$number) add else input$delete_number
    calc <- add - delete
    reac$calc <- if(calc > 0) 1:calc else 1
  })
  # Get new input by clicking Add Row
  observe({
    req(reac$calc)
    output$inputwidgets = renderUI({
      input_list <- lapply(reac$calc, function(i) {
        Geography <- input[[paste0("Geography",i)]]
        Region <- input[[paste0("WorldRegion",i)]]
        amount <- input[[paste0("amount",i)]]

        fluidRow(
          column(2,
                 selectInput(paste0("Geography", i),
                             label = paste0("Geography", i),
                             choices = GeographyList,
                             multiple = FALSE,
                             selected = if(!is.null(Geography)) Geography)
          ),
          column(3,
                 selectInput(paste0("WorldRegion", i),
                             label = paste0("World Region", i),
                             choices = WorldRegionList,
                             multiple = FALSE,
                             selected = if(!is.null(Region)) Region)),
          column(3, 
                 # Input: Specify the amount ----
                 numericInput(
                   paste0("amount",i),
                   label="Amount",
                   value = if(!is.null(amount)) amount else 0
                 )
          )
        )
      })
      do.call(tagList, input_list)
    })
  })

  InputList <- eventReactive(input$update,{ 
    lapply(1:input$number, function(i) { paste0(
      # desired abbreviations needed to store in a list
      switch(eval(parse(text=paste0("input$Geography",i))),
             "Africa"="Afr_",
             "Asia"="AS_",
             "Europe"="EU_"

             , ignoreNULL=FALSE),

      switch(eval(parse(text=paste0("input$WorldRegion",i))),
             "Region1"="Em",
             "Region2"="Dev"
             ,ignoreNULL=FALSE))
    })

    #InputList <-paste0(eval(parse(text=paste0("input$Geography",i))),eval(parse(text=paste0("input$WorldRegion",i))))
  })

  output$labels <- renderText({ paste0(InputList()) })

  # List with the desired abbreviations, 1 abbreviation for each row



  allocation <- eventReactive(input$update, {
    x <- c(input$amount1, input$amount2, input$amount3)
    lbls <- c(paste0(InputList()))
    pie(x, labels = lbls)
  })
  output$allocation <- renderPlot({
    if (input$update == 0)
      return()

    (allocation())
  })

}
# Create Shiny app ----
shinyApp(ui = ui, server = server)

当我尝试将InputList显示为简单文本时,出现错误Object of type closure is not subsettable

非常感谢。我是Shiny的初学者,所以我将感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

当您的应用启动时,input$number的值为0,因此您生成的lapply的{​​{1}}不会循环。单击lbls后,一切正常。除此之外,您输入的UI比创建的标签多,因为您从一个UI开始,但是它的索引是0,所以在UI5,您有索引4,并且正在生成4个而不是5个标签。

我添加了两个add调用来帮助调试。请参见下面的两个代码块。

首先检查cat操作按钮的值。

add

然后检查 InputList <- eventReactive(input$update, { cat("The value of input$number is: ", input$number, "\n\n") lapply(1:input$number, function(i) { paste0(# desired abbreviations needed to store in a list switch( eval(parse(text = paste0( "input$Geography", i ))), "Africa" = "Afr_", "Asia" = "AS_", "Europe" = "EU_", ignoreNULL = FALSE ), switch( eval(parse(text = paste0( "input$WorldRegion", i ))), "Region1" = "Em", "Region2" = "Dev", ignoreNULL = FALSE )) }) #InputList <-paste0(eval(parse(text=paste0("input$Geography",i))),eval(parse(text=paste0("input$WorldRegion",i)))) }) 的内容。

lbls

这是运行应用程序时的控制台输出:

  1. 未添加行,但单击更新:

enter image description here

  1. 单击一次添加并单击更新(生成地理区域2)时:

enter image description here

  1. 添加4行并单击“更新”(生成地理位置5)后:

enter image description here

答案 1 :(得分:0)

感谢@teofil的调试帮助,我已经弄清楚了。 我没有使用TCEFORM { tx_rwfm_domain_model_website { [field_of_related_table].PAGE_TSCONFIG_ID = 12 } tx_rwfm_domain_model_websitecategory { [field_of_related_table].PAGE_TSCONFIG_ID = 28 } } 来访问input$number的最后一个值(即显示的输入行数)。

以下是其他人遇到类似问题的代码:

tail(reac$calc, n=1)

现在,所有字符串都将显示!