在Shiny Application中显示带有绘图的模态对话框

时间:2018-04-16 19:12:01

标签: r shiny

我有以下Shiny Application:

dates <- seq(as.Date("2018-01-01"), as.Date("2018-05-01"), by="days")
point_duration = rnorm(n=length(dates), mean=6, sd=1)
point_duration_bench = rnorm(n=length(dates), mean=5, sd=2)
df <- data.frame(dates, point_duration, point_duration_bench)
df$week <- strftime(df$dates, format = "%V")
df$month <- strftime(df$dates, format = "%m")

current_day = Sys.Date()
current_week = strftime(current_day, format = "%V")
current_month = strftime(current_day, format = "%m")


library(shiny)
library(ggplot2)
library(plotly)
library(shinyBS)

UI <- fluidPage(
  actionButton("month","Show last week"),
  plotOutput("line_graph"),
  bsModal("modalExample", "Your plot", "go", size = "large",plotOutput("plot"),downloadButton('downloadPlot', 'Download'))

)
Server <- function(input, output) {

  observeEvent(input$month, {

    output$plot <- renderPlot({
      hist(50)
    })


  })

  output$line_graph <- renderPlot({
    ggplot(df, aes(x=dates, y=point_duration)) +
      geom_bar(stat = "identity") +
      geom_line(aes(x=dates, y = point_duration_bench), colour = "blue") + 
      geom_point() +
      labs(y="Amount of calls (#1000)",x="")
  })


}


shinyApp(ui = UI, server = Server)

使用bsModal函数我尝试实现这一点,当你按下月份按钮时会出现一个显示绘图输出的弹出屏幕(在这种情况下是一个简单的hist(50))。

然而,它似乎不起作用...对我出错的地方有任何想法?

1 个答案:

答案 0 :(得分:0)

您需要将模态输出连接到右侧按钮。当需要'月'工作时,你把它附加到'去'。此外,我认为您不需要观察者,因为行为内置于bsModal()。见工作代码:

dates <- seq(as.Date("2018-01-01"), as.Date("2018-05-01"), by="days")
point_duration = rnorm(n=length(dates), mean=6, sd=1)
point_duration_bench = rnorm(n=length(dates), mean=5, sd=2)
df <- data.frame(dates, point_duration, point_duration_bench)
df$week <- strftime(df$dates, format = "%V")
df$month <- strftime(df$dates, format = "%m")

current_day = Sys.Date()
current_week = strftime(current_day, format = "%V")
current_month = strftime(current_day, format = "%m")


library(shiny)
library(ggplot2)
library(plotly)
library(shinyBS)

UI <- fluidPage(
  actionButton("month","Show last week"),
  plotOutput("line_graph"),
  bsModal("modalExample", 
          "Your plot", 
          "month", # <----set the observer to the right button
          size = "large",
          plotOutput("plot"),
          downloadButton('downloadPlot', 'Download'))

)
Server <- function(input, output) {

      output$plot <- renderPlot({
        hist(50)
      })

  output$line_graph <- renderPlot({
    ggplot(df, aes(x=dates, y=point_duration)) +
      geom_bar(stat = "identity") +
      geom_line(aes(x=dates, y = point_duration_bench), colour = "blue") + 
      geom_point() +
      labs(y="Amount of calls (#1000)",x="")
  })


}


shinyApp(ui = UI, server = Server)