使用get()在Shiny R

时间:2019-05-14 09:58:38

标签: r shiny

我的Shiny应用程序中有n个sliderInput对象,每个对象的ID由df$category[n]给定。这些sliderInput对象具有由df $ value [n]给出的起始值。我想定义一个新的反应数据框架,其中的值由滑块值给出。下面的代码适用于您显式调用sliderInput对象的情况。

如何间接调用sliderInput对象?

df <- data.frame(category = c("Coffee", "Grinder", "Machine"),
                 unitcost = c(1, 3, 8),
                 value = round(runif(3, min = 0, max = 50)))

ui <- ...
         sliderInput(df$category[1], paste(df$category[1]), 
                     value = df$value[1],
                     min = 0,
                     max = maximum),
         sliderInput(df$category[2], paste(df$category[2]), 
                     value = df$value[2],
                     min = 0,
                     max = maximum),
         sliderInput(df$category[3], paste(df$category[3]), 
                     value = df$value[3],
                     min = 0,
                     max = maximum)
   ...

server <- function(input, output) {
   ...
    new_df <- reactive({
    return(as_tibble(df) %>%
    mutate(value = c(input$Coffee, input$Grinder, input$Machine))
    )
  ...
  })

函数get()应该允许我间接调用对象,但这对我不起作用。我尝试了以下方法但没有成功

    new_df <- reactive({
    return(as_tibble(df) %>%
    mutate(value = c(get(paste0("input$",df$category[1])),
                     get(paste0("input$",df$category[2])),
                     get(paste0("input$",df$category[3]))
    )

2 个答案:

答案 0 :(得分:1)

get()将返回一个可以在当前环境中找到的名为“ input $ Coffee”的对象,而不是ID为input$Coffee的输入结构的值。 您应尝试使用以下代码替换您的代码:

 new_df <- reactive({
    return(as_tibble(df) %>%
    mutate(value = c(get("input")[[ df$category[1] ]],
                     get("input")[[ df$category[2] ]],
                     get("input")[[ df$category[3] ]]
    )

答案 1 :(得分:1)

您可以使用reactiveValuesToList()中的shiny。我会避免使用get(),这在shiny中可能很困难,这是由于通过反应性创建的环境以及服务器组件是除全局环境之外的自身环境。另外,不需要粘贴,并且由于[是矢量化的,因此您可以将其传递给列表,而不是使用c()并多次调用。您也不需要显式调用return(),因为reactive()的作用就像一个函数一样,并且会自动返回。

server <- function(input, output) {

  new_df <- reactive({
    as_tibble(df) %>%
      mutate(value = reactiveValuesToList(input)[df$category[1:3]])
  })

}

您还可以使用:

server <- function(input, output) {

  new_df <- reactive({
    as_tibble(df) %>%
             mutate(value = reactiveValuesToList(input)[category])
  })
}

之所以可行,是因为您处于管道序列中,并且无需引用category就可以获取df的值。

更新

这是一个完整的工作示例,包括呈现表格以验证准确性。

library(magrittr)
library(tibble)
library(dplyr)
library(shiny)
df <- data.frame(category = c("Coffee", "Grinder", "Machine"),
                 unitcost = c(1, 3, 8),
                 value = round(runif(3, min = 0, max = 50)))

ui <- fluidPage(sliderInput(df$category[1], paste(df$category[1]), 
            value = df$value[1],
            min = 0,
            max = 1000),
sliderInput(df$category[2], paste(df$category[2]), 
            value = df$value[2],
            min = 0,
            max = 1000),
sliderInput(df$category[3], paste(df$category[3]), 
            value = df$value[3],
            min = 0,
            max = 1000),
tableOutput("table"))

server <- function(input, output) {

  new_df <- reactive({
    as_tibble(df) %>%
             mutate(value = reactiveValuesToList(input)[category])
  })

  output$table <- renderTable({new_df()})
}

shinyApp(ui,server)

enter image description here