2

我有一个非常简单的应用程序失败了。它失败的原因是反应数据集仅在 observeEvent 函数内可用,而在外部不可用。我使用 observeEvent 从两个不同的来源获取数据集。对于这个例子,我只是使用了 cbind。我的实际代码要复杂得多。

这是一个与逻辑/语法相关的问题,但我的所有搜索都失败了。本质上,我希望 merge_data() 可用于应用程序的所有部分。

最小 repr 示例 - 这会失败,因为 merge_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)

最小 repr 示例 - 这因为数据表是在 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)

我真正需要的是让反应数据集继续在 observeEvent 中创建,但可以在 ObserveEvent 环境之外访问,以便我在应用程序的其他部分使用它,但我怀疑这是错误的方法。所以任何有效的东西都会很棒。

4

1 回答 1

3
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)
于 2017-12-12T14:48:53.970 回答