您可以根据反应性输入在何处创建数据集,以便稍后在应用中使用

时间:2018-12-11 11:09:33

标签: r shiny

我有一个相对繁重的程序可以根据手动选择的输入运行。我想让我的应用程序使用户选择参数,然后运行,然后创建一个数据库,该数据库可用于基于此数据集创建多个表和图形。但是创建数据库只能发生一次。

到目前为止,根据用户输入,数据库是在运行应用程序之前创建的。有关示例,请参见下面的代码。

countries <- c("BEL", "FRA", "AFG")
el_inf_ex <- 1


df_TEST <-data.frame(iso3= c(rep("BEL", 10),rep("FRA", 10),rep("AFG", 10)), 
                     year= c(seq(2001, 2010), seq(2001, 2010), seq(2001, 2010)),
                     test= rnorm(30)*el_inf_ex)



#The shiney appp has three parts

  ui <- fluidPage(
    # App title ----
    titlePanel("TEST"),

    # Sidebar layout with a input and output definitions ----
    sidebarLayout(

# 1 Where you select user input ----
      sidebarPanel(

        # Input: Selector for choosing dataset ----
        selectInput(inputId = "Country",
                    label = "Choose a country:",
                    choices = countries),

        # Input: Numeric entry for number of obs to view ----
        sliderInput(inputId = "Year",
                    label = "Choose a year:",
                    value = 2018,
                    min = 2000,
                    max = 2010),
        # Input: Numeric entry for number of obs to view ----
        sliderInput(inputId = "el_inf_ex",
                    label = "El(inf,exrate):",
                    value = 0.3,
                    min = 0,
                    max = 1)
      ),
# 2 Where you specify the output ----
        mainPanel(

          # Output: Tabset w/ plot, summary, and table ----
          tabsetPanel(type = "tabs",
                      tabPanel("Data Input No shock", 
                               # Output: HTML table with requested number of observations ----
                               h3("I. One title:"),
                               tableOutput("CI"),
                               h3("II. Second title:"),
                               tableOutput("VUL")
                      )
          )
        )
      )
)


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

      # Return the MonArr variable
      datasetInput_CI <- reactive({
        df_TEST %>% filter(iso3 == input$Country, year == input$Year) %>% summarise(blabla = max(test))
      })
      # Return the Vulnerability variables ----
      datasetInput_Vul <- reactive({
        df_TEST %>% filter(iso3 == input$Country, year == input$Year) 
      })
      output$CI <- renderTable(datasetInput_CI())
      output$VUL <- renderTable(datasetInput_Vul())
    }

  shinyApp(ui = ui, server = server)

因此,我想在代码中创建数据框df_TEST,以便可以在应用程序中选择el_inf_ex,但是用于创建数据集的行只能运行一次。 (在我的实际应用程序中,我将不得不获取其他R文件的源代码)。之后,我想在图形和表格中使用输出(数据帧df_TEST)。

1 个答案:

答案 0 :(得分:0)

我自己找到了解决方案。诀窍是使用功能watch()。在此功能内,可以创建数据集。

我更改了示例的某些部分,以使所有内容均响应按钮“更新”。

countries <- c("BEL", "FRA", "AFG")

#The shiney appp has three parts

  ui <- fluidPage(
    # App title ----
    titlePanel("TEST"),

    # Sidebar layout with a input and output definitions ----
    sidebarLayout(

# 1 Where you select user input ----
      sidebarPanel(

        # Input: Selector for choosing dataset ----
        selectInput(inputId = "Country",
                    label = "Choose a country:",
                    choices = countries),

        # Input: Numeric entry for number of obs to view ----
        sliderInput(inputId = "Year",
                    label = "Choose a year:",
                    value = 2018,
                    min = 2000,
                    max = 2010),
        # Input: Numeric entry for number of obs to view ----
        sliderInput(inputId = "el_inf_ex",
                    label = "El(inf,exrate):",
                    value = 0.3,
                    min = 0,
                    max = 10),      
        actionButton("update", "Update")
      ),
# 2 Where you specify the output ----
        mainPanel(

          # Output: Tabset w/ plot, summary, and table ----
          tabsetPanel(type = "tabs",
                      tabPanel("Data Input No shock", 
                               # Output: HTML table with requested number of observations ----
                               h3("I. One title:"),
                               tableOutput("CI"),
                               h3("II. Second title:"),
                               tableOutput("VUL")
                      )
          )
        )
      )
)


    # Define server logic to summarize and view selected dataset ----
    server <- function(input, output) {
      Output <-  reactiveValues(datasetInput_CI = NULL)
      Output <-  reactiveValues(datasetInput_Vul = NULL)


      storage <- reactiveValues()
      observe({
        storage$df_Test <- data.frame(iso3= c(rep("BEL", 10),rep("FRA", 10),rep("AFG", 10)), 
                            year= c(seq(2001, 2010), seq(2001, 2010), seq(2001, 2010)),
                            test= rnorm(30)*input$el_inf_ex)
      })

      # Return the MonArr variable
      observeEvent(input$update, {
        Output$datasetInput_CI <- storage$df_Test %>% filter(iso3 == input$Country, year == input$Year) %>% summarise(blabla = max(test))
      })
      # Return the Vulnerability variables ----
      observeEvent(input$update, {

        Output$datasetInput_Vul <- storage$df_Test %>% filter(iso3 == input$Country, year == input$Year) 
      })
      output$CI <- renderTable(Output$datasetInput_CI)
      output$VUL <- renderTable(Output$datasetInput_Vul)
    }

  shinyApp(ui = ui, server = server)
相关问题