【问题标题】:R Shiny: Using variables from output for further calculation outside of functionR Shiny:使用输出中的变量在函数之外进行进一步计算
【发布时间】:2018-07-25 07:38:35
【问题描述】:

到目前为止,我的闪亮应用如下所示(摘录):

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
    }


  })

}

稍后在代码(服务器)中,我想从输入“bipop”中提取一些数据以创建一些新表,这些表将成为另一个输出的一部分。

我的审判

monate <- bipop[,4]

不起作用:“错误:...选择了未定义的列...”和“错误:对象...未找到”

如何将变量“bipop”定义为全局变量,以便在“输出”的代码之外使用它?

谢谢。

【问题讨论】:

  • 您好,请同时添加您的 UI 端。此外,您使用哪些数据?否则这个问题无法回答,因为你的代码示例不完整
  • @5th:我已经编辑了我的问题,@pieca:我认为这在这里没有帮助。上传 csv 文件后,我需要操作“bipop”。
  • 被动读取bipop,即将read.csv放入reactive。然后你可以在任何你想使用bipop()的地方使用bipop

标签: r variables server shiny global


【解决方案1】:

为了扩展@A.Suliman 的评论,这里有一个完整的例子。

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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-05
    • 1970-01-01
    • 2021-10-29
    • 1970-01-01
    • 1970-01-01
    • 2021-03-26
    • 1970-01-01
    • 2021-12-28
    相关资源
    最近更新 更多