R Shiny: Using variables from output for further calculation outside of function

我们两清 提交于 2019-12-13 03:17:54

问题


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)


来源:https://stackoverflow.com/questions/51513364/r-shiny-using-variables-from-output-for-further-calculation-outside-of-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!