[英]Shiny reactive dataset within observeEvent
I have a very simple app which fails. 我有一个非常简单的应用程序,但失败了。 The reason it fails is that the reactive dataset is available solely within the observeEvent function but not outside.
它失败的原因是反应性数据集仅在observeEvent函数内可用,而在外部则不可用。 I use observeEvent to get datasets from two different sources wrangled.
我使用observeEvent从两个不同的来源获取数据集。 For this example I simply used cbind.
对于此示例,我仅使用了cbind。 My actual code is much more complicated.
我的实际代码要复杂得多。
This is a logical / syntax related problem but all my searching came up short. 这是一个与逻辑/语法有关的问题,但我的所有搜索都很短。 In essence I want merged_data() to be available for all parts of the app.
本质上,我希望merged_data()可用于应用程序的所有部分。
Minimum repr example - this fails because merged_data() is not available outside of the ObserveEvent. 最小repr示例-之所以失败,是因为merged_data()在ObserveEvent之外不可用。
library(shiny)
library(shinyjs)
library(DT)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("testing 1 2 3"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
),
# Show a plot of the generated distribution
mainPanel(
fluidRow(
column(width = 2,
offset = 0,
align = "center",
actionButton(inputId = "fetch_data_inputId",
label = "data")
) #column
,
column(width = 10,
offset = 0,
align = "center",
DT::dataTableOutput("DT1")
) #column
)#fluidrow
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output,session) {
observeEvent(input$fetch_data_inputId, {
req(iris)
button_data <- colnames(iris)
merged_data <- reactive({
if( !is.null(cbind(iris[,1:4],iris3))) {
cbind(iris[,1:4],iris3)
} else {NULL}
})
}) #observeevent
output$DT1 <- renderDataTable({#
rendered_table <- merged_data()
DT::datatable(rendered_table)
})
}
# Run the application
shinyApp(ui = ui, server = server)
Minimum repr example - this works because the datatable is created within the ObserveEvent. 最小再版的例子-这个工作 ,因为该数据表上的ObserveEvent内创建。
library(shiny)
library(shinyjs)
library(DT)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("testing 1 2 3"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
),
# Show a plot of the generated distribution
mainPanel(
fluidRow(
column(width = 2,
offset = 0,
align = "center",
actionButton(inputId = "fetch_data_inputId",
label = "data")
) #column
,
column(width = 10,
offset = 0,
align = "center",
DT::dataTableOutput("DT1")
) #column
)#fluidrow
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output,session) {
observeEvent(input$fetch_data_inputId, {
req(iris)
button_data <- colnames(iris)
merged_data <- reactive({
if( !is.null(cbind(iris[,1:4],iris3))) {
cbind(iris[,1:4],iris3)
} else {NULL}
})
output$DT1 <- renderDataTable({#
rendered_table <- merged_data()
DT::datatable(rendered_table)
})
}) #observeevent
}
# Run the application
shinyApp(ui = ui, server = server)
What I really need is for the reactive dataset to continue being created within observeEvent but to be accessible outside of the ObserveEvent environment so that i use it in other parts of the app, but I suspect it's the wrong approach. 我真正需要的是让反应性数据集继续在observeEvent中创建,但是可以在ObserveEvent环境之外访问,以便在应用程序的其他部分中使用它,但是我怀疑这是错误的方法。 So anything that works would be great.
因此,任何可行的方法都会很棒。
library(shiny)
library(shinyjs)
library(DT)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("testing 1 2 3"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
),
# Show a plot of the generated distribution
mainPanel(
fluidRow(
column(width = 2,
offset = 0,
align = "center",
actionButton(inputId = "fetch_data_inputId",
label = "data")
) #column
,
column(width = 10,
offset = 0,
align = "center",
DT::dataTableOutput("DT1")
) #column
)#fluidrow
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output,session) {
merged_data <- eventReactive(input$fetch_data_inputId, {
req(iris)
button_data <- colnames(iris)
if( !is.null(cbind(iris[,1:4],iris3))) {
cbind(iris[,1:4],iris3)
} else {NULL}
}) #eventReactive
output$DT1 <- renderDataTable({#
rendered_table <- merged_data()
DT::datatable(rendered_table)
})
}
# Run the application
shinyApp(ui = ui, server = server)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.