r-你在哪里创建基于反应输入的数据集,以便稍后在应用程序中使用



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

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

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(。

我自己找到了解决方案。诀窍是使用函数observe((。在此函数中,可以创建数据集。

我更改了示例的某些部分,使所有内容都响应"更新"按钮。

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)

最新更新