R Shiny: Using variables from output for further c

2019-08-27 16:12发布

问题:

My shiny app looks as follows so far (extract):

ui <- fluidPage(


 headerPanel("title"),

   sidebarLayout(

         sidebarPanel(


              h4("header"),
              tags$hr(),


  # Input: Bilanzpositionen Passiv ----

      fileInput("file1", "Kollektive hochladen",
                multiple = TRUE,
                accept = c("text/csv",
                           "text/comma-separated-values,text/plain",
                           ".csv")),
)
)
)
# # # # # 

server <- function(input, output) {



  output$contents <- renderTable({


    req(input$file1)

    bipop <- read.csv(input$file1$datapath,

                  sep = input$sep,
                  quote = input$quote)

    if(input$disp == "head") {
      return(head(bipop))
    }
    else {
      bipop
    }


  })

}

Later on in code (server) I want to extract some data from the Input "bipop" to create some new tables which shall be part of another output.

My trial

monate <- bipop[,4]

doesn't work: "Error: ... undefined columns selected..." and "Error: object ... not found"

How can I define the variable "bipop" as global, so that I can use it outside of the code from "output"?

Thanks.

回答1:

To expand @A.Suliman's comment, here is a complete example.

library(shiny)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      h4("header"),
      tags$hr(),

      # Input: Bilanzpositionen Passiv ----
      fileInput("file1", "Kollektive hochladen",
                multiple = TRUE,
                accept = c("text/csv",
                           "text/comma-separated-values,text/plain",
                           ".csv"))
    ),

    mainPanel(
      tableOutput("contents"),
      verbatimTextOutput("test")
    )

  )
)

# # # # # 

server <- function(input, output) {

  bipop <- eventReactive(input$file1, {
    read.csv(input$file1$datapath)
  })

  output$contents <- renderTable({
    bipop()
  })

  output$test <- renderPrint({
    summary(bipop())
  })

}

shinyApp(ui, server)