迭代加载和筛选表[R][s]

迭代加载和筛选表[R][s],r,shiny,shinydashboard,R,Shiny,Shinydashboard,我在Shiny中迭代加载和筛选数据表时遇到问题。理想的工作流程如下: get_data=function(){ # note that this is for sample purpose, real function is MySQL query df=data.frame(x=1:10,Age=1:100) print("loading data...") return(df) } ui = bootstrapPage( fluidPage( fluidRow(

我在Shiny中迭代加载和筛选数据表时遇到问题。理想的工作流程如下:

get_data=function(){ # note that this is for sample purpose, real function is MySQL query
  df=data.frame(x=1:10,Age=1:100)
  print("loading data...")
return(df)
}

ui = bootstrapPage(
  fluidPage(
    fluidRow(
      actionButton(
        inputId = "confirm_button",
        label = "Confirm"
      )
    )
    ,
    fluidRow(
      column(4,

             sliderInput("slider_age", label = h4("Age"), min = 0, 
                         max = 100, value = c(0, 100))
      )
    ),

    hr(),

    fluidRow(
      DT::dataTableOutput("all_background_table") 
    )
  )
)

server = function(input, output){


observeEvent(input$confirm_button, {

  req(input$confirm_button) 


  output$all_background_table <- DT::renderDataTable({

    all_background=get_data() # <- MySQL function to laod data

    # if all_background filter function put here: 
    #--> data is re-loaded by MySQL query

    # if all_background filter function is put here surrounded by observeEvent(input$slider_age, {...:
    #--> there is no change when input$slider_age is changed

    datatable(all_background,
              rownames = FALSE,
              style = "bootstrap")

  })  


})

  observeEvent(input$slider_age, {
    ## this will throw an error requiring all_background
    #--> Error in observeEventHandler: object 'all_background' not found

    req(input$confirmation_load_pts)  

    all_background=all_background[(all_background$Age > as.numeric(input$slider_age[1]) &  all_background$Age < as.numeric(input$slider_age[2])),]

  })

}  

shinyApp(ui, server)
用户按下按钮确认加载数据 从MySql查询中检索数据。注意:这种情况只发生一次 可选过滤器按钮/滑块变为可见/可用 用户与按钮/滑块交互以筛选数据表 1和2的工作很好,但我有特别的问题,4和任何输入为3将不胜感激

不工作的初始代码如下所示:

get_data=function(){ # note that this is for sample purpose, real function is MySQL query
  df=data.frame(x=1:10,Age=1:100)
  print("loading data...")
return(df)
}

ui = bootstrapPage(
  fluidPage(
    fluidRow(
      actionButton(
        inputId = "confirm_button",
        label = "Confirm"
      )
    )
    ,
    fluidRow(
      column(4,

             sliderInput("slider_age", label = h4("Age"), min = 0, 
                         max = 100, value = c(0, 100))
      )
    ),

    hr(),

    fluidRow(
      DT::dataTableOutput("all_background_table") 
    )
  )
)

server = function(input, output){


observeEvent(input$confirm_button, {

  req(input$confirm_button) 


  output$all_background_table <- DT::renderDataTable({

    all_background=get_data() # <- MySQL function to laod data

    # if all_background filter function put here: 
    #--> data is re-loaded by MySQL query

    # if all_background filter function is put here surrounded by observeEvent(input$slider_age, {...:
    #--> there is no change when input$slider_age is changed

    datatable(all_background,
              rownames = FALSE,
              style = "bootstrap")

  })  


})

  observeEvent(input$slider_age, {
    ## this will throw an error requiring all_background
    #--> Error in observeEventHandler: object 'all_background' not found

    req(input$confirmation_load_pts)  

    all_background=all_background[(all_background$Age > as.numeric(input$slider_age[1]) &  all_background$Age < as.numeric(input$slider_age[2])),]

  })

}  

shinyApp(ui, server)

我不确定是否要获取_数据,但我将使用df使其更容易。使用EventResponsive,您可以在使用滑块并仅在单击确认按钮后创建新的数据帧。在这种情况下,不需要您的观察

library(shiny)
library(DT)
get_data=function(){ # note that this is for sample purpose, real function is MySQL query
  df=data.frame(x=1:10,Age=1:100)
  print("loading data...")
return(df)
}
ui = bootstrapPage(
  fluidPage(
    fluidRow(
      actionButton(
        inputId = "confirm_button",
        label = "Confirm"
      )
    )
    ,
    fluidRow(
      column(4,

             sliderInput("slider_age", label = h4("Age"), min = 0, 
                         max = 100, value = c(0, 100))
      )
    ),

    hr(),

    fluidRow(
      DT::dataTableOutput("all_background_table") 
    )
  )
)

server = function(input, output){

  test <- eventReactive(input$confirm_button, {
    df=get_data()


  })  

  observeEvent(input$confirm_button, {

    output$all_background_table <- DT::renderDataTable({
      df=test() 

      all_background2=df[(df$Age > as.numeric(input$slider_age[1]) &  df$Age < as.numeric(input$slider_age[2])),]


      datatable(all_background2,
                rownames = FALSE,
                style = "bootstrap")

    })  


  })

}  

shinyApp(ui, server)

请提供完整的闪亮应用程序。不知道如何使用MySQL查询,但我会编辑以提供一个示例,您可以使用数据的子集。足够重现这个问题了。很抱歉,我仍然没有完全理解…不管怎样,我添加了应用程序布局以及一个示例数据框,虽然不是真实的数据,但仍然可以达到我相信的目的谢谢你的帮助!我收到一条警告:类型为“closure”的$:对象中的错误不是可子集错误。调查人员可能会开始新的会话。我刚刚在代码中添加了库和df=data.framex=1:10,Age=1:100。但它是这样工作的。啊,我认为图书馆没有关门。现在开始测试。谢谢如果我弄糊涂了,我很抱歉,但是我不能在应用程序启动时预装数据。应仅在按下按钮时生成。感谢你迄今为止的帮助,我会继续玩下去的@ben如果我用df=get_数据替换df,效果也是一样的