【问题标题】:Passing on data and input within a shiny app在闪亮的应用程序中传递数据和输入
【发布时间】:2018-09-11 04:04:22
【问题描述】:

我试图简化我闪亮的应用程序。但是,尽管我尝试了它,但它并没有像我希望的那样工作。 我的想法是将数据加载到应用程序,执行一些分析并将中间结果返回给用户。目前我必须加载数据,为我生成的每个输出选择正确的列等:

ui <- shinyServer(
  fluidPage(
    tabsetPanel(

      tabPanel("Data upload",
               titlePanel("Data upload"),
               sidebarLayout(
                sidebarPanel(
                  fileInput("file1", "Choose CSV File",multiple = TRUE, accept = c("text/csv","text/comma-separated-values,text/plain",".csv")),
                  tags$hr(),
                  checkboxInput("header", "Header", TRUE), radioButtons("sep", "Separator", choices = c(Comma = ",", Semicolon = ";",Tab = "\t"), selected = ","),
                  tags$hr(),
                  checkboxInput("disp", "Display",TRUE),
                  tags$hr(),

                  uiOutput("choose_first_column"),
                  uiOutput("choose_second_column"),
                  br()

                ),
                mainPanel(
                  tableOutput("contents"),
                  tags$hr(),
                  tableOutput("tab")
                  )
               )
          ),
        tabPanel("2","2"
    )  

)
)
)


server <- shinyServer(
function(input, output) {

  observe({

    req(input$file1)

    df <-  read.csv(input$file1$datapath,
                   header = input$header,
                   sep = input$sep,
                   quote = input$quote)

    output$contents <- renderTable({
                      (head(df))})

    output$choose_first_column <- renderUI({
        colnames <- names(df)
        selectInput("column_1", "Choose Date column", 
                choices  = colnames,
                selected = colnames)})

    output$choose_second_column <- renderUI({
        colnames <- names(df)
        selectInput("column_2", "Choose Variable column", 
                choices  = colnames,
                selected = colnames)})

    output$tab <- renderTable({         

        req(input$file1)

        df2 <-  read.csv(input$file1$datapath,
                       header = input$header,
                       sep = input$sep,
                       quote = input$quote)

        df2 <- df2[, c(input$column_1,input$column_2), drop = FALSE]    

        return(head(df2))})

  })
  })  

runApp(list(ui = ui, server = server))

它可以工作,但由于我通常有很多数据并且我想执行一些分析,因此为每个“输出内容”加载和处理数据会非常耗时。 有没有办法避免这种情况?例如,我可以像第二个示例那样加载数据并在全局范围内选择正确的列吗? (我划掉了出错的地方)

ui <- shinyServer(
  fluidPage(
    tabsetPanel(

      tabPanel("Data upload",
               titlePanel("Data upload"),
               sidebarLayout(
                sidebarPanel(
                  fileInput("file1", "Choose CSV File",multiple = TRUE, accept = c("text/csv","text/comma-separated-values,text/plain",".csv")),
                  tags$hr(),
                  checkboxInput("header", "Header", TRUE), radioButtons("sep", "Separator", choices = c(Comma = ",", Semicolon = ";",Tab = "\t"), selected = ","),
                  tags$hr(),
                  checkboxInput("disp", "Display",TRUE),
                  tags$hr(),

                  uiOutput("choose_first_column"),
                  uiOutput("choose_second_column"),
                  br()

                ),
                mainPanel(
                  tableOutput("contents"),
                  tags$hr(),
                 tableOutput("tab")
                  )
               )
          ),
        tabPanel("2","2"
    )  

)
)
)


server <- shinyServer(
function(input, output) {

  observe({

    req(input$file1)

    df <-  read.csv(input$file1$datapath,
                   header = input$header,
                   sep = input$sep,
                   quote = input$quote)

    output$contents <- renderTable({
                      (head(df))})

    output$choose_first_column <- renderUI({
        colnames <- names(df)
        selectInput("column_1", "Choose Date column", 
                choices  = colnames,
                selected = colnames)})

    output$choose_second_column <- renderUI({
        colnames <- names(df)
        selectInput("column_2", "Choose Variable column", 
                choices  = colnames,
                selected = colnames)})

#   df <- df[, c(input$column_1,input$column_2), drop = FALSE]  
#               
#   output$tab <- renderTable({
#                      (head(df))})

  })
  })  

runApp(list(ui = ui, server = server))

输入数据示例:

date    time    level
01.01.2000  00:00:00    0.3724
01.01.2000  01:00:00    0.192
01.01.2000  02:00:00    -0.0252

如果有任何帮助,我将不胜感激! 爱舍

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    据我了解,您收到错误消息,因为您定义的数据帧 df 不是反应式的。您应该使其具有反应性,因为每次用户选择输入列时它都会改变。

    请参阅this 以了解反应性。将代码的已删除部分更改为:

    df.selected.columns <- df[c(input$column_1,input$column_2)]  
    
    output$tab <- renderTable({
                          (head(df.selected.columns()))
                  })
    

    【讨论】:

      猜你喜欢
      • 2014-04-11
      • 1970-01-01
      • 2021-05-25
      • 2020-06-03
      • 1970-01-01
      • 2014-12-29
      • 2020-11-05
      • 1970-01-01
      • 2022-11-20
      相关资源
      最近更新 更多