簡體   English   中英

為 Shiny 使用分配內部反應值?

[英]using assign inside reactiveValues for Shiny?

我想知道,為什么assign函數在reactiveValues 中不起作用? 我需要應用一個函數來為向量中的所有元素定義reactiveValues (零)。 這些元素是事先不知道的,因為它們是最初打開的 csv 文件中所有變量的列名。 所以我不能簡單地一一設置值。 任何建議都受到高度贊賞。

ui <- shinyUI(fluidPage(

  titlePanel("Wondering"),

  sidebarLayout(

    sidebarPanel(
        actionButton(inputId = "add_one", label = "", icon = icon("plus"))
      ),

    mainPanel(
      tabsetPanel(
        tabPanel("Simple example",textOutput("test1"))
      )
    )
  )
)
)

##########
# SERVER #
##########

server <- shinyServer(function(input, output) {

  col_names <- c("A", "B")

  # Assign zeros to all elements from col_names
  # Please, use the assign function to do that!
  rv <- reactiveValues(

    # 1 How can I assign the initial value of zero to all column names? 

    # This is easy:
    A = 0, B = 0

    # But, in reality, in my app I do not know the variable names in advance, I just extract them and save
    # in the col_names vector. Now, I need to assign initial value of zero to all column names

    # I thought this might work, but no luck: All arguments passed to reactiveValues() must be named.
    #for (k in 1:length(col_names)){
    #
    #  assign(col_names[k], 0)  
    #}

    )

  # Sure, I will later have to figure out how to define observeEvent(s) for the unknown number of column names, but I am not there yet...
  observeEvent(input$add_one, {
    rv$A <- rv$A + 1
  })

  observeEvent(input$add_one, {
    rv$B <- rv$B + 1
  })

  # Output text
  output$test1 <-renderText({

    paste(rv$A, rv$B)

  })

})

shinyApp(ui = ui, server = server)

你可以這樣做:

rv <- reactiveValues()
for(colname in col_names){
  rv[[colname]] <- 0
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM