【问题标题】:Reactive column names in reactive data frame shiny反应式数据框中的反应式列名闪亮
【发布时间】:2015-03-21 03:56:18
【问题描述】:

我想创建一个反应性数据框,其反应性列名称为闪亮。但是,这是抛出错误。我提供了下面的代码。错误是由 () 后跟 = 引起的,但我找不到解决方法。任何帮助将不胜感激

ui.R

library(shiny)
shinyUI(fluidPage(
  titlePanel("Tool"),
  sidebarLayout(
    sidebarPanel(
      textInput("Item","Enter Item Name"),
      div(class='row-fluid',
          div(class='span6', numericInput("sales1","Enter Sales",value=0),numericInput("sales2","Enter Sales",value=0)),
          div(class='span6', numericInput("prices1","Enter price",value=0),numericInput("prices2","Enter price",value=0))
      )),
    mainPanel(
      dataTableOutput("table")
      )
  )
  ))

server.R

library(shiny)
shinyServer(function(input, output) {
  prices<-reactive({
    c(input$prices1,input$prices2)
  })
  sales<-reactive({
    c(input$sales1,input$sales2)
  })
  combined<-reactive({
    data.frame(prices(),sales())
  })
  combined_final<-reactive({
    mutate(combined(),Rev=prices()*sales())
  })
  namerev<-reactive({
    as.character(paste("Rev",input$Item,sep="_"))
  })
  combined_final_rename<-reactive({
    rename_(combined_final(),namerev() ="Rev")
  })
  output$table<-renderDataTable({
    combined_final_rename()
  })
  })

【问题讨论】:

  • 只是出于好奇——为什么需要这么多reactive 表达式?在您的示例中,一个组合 reactive() 就可以了...
  • 实际上这是更大代码的一部分。我必须创建一个小例子。你完全正确地指出有太多的反应性表达。这是一项仓促的工作。

标签: r shiny


【解决方案1】:

如果我正确理解了这个问题,您可能需要这样的东西:

  combined_final_rename<-reactive({
    d <- combined_final()
    colnames(d)[colnames(d)=='Rev'] <- namerev()
    d
  })

【讨论】:

    猜你喜欢
    • 2015-05-13
    • 2016-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-22
    • 1970-01-01
    • 2017-04-13
    • 2013-07-16
    相关资源
    最近更新 更多