在R-Shiny中将函数传递给Reactives

时间:2018-03-07 15:11:33

标签: r function shiny shiny-server

问题:

我有兴趣在闪亮的网络应用中显示三个图表。我已经写了三个函数来绘制指定范围的每种类型的图。函数的参数将输入参数,区域和子集设置为该指定区域的较大data.frame并随后对其进行图形化。

我很难将此功能传递到r闪亮的反应元素中。

这是我的功能:

plot_30cumulative <- function(enter_region) { 

  subset <<- HPF[HPF$region == enter_region, ]

  thirty_year_cumulative <<- ggplot(subset, aes(x=subset$date)) + 
    geom_line(aes(y = subset$BaseCumulative, color = "Base"), color = "green") +
    geom_line(aes(y = subset$StressCumulative, color = "Adjusted"), color = "red", linetype = "dashed") +
    theme_bw() + 
    scale_x_date(name = "",
                 date_labels = "%Y-%m-%d",
                 limits = as.Date(c("2014-02-15", "2044-02-15")), 
                 breaks = seq(as.Date("2014-02-15"), as.Date("2044-02-15"), by = "1 year")) +
    scale_y_continuous(name = "Cumulative Growth Rates",
                       breaks = seq(min(subset$StressCumulative),max(subset$BaseCumulative), .25),
                       limits = c(NA, 4),                                                                   # Temporarily hard-coded for better visability.
                       labels = percent) +
    theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
    ggtitle("Cumulative 30-Year Growth Rates")

我想要的是什么:

library(shiny)

#Define UI for dataset
ui <- fluidPage(

  #App title ----
  titlePanel("Home Price Forecasting under Stress Scenarios"),

  sidebarLayout(

    sidebarPanel(

      #Input: Stress Path Function Parameters ----

      #Input: Numeric entry for region to plot ----
      numericInput(inputId = "region",
                   label = "Enter Region Number:",
                   value = "1", 
                   min = 1, 
                   max = 110)

    ),

    # Main panel for displaying outputs ----
    mainPanel(plotOutput(outputId = "ThYrC"),
              plotOutput(outputId = "FiYrC"), 
              plotOutput(outputId = "FoYrQtr")
    )

  )
)


#Define server logic to summarize and view selected dataset ----
server <- function(input, output){


       output$ThYrC <- renderPlot({reactive(plot_30cumulative(enter_region == input$region)}))

  }                   

# Run the application ----
shinyApp(ui = ui, server = server)

问题:

我似乎无法通过enter_region将函数参数input$region == enter_region传递给反应元素。

对此问题的任何见解将不胜感激!

0 个答案:

没有答案