闪亮:使用服务器输出进一步输入以创建绘图

时间:2015-08-07 19:55:19

标签: r shiny

我将拥有2个以上不同列的数据集。数据集因收集数据的频率而异。数据类型/列可能相同,但有些数据集之间会有所不同。我希望能够一次创建一个包含数据的图。我有用于按收集频率选择数据集以及选择列/数据类型的代码。我还有用于选择用户可能感兴趣的时间窗口的代码,例如,一小时,几个小时等。但是,我不能弄清楚如何使用时间窗口中的输入来提出从所选列中选择适当行所需的索引。这是一个数据样本(我也无法弄清楚如何进行以下代码包装):

structure(list(`TIMESTAMP-TS` = structure(c(1432052400, 1432052700, 1432053000, 1432053300, 1432053600, 1432053900), class = c("POSIXct", "POSIXt"), tzone = ""), `RECORD-RN` = 49178:49183, `Rain_in_Tot-inch` = c(0, 0, 0, 0, 0, 0), `Stage-Feet` = c(-0.03, -0.03, -0.03, -0.03, -0.03, -0.03), `ISCO_Tot-` = c(0, 0, 0, 0, 0, 0)), .Names = c("TIMESTAMP-TS", "RECORD-RN", "Rain_in_Tot-inch", "Stage-Feet", "ISCO_Tot-"), row.names = c(NA, 6L), class = "data.frame")

这是我到目前为止的代码。

ui.R

library(shiny)
library(data.table)

shinyUI(fluidPage(

fluidRow(
column(4, wellPanel(

      helpText("To begin, select the collection frequency you want to 
           view from the dropdown menu. Only one data type at a time is available."),

      selectInput("freqtype", "Collection frequency", choices = c("Five second", 
              "One minute", "Five minute", "Hourly"), size =4, selectize = FALSE),

      uiOutput("dynamicUI1"),

      helpText("Select dates"),
      uiOutput("dynamicUI2"),

      uiOutput("dynamicUI3"),

      textOutput("s2")

)),

fluidRow(
  wellPanel(
      plotOutput("plot")))

)
))

shinyServer(

server.R

function(input, output, session) {
    Inputfreq <- reactive ({
          switch (input$freqtype, "Five second" = names(data.5sec), "One minute" = names(data.min), 
                 "Five minute" = names(data.5min), "Hourly" = names(data.hr))
        })

    Inputfreq2 <<- reactive ({
          switch (input$freqtype, "Five second" = data.5sec, "One minute" = data.min, "Five minute" = data.5min,
                  "Hourly" = data.hr)
        })

    output$dynamicUI1 <- renderUI ({
          selectInput ("type", "Select data type", choices = c(Inputfreq2()[-1]))
        })

    output$dynamicUI2 <- renderUI ({
          selectInput ("start", "Start date", choices = (as.character(as.POSIXct(Inputfreq2()$TIMESTAMP_TS))))
        })

    output$dynamicUI3 <- renderUI ({
          selectInput ("finish", "Finish date", choices = c(as.character(as.POSIXct(Inputfreq2()$TIMESTAMP_TS))))
        })

    output$s2 <- renderText({  #This is here to see what the value was for input$finish
      paste('tim.st =', input$finish)
        })

    output$tim.st <- renderText({ #And here is where things get wonky.
      which(as.character(Inputfreq2()$TIMESTAMP_TS) == input$start)
        })

    output$tim.fi <- observe ({ #Tried something different when I couldn't use renderText
      which(as.character(Inputfreq2()$TIMESTAMP_TS) == input$finish)
        })

    plotOutput(outputId = c(Inputfreq2()$TIMESTAMP_TS[which(Inputfreq2()$TIMESTAMP_TS == input$start:input$finish)], input$type[which(Inputfreq2()$TIMESTAMP_TS == input$start:input$finish)] ))  

        })
  }
)

我的想法是将数据集选择作为第一个输入,因为它确定了数据列/类型和时间窗口的频率。我想通过ui获取数据列和时间窗口输入,但不知道如何让ui返回服务器中所选数据集的输出。如果我尝试使用ui

selectInput("fun", "func", choices = c(names(input$freqtype))),

我明白了:

ERROR: object 'input' not found

如果我尝试

selectInput("fun", "func", choices = c(Inputfreq2)

我明白了:

ERROR: Operation not allowed without an active reactive context. (You 
tried to do something that can only be done from inside a reactive 
expression or observer.

所以我选择renderUI(),但我似乎没有正确使用该功能的输出。由于代码在这里,当我运行它时,UI显示我的输入的选择框,但该图显然得到一个NAN并给出一条消息,指示该类似的东西,具体取决于我如何调整它。

This question似乎与我的相似,并提到将其中一个回复放回&#34;全球&#34;然后从那里使用它,但我不知道这意味着什么。

1 个答案:

答案 0 :(得分:0)

当UI元素依赖于用户选择时,最好在服务器端呈现它们。您将能够访问服务器端输入列表的值。 http://shiny.rstudio.com/articles/dynamic-ui.html

哦,我在底部看到你已经尝试过了。因为没有选择,你将获得NAN。您可以设置默认值,也可以在渲染绘图之前检查值是否为空,只有在它们不为空时才渲染。