【问题标题】:Subset a reactive datafrane by selectinput choices that come from the reactive dataframe通过来自反应性数据帧的选择输入选择子集反应性数据帧
【发布时间】:2018-07-31 02:02:46
【问题描述】:

我是闪亮的新手,我想提出一个可能是错误的(理论上的?)问题,但无论如何,搜索后我想知道。

您能否使用基于此数据框创建的selectinput() 的值来对反应式数据框进行子集化?这对我来说似乎是一个悖论,因为每次更新数据框时,selectinput() 也会更新。我将使用我的想法的一个小例子来帮助别人理解。可能我问的很幼稚,但我想知道。

 location = c("100 ail","16th and Whitmore","40AB01 - ANTWERPEN","Abington") 
last = c("2018-02-01 09:30:00", "2018-02-01 03:00:00", "2017-03-07 10:00:00","2018-02-01 03:00:00") 
first = c("2015-09-01 00:00:00","2016-03-06 19:00:00","2016-11-22 15:00:00","2016-06-09 19:00:00") 
locations = data.frame(location, last, first)

library(shiny)
library(shinythemes)
library(htmltools)
library(DT)
library(utilr)
library(openair)
library(dplyr)
library(ropenaq)
library(worldmet)

  ui = fluidPage(theme = shinytheme("slate"),

  # title
  titlePanel("Select Dates of interest and location"),

  # first row; This allows a user to enter their start and stop dates
  fluidRow(
    column(3, wellPanel(

    )
    ),
    column(3, wellPanel(

    )
    ),
      column(
          width = 6

      )
  ),



  fluidRow(
      tabsetPanel(
          type = "tabs",
          # summary tab
          tabPanel(
              "  Select Dates and Location",
              uiOutput("loc"),
              uiOutput("dt"),
              shiny::dataTableOutput("merged")         

          ),                      

          # scenario tab.  Needs work.  Panel should include a summary of user choices (selected on previous panels) and then 
          # allow a user to enter their email address.  An action button would be pressed to create the output from OpenAir.
          tabPanel(
              "Selection Summary and Process Data",
              fluidRow(
                  # actionButton("goButton", "OpenAir Local!"),
                  # helpText("When you click the button above, you should see",
                  #          "the output below update to reflect the value you",
                  #          "entered at the top:"),

                  )


                  )
              )
          )
      )

#server.r
server = function(input, output, session) {
 #initial selectinput to create the initial dataframe
output$loc<-renderUI({
     selectInput("loc", label = h4("Choose location"),
                 choices = locations$location ,selected = 1
     )
   })

    rt<-reactive({
#dataframe creation
    AQ<- aq_measurements(location = input$loc) 
    )
    met <- importNOAA(year = 2014:2018)
    colnames(AQ)[9] <- "date"
    merged<-merge(AQ, met, by="date")

    merged$location <- gsub( " " , "+" , merged$location)
    merged


   })

#selectinput based on the dataframe
output$dt<-renderUI({
     selectInput("dt", label = h4("Choose Dates"), 
                 choices = as.character(rt()[,1]),
                 selected = 1,
                 multiple = T)
   })


#attempt to create a datatable by subseting the dataframe by the choices of selectinput   
   output$merged <- shiny::renderDataTable({
     a <- subset(rt(), rt()[,1] %in% input$dt)
   })
}
shinyApp(ui, server)

【问题讨论】:

  • 您可能可以做到,您能否提供一个最小且完整的shinyApp,以便我们提供更好的帮助?谢谢
  • 我添加了一个简单的可重现
  • 你的例子没有做任何事情。我收到一个错误:This location/city/country combination is not available within the platform. See ?locations.
  • 我编辑了它。这是由于ropenaq API 造成的。它现在运行但速度较慢

标签: r shiny subset selectinputdate


【解决方案1】:

问题不同。 Shiny 无法读取日期时间之间的空格,因此我将其替换为“+”。

 output$loc<-renderUI({
     selectInput("loc", label = h4("Choose location"),
                 choices = locations$location ,selected = 1
     )
   })

   rt<-reactive({
     locations$location <- gsub( " " , "+" , locations$location)

   AQ<- aq_measurements(location = input$loc,date_from = as.Date(as.POSIXct(subset(locations$firstUpdated,(locations[,1]==input$loc))
   ,format="%Y-%m-%d %H:%M:%S")) 
   , date_to = as.Date(as.POSIXct(subset(locations$lastUpdated,(locations[,1]==input$loc)),format="%Y-%m-%d %H:%M:%S")) 
   )
   met <- importNOAA(year = 2014:2018)
   colnames(AQ)[9] <- "date"
   merged<-merge(AQ, met, by="date")
   # date output -- reports user-selected state & stop dates in UI
   merged$location <- gsub( " " , "+" , merged$location)
   merged$date <- gsub( " " , "+" , merged$date)
   merged


   })

   output$dt<-renderUI({
     selectInput("dt", label = h4("Choose Dates"), 
                 choices = as.character(rt()[,1]),
                 selected = 1
                 )
   })

   #DT  

   output$merged <- shiny::renderDataTable({


     subset(rt(),(rt()[,1]==input$dt))


   })

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-25
    • 1970-01-01
    • 2021-01-16
    • 1970-01-01
    • 2017-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多