R-shiny:图未渲染为shiny,但在r-script中工作正常

4sup72z8  于 2023-01-28  发布在  其他
关注(0)|答案(1)|浏览(118)

我正在创建一个基于用户输入的时间序列图。应用程序运行没有任何问题,但没有生成图。然而,相同的代码生成了一个图在常规的r脚本。我看不出为什么闪亮的图不会呈现的原因。
试验数据:

mydata<-structure(list(employee = c("Mary", "rob", 
"smary", "rob", "Abe", "Abe"
), joining_date = structure(c(17869, 17862, 17865, 17848, 
17862, 17848), class = "Date"), batch___A_2019 = c(1, 1, 
1, 1, 1, 0), batches___A_2020 = c(0, 0, 0, 0, 0, 1), batch___B_2020 = c(0, 
0, 0, 0, 0, 0), batch___B_2023 = c(0, 0, 0, 0, 0, 0)), row.names = c(NA, 
6L), class = "data.frame")

R脚本:运行良好,并根据需要生成图

test <- mydata %>%
  mutate(week = week(joining_date)) %>%
  mutate(new_date = format(joining_date, "%m %Y"), year = format(joining_date, "%Y")) %>%
  filter(
    employee == "Mary" | employee == "Abe",
    joining_date >= "2019-01-01" &
      joining_date <= "2023-01-01"
  ) %>%
  group_by(employee,month = lubridate::floor_date(joining_date, 'month')) %>%
  summarize(Total = n(), .groups = "drop") %>%
  complete(month = seq.Date(min(month), max(month), by="month"), employee) %>%
  replace(is.na(.), 0)
  
  
g<-ggplot(test, aes(x = month, y = Total)) +
  geom_line(aes(color = employee)) +
  scale_x_date(date_labels = "%b %Y", date_breaks = "1 month") +
  theme(axis.text.x=element_text(angle=50, hjust=1)) +
  ylab("Total joinee")

R-闪亮(不生成图)

timeseries <- reactive({
    req(input$employee)
    req(input$test)
    req(input$Dates)
    mydata %>%
      filter(
        employee == input$employee,
        if_any(matches(str_c(
          'batch___', tolower(input$test)
        )), ~
          .x == 1),
        joining_date >= input$Dates[1] &
          joining_date <= input$Dates[2]
      ) %>%
      group_by(month = lubridate::floor_date(joining_date, 'month')) %>%
      summarize(Total = n(), .groups = "drop") %>%
      complete(month = seq.Date(min(month), max(month), by="month"), employee) %>%
      replace(is.na(.), 0)
  })
  
  output$timeseriesplot <- renderPlot({
    ggplot(timeseries()) + 
      geom_line(aes(x = month, y = Total, color=input$test)) +
      scale_x_date(date_labels = "%b %Y", date_breaks = "1 month") +
      theme(axis.text.x=element_text(angle=50, hjust=1)) +
      ylab("Total joinee") 
  })

用户界面

tabItem(tabName = "Timeseries",
              
              box(
                width = 10,
                
                h2("Time series plot"),
                
                sidebarPanel(
                  selectInput(
                    inputId = "employee",
                    choices = mydata$employee,
                    label = "Search by employee"
                  ),
                  
                  dateRangeInput(
                    "Dates",
                    h5("Select the Dates"),
                    format = "yyyy-mm-dd",
                    start = Sys.Date() - 365
                  ),
                  
                  checkboxGroupInput(
                    inputId = "test",
                    choices = c("A", "B", "C"),
                    selected = c("A", "B", "C"),
                    label = "Select test"
                  )
                ),
                
                mainPanel(plotOutput("timeseriesplot")),
              ),
      ),
pw9qyyiw

pw9qyyiw1#

我不能确定我的建议是否对你有用,因为我不能在没有数据的情况下运行你的应用程序。这里有一种方法,我已经能够让图形工作在闪亮。首先,我建议使用verbatimTextOutput作为一种方法来查看你的时间序列React的输出,以确保这不是问题所在。
然后考虑将图设为React图,然后渲染React图。

ts <- reactive({ 

    timeseries <- timeseries()
    
    timeseries %>%
      ggplot() +
      geom_line(aes(x = month, y = Total, color=input$test)) +
      scale_x_date(date_labels = "%b %Y", date_breaks = "1 month") +
      theme(axis.text.x=element_text(angle=50, hjust=1)) +
      ylab("Total joinee") 
})

output$timeseriesplot <- renderPlot({
   ts()
})

相关问题