有光泽的输出根据条件而变化

时间:2018-06-24 14:28:51

标签: r shiny plotly

我正在尝试制作一个闪亮的应用程序,以便对我拥有的某些数据进行一些用户友好的数据分析,并且我想根据我正在查看的文件来更改输出的Plotly图。基本上,我希望一次输出一个图,在此可以循环浏览多个图(在我闪亮的应用程序中不会更改位置),具体取决于我使用的文件夹和条件。目前,我正在为此而苦苦挣扎,我不知道该怎么做。我附了几张图片,以阐明我的意思和想要的东西。

This photo shows my UI and how I want my figures to be displayed. I'd like all figures to show in that same location, depending on the selected file. This photo shows my UI and how I want my figures to be displayed. I'd like all figures to show in that same location, depending on the selected file.

When I switch to 'Datalogger', a new plot is generated, and it is outputted below the first one. I'd like it to be placed on top of it, in the exact same location. When I switch to 'Datalogger', a new plot is generated, and it is outputted below the first one. I'd like it to be placed on top of it, in the exact same location.

非常欢迎您提供任何帮助。

最好, T。

脚本:

# Load packages
library(shiny)
library(shinythemes)
library(dplyr)
library(readr)
library(lubridate)
library(plotly)

#picarro
time = as.character(seq(as.POSIXct("2018-06-01 12:00:00"), as.POSIXct("2018-06-01 12:10:00"), by=seconds() )); ch4.corr = runif(length(time), 1980, 2000);
data = data.frame(time, ch4.corr); data$time = as.POSIXct(time); 
#datalogger
time = as.character(seq(as.POSIXct("2018-06-01 12:00:00"), as.POSIXct("2018-06-01 12:10:00"), by=seconds() )); PressureOut = runif(length(time), 1010, 1020);
dlog = data.frame(time, PressureOut); dlog$time = as.POSIXct(time);
#dronelog
time = as.character(seq(as.POSIXct("2018-06-01 12:00:00"), as.POSIXct("2018-06-01 12:10:00"), by=seconds() ));
ulog = data.frame(time); ulog$time = as.POSIXct(time);

#------------------------------------------------------------------------------
ui <- fluidPage(
   titlePanel("Active AirCore analysis"),
   hr(),
   fluidRow(
      column(3,
             radioButtons("fileInput", "File",
                          choices = c("Picarro", "Datalogger", "Dronelog"),
                          selected = "Picarro"),
             hr(),
             conditionalPanel(
                condition = "input.fileInput == 'Picarro'",
                sliderInput("timeInputPicarro", "Time", as.POSIXct(data$time[1]), as.POSIXct(data$time[length(data$time)]), c(as.POSIXct(data$time[1])+minutes(1), as.POSIXct(data$time[length(data$time)])-minutes(1)), timeFormat = "%H:%M:%S", ticks = T, step = seconds(1), pre = "")),
             conditionalPanel(
                condition = "input.fileInput == 'Datalogger'",
                sliderInput("timeInputDatalogger", "Time", as.POSIXct(dlog$time[1]), as.POSIXct(dlog$time[length(dlog$time)]), c(as.POSIXct(dlog$time[1]), as.POSIXct(dlog$time[length(dlog$time)])), timeFormat = "%H:%M:%S", ticks = T, step = seconds(1), pre = "")),
             conditionalPanel(
                condition = "input.fileInput == 'Dronelog'",
                sliderInput("timeInputDronelog", "Time", as.POSIXct(ulog$time[1]), as.POSIXct(ulog$time[length(ulog$time)]), c(as.POSIXct(ulog$time[1])+minutes(1), as.POSIXct(ulog$time[length(ulog$time)])-minutes(1)), timeFormat = "%H:%M:%S", ticks = T, step = seconds(1), pre = "")),
             hr(),
             conditionalPanel(
                condition = "input.fileInput == 'Picarro'",
                radioButtons("picarroPlotInput", "Plot type",
                             choices = c("Time-series", "Process"),
                             selected = "Time-series")),
             conditionalPanel(
                condition = "input.fileInput == 'Datalogger'",
                radioButtons("dataloggerPlotInput", "Plot type",
                             choices = c("Time-series", "Altitude"),
                             selected = "Time-series")),
             hr(),
             checkboxGroupInput(inputId='sidebarOptions', 
                                label=('Options'),
                                choices=c('Blabla', 'Store data', 'BlablaBla')),
             hr()),
      br(),

      mainPanel(
         plotlyOutput("dataplot"),
         hr(),
         plotlyOutput("dlogplot")
      )
   )
)

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

   datasetInputPic <- reactive({ data = data;  })
   datasetInputPicSamp <- reactive({ dat = data[(data$time>=input$timeInputPicarro[1]) & (data$time<=input$timeInputPicarro[2]),]; })
   datasetInputDatalogger <- reactive({ dlog = dlog })
   datasetInputDronelog <- reactive({ ulog = ulog })

   output$dataplot <- renderPlotly({
      if( (input$fileInput == 'Picarro' ) & (input$picarroPlotInput == 'Time-series')){
         data = datasetInputPic();
         data$time = as.POSIXct(data$time);
         dat = datasetInputPicSamp();
         dat$time = as.POSIXct(dat$time);

         sec.col = "red";
         f = list(size = 8);

         x <- list(title = " ")
         y <- list(title = "CH<sub>4</sub> [ppb]")
         p2 = plot_ly() %>%
            add_trace(data = data,
                      x = ~time,
                      y = ~ch4.corr,
                      type = 'scatter',
                      mode = "markers",
                      marker = list(size = 3, color = 'black')) %>%
            add_trace(data = dat,
                      x = ~time,
                      y = ~ch4.corr,
                      type = 'scatter',
                      mode = "markers",
                      marker = list(size = 3, color = sec.col)) %>%
            layout(xaxis = x, yaxis = y, title = '', showlegend = F, titlefont = f);

         s1 = subplot(p2, margin = 0.06,nrows=1,titleY = TRUE) %>%
            layout(showlegend = F, margin = list(l=50, r=0, b=50, t=10), titlefont = f);
         s1
      }
   })
   output$dlogplot <- renderPlotly({
      if( (input$fileInput == 'Datalogger' ) & (input$dataloggerPlotInput == 'Time-series')){
         data = datasetInputDatalogger();
         data$time = as.POSIXct(data$time);

         x <- list(title = " ")
         y <- list(title = "Outside pressure [mbar]")
         p1 = plot_ly() %>%
            add_trace(data = data, 
                      y = ~PressureOut, 
                      x = ~time, 
                      type = 'scatter',
                      mode = "markers",  
                      marker = list(size = 3, color = 'black'));

         s1 = subplot(p1, margin = 0.07, nrows=2, titleY = TRUE, titleX = FALSE)
         layout(s1, showlegend = F, margin = list(l=100, r=100, b=0, t=100), title = "Datalogger data")
         s1
      }
   })

   outputOptions(output, c("dataplot", "dlogplot"), suspendWhenHidden = TRUE)
}

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

1 个答案:

答案 0 :(得分:1)

您的问题是,您在用户界面中编写了:

mainPanel(
     plotlyOutput("dataplot"),
     hr(),
     plotlyOutput("dlogplot")
  )

使用此结构,“ dlogplot”将始终显示在“ dataplot”下方,因为您实际上已在“ dataplot”下方的主面板中为其指定了自己的位置。一种解决方案是,如果希望在单击各种按钮时将图显示在相同的确切位置,则仅给出一个plotlyOutput。接下来,您将有条件的ifelse ifelse放在renderPlotly中。例如:

   output$dataplot <- renderPlotly({
  if( (input$fileInput == 'Picarro' ) & (input$picarroPlotInput == 'Time-series')){
     data = datasetInputPic();
     data$time = as.POSIXct(data$time);
     dat = datasetInputPicSamp();
     dat$time = as.POSIXct(dat$time);

     sec.col = "red";
     f = list(size = 8);

     x <- list(title = " ")
     y <- list(title = "CH<sub>4</sub> [ppb]")
     p2 = plot_ly() %>%
        add_trace(data = data,
                  x = ~time,
                  y = ~ch4.corr,
                  type = 'scatter',
                  mode = "markers",
                  marker = list(size = 3, color = 'black')) %>%
        add_trace(data = dat,
                  x = ~time,
                  y = ~ch4.corr,
                  type = 'scatter',
                  mode = "markers",
                  marker = list(size = 3, color = sec.col)) %>%
        layout(xaxis = x, yaxis = y, title = '', showlegend = F, titlefont = f);

     s1 = subplot(p2, margin = 0.06,nrows=1,titleY = TRUE) %>%
        layout(showlegend = F, margin = list(l=50, r=0, b=50, t=10), titlefont = f);
     s1
  }
 else if( (input$fileInput == 'Datalogger' ) & (input$dataloggerPlotInput == 'Time-series')){
     data = datasetInputDatalogger();
     data$time = as.POSIXct(data$time);

     x <- list(title = " ")
     y <- list(title = "Outside pressure [mbar]")
     p1 = plot_ly() %>%
        add_trace(data = data, 
                  y = ~PressureOut, 
                  x = ~time, 
                  type = 'scatter',
                  mode = "markers",  
                  marker = list(size = 3, color = 'black'));

     s1 = subplot(p1, margin = 0.07, nrows=2, titleY = TRUE, titleX = FALSE)
     layout(s1, showlegend = F, margin = list(l=100, r=100, b=0, t=100), title = "Datalogger data")
     s1
  }
})

此代码会将“ dlogplot”和“ dataplot”放在主面板的相同位置。 (您还需要摆脱output$dlogplot <- renderPlotly({...}),以便它也不想尝试绘制该图。)

尝试一下,看看它是否适合您的目的。