已发布的Shiny应用程序在ggplot geom_line中缺少本地R输出中存在的值

w1e3prcc  于 2023-04-27  发布在  其他
关注(0)|答案(1)|浏览(110)

我有一个完整的Shiny应用程序,它可以从在线数据库中提取数据,并输出可以通过选择数据位置和日期进行编辑的图表和表格。当我在R Studio中运行该应用程序时,所有元素都可以完美渲染。然而,当我将同一个应用程序发布到shinyapps.io时,其中一个图表无法正确渲染。
以下是www.example.com上的输出shinyapps.io:

下面是R Studio窗口中的本地输出:

如您所见,由于某些原因,TP & Stage选项卡中的阶段高程数据的虚线几何缺少数据块。数据存在于本地输出、shinyapps.io绘图选项卡下方的www.example.com阶段表以及Flow & Stage选项卡中。绘制了同一阶段的数据(只有流量而不是磷浓度)。这种情况发生在每年相同的时间块,即使你扩大记录的时期更早。
有没有一个常见的问题,会导致这一点?我甚至不知道什么代码更改,因为它作为一个本地输出完美的工作。我怀疑这可能是某种图形故障,但我已经重新发布的应用程序无济于事。

odopli94

odopli941#

我能够自己解决这个问题。
之前,我根本没有使用React式表达式(我上周刚开始使用Shiny)。在从每个renderPlot({})函数中取出数据清理和过滤表达式,并将它们分别放入Flow、Stage和Phosphorus数据的React式表达式中后,这些图都能正确渲染。
之前:

server <- function(input, output, session){
  # Flow & Stage Plot
  output$flowstage = renderPlot({
    flow = flow.xtab %>%
      filter(Date >= input$POR[[1]] & Date <= input$POR[[2]], SITE %in% as.list(input$site1))
    stage = stage %>%
      filter(Date >= input$POR[[1]] & Date <= input$POR[[2]])
    dat = flow %>%
      left_join(stage, by = "Date")
    sec.axis.scaler = ifelse(input$stack == "Identity",
                             max(dat$fflow.cfs,na.rm=T)/max(dat$S333HW, na.rm=T)*0.8,
                             max(aggregate(dat$fflow.cfs, by = list(dat$Date), sum)$x, na.rm = T)/max(dat$S333HW, na.rm = T)*0.8)
    ggplot(dat) +
      scale_y_continuous(name = "Flow (cfs)",
                         sec.axis = sec_axis(trans = ~./sec.axis.scaler, name = "Stage (ft)")) +
      geom_area(aes(as.Date(Date), fflow.cfs, fill = SITE), alpha = 0.6, position = input$stack) +
      geom_line(aes(as.Date(Date), S333HW*sec.axis.scaler, color = "Stage"), color = "black", linetype = "dashed") +
      labs(title = 'Flow & S333HW Stage Data',
           y = 'Flow (cfs)',
           x = 'Date') +
      xlim(as.Date(input$POR[[1]]), as.Date(input$POR[[2]])) +
      theme_bw()
  })
  # TP & Stage Plot
  output$tpstage = renderPlot({
    tp = tp.xtab3 %>%
      filter(Date >= input$POR[[1]] & Date <= input$POR[[2]], SITE %in% as.list(input$site2)) %>%
      mutate(tp.ugL = tp.ugL*1000)
    stage = stage %>%
      filter(Date >= input$POR[[1]] & Date <= input$POR[[2]])
    dat = tp %>%
      left_join(stage, by = "Date")
    sec.axis.scaler = max(dat$tp.ugL,na.rm=T)/max(dat$S333HW, na.rm=T)*0.8
    ggplot(dat) +
      scale_y_continuous(name = "TP (ug/L)",
                         sec.axis = sec_axis(trans = ~./sec.axis.scaler, name = "Stage (ft)")) +
      geom_point(aes(as.Date(Date), tp.ugL, color = SITE), alpha = 0.6) +
      geom_line(aes(as.Date(Date), S333HW*sec.axis.scaler, color = "Stage"), color = "black", linetype = "dashed") +
      labs(title = 'TP & S333HW Stage Data',
           y = 'TP (ug/L)',
           x = 'Date') +
      xlim(as.Date(input$POR[[1]]), as.Date(input$POR[[2]])) +
      theme_bw()
  })
  # Flow & TP Plot
  output$flowtp = renderPlot({
    flow = flow.xtab %>%
      filter(Date >= input$POR[[1]] & Date <= input$POR[[2]], SITE %in% as.list(input$site1))
    tp = tp.xtab3 %>%
      filter(Date >= input$POR[[1]] & Date <= input$POR[[2]], SITE %in% as.list(input$site2)) %>%
      mutate(tp.ugL = tp.ugL*1000)
    dat = flow %>%
      full_join(tp, by = c("Date","SITE"))
    sec.axis.scaler = ifelse(input$stack == "Identity",
                             max(dat$fflow.cfs,na.rm=T)/max(dat$tp.ugL, na.rm=T)*0.8,
                             max(aggregate(dat$fflow.cfs, by = list(dat$Date), sum)$x, na.rm = T)/max(dat$tp.ugL, na.rm = T)*0.8)
    ggplot(dat) +
      scale_y_continuous(name = "Flow (cfs)",
                         sec.axis = sec_axis(trans = ~./sec.axis.scaler, name = "TP (ug/L)")) +
      geom_area(aes(as.Date(Date), fflow.cfs, fill = SITE), alpha = 0.6, position = input$stack) +
      geom_point(aes(as.Date(Date), tp.ugL*sec.axis.scaler, color = SITE), alpha = 0.6) +
      labs(title = 'Flow & TP Data',
           y = 'Flow (cfs)',
           x = 'Date') +
      xlim(as.Date(input$POR[[1]]), as.Date(input$POR[[2]])) +
      theme_bw()
  })

之后:

server <- function(input, output, session){
  # Reactive functions for data subsets
  rflow = reactive({
    flow.xtab %>%
      filter(Date >= input$POR[[1]] & Date <= input$POR[[2]], SITE %in% as.list(input$site1)) %>%
      mutate(Date = as.Date(Date))
  })
  rstage = reactive({
    stage %>%
      filter(Date >= input$POR[[1]] & Date <= input$POR[[2]]) %>%
      mutate(Date = as.Date(Date))
  })
  rtp = reactive({
    tp.xtab3 %>%
      filter(Date >= input$POR[[1]] & Date <= input$POR[[2]], SITE %in% as.list(input$site2)) %>%
      mutate(tp.ugL = tp.ugL*1000, Date = as.Date(Date))
  })
  # Flow & Stage Plot
  output$flowstage = renderPlot({
    dat = rflow() %>%
      left_join(rstage(), by = "Date")
    sec.axis.scaler = ifelse(input$stack == "Identity",
                             max(dat$fflow.cfs,na.rm=T)/max(dat$S333HW, na.rm=T)*0.8,
                             max(aggregate(dat$fflow.cfs, by = list(dat$Date), sum)$x, na.rm = T)/max(dat$S333HW, na.rm = T)*0.8)
    ggplot(dat) +
      scale_y_continuous(name = "Flow (cfs)",
                         sec.axis = sec_axis(trans = ~./sec.axis.scaler, name = "Stage (ft)")) +
      geom_area(aes(as.Date(Date), fflow.cfs, fill = SITE), alpha = 0.6, position = input$stack) +
      geom_line(aes(as.Date(Date), S333HW*sec.axis.scaler, color = "Stage"), color = "black", linetype = "dashed") +
      labs(title = 'Flow & S333HW Stage Data',
           y = 'Flow (cfs)',
           x = 'Date') +
      xlim(as.Date(input$POR[[1]]), as.Date(input$POR[[2]])) +
      theme_bw()
  })
  # TP & Stage Plot
  output$tpstage = renderPlot({
    dat = rtp() %>%
      left_join(rstage(), by = "Date")
    sec.axis.scaler = max(dat$tp.ugL,na.rm=T)/max(dat$S333HW, na.rm=T)*0.8
    ggplot(dat) +
      scale_y_continuous(name = "TP (ug/L)",
                         sec.axis = sec_axis(trans = ~./sec.axis.scaler, name = "Stage (ft)")) +
      geom_point(aes(as.Date(Date), tp.ugL, color = SITE), alpha = 0.6) +
      geom_line(aes(as.Date(Date), S333HW*sec.axis.scaler, color = "Stage"), color = "black", linetype = "dashed") +
      labs(title = 'TP & S333HW Stage Data',
           y = 'TP (ug/L)',
           x = 'Date') +
      xlim(as.Date(input$POR[[1]]), as.Date(input$POR[[2]])) +
      theme_bw()
  })
  # Flow & TP Plot
  output$flowtp = renderPlot({
    dat = rflow() %>%
      full_join(rtp(), by = c("Date","SITE"))
    sec.axis.scaler = ifelse(input$stack == "Identity",
                             max(dat$fflow.cfs,na.rm=T)/max(dat$tp.ugL, na.rm=T)*0.8,
                             max(aggregate(dat$fflow.cfs, by = list(dat$Date), sum)$x, na.rm = T)/max(dat$tp.ugL, na.rm = T)*0.8)
    ggplot(dat) +
      scale_y_continuous(name = "Flow (cfs)",
                         sec.axis = sec_axis(trans = ~./sec.axis.scaler, name = "TP (ug/L)")) +
      geom_area(aes(as.Date(Date), fflow.cfs, fill = SITE), alpha = 0.6, position = input$stack) +
      geom_point(aes(as.Date(Date), tp.ugL*sec.axis.scaler, color = SITE), alpha = 0.6) +
      labs(title = 'Flow & TP Data',
           y = 'Flow (cfs)',
           x = 'Date') +
      xlim(as.Date(input$POR[[1]]), as.Date(input$POR[[2]])) +
      theme_bw()
  })

我想这是一个教训,总是精简您的代码,并消除重复的表达式在任何可能的地方!

相关问题