с использованием присваивания внутри реактивных значений для Shiny? - PullRequest
2 голосов
/ 22 марта 2020

Мне интересно, почему функция assign не работает внутри реактивных значений? Мне нужно применить функцию, которая будет определять 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)

1 Ответ

1 голос
/ 22 марта 2020

Вы можете сделать:

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