添加下载按钮到闪亮,打印服务器功能的输出

时间:2017-07-27 09:19:09

标签: r download shiny

我无法添加下载按钮来下载我的绘图,收到错误消息:"不允许从shinyoutput对象中删除对象。"

´  x <- output$Plot
  output$down <- downloadHandler(
    filename = function(){
      paste("AAA", input$var3, sep = ".")
    },
    content = function(file){
      if(input$var3 == "png")
      png(file)
      else
        pdf(file)
      plot(x)
      dev.off
    }
  )´

问题是我不能简单地添加图(x(),y()),因为在我的服务器中,我确实计算了大约7个不同的图,每个图都有不同的数据集,并且正在粘合&#34;和其他如果。

`ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput("Select2","Partei:", choices = c("AFD", "CDU", "DieLinke", "FDP", "Gruene", "Piraten", "SPD", "total")),
      selectInput("Select1", "Function:", choices = c("Absolute Worthaufigkeiten", "Relative Worthaufigkeiten",
                                                      "Wordclouds", "Term frequencies", "Sentiment Analysis 1", "Importance")),
      radioButtons(inputId = "var3", label = "Select file type", choices = list("png", "pdf"))
    ),
    mainPanel(
    plotOutput("Plot"),
    downloadButton(outputId = "down", label = "Download the Plot")
  )))`


`server <- function(input, output) {output$Plot <- renderPlot({
if(input$Select1 == "Absolute Worthaufigkeiten"){
  if(input$Select2 == "AFD"){
    AAA %>%
      filter(AFD > 2) %>%
      ggplot(aes(word, AFD, Jahr)) +
      geom_col(colour = "white", fill = "red2")
  }
  else{
    if(input$Select2 == "CDU"){
      AAA %>%
        filter(CDU > 80) %>%
        ggplot(aes(word, CDU)) +
        geom_col(colour = "red3", fill = "red3") 
        else {

            }
           else {
          if(input$Select1 == "Importance"){
            if(input$Select2 == "AFD"){
              print("NA")
            else{
              if(input$Select2 == "CDU"){

                book_words_CDU <- tidy_CDU_total %>%
                  count(book, word, sort = TRUE) %>%
                  ungroup()

                total_words_CDU<- book_words_CDU %>%
                  group_by(book) %>% 
                  summarize(total = sum(n))

                book_words_CDU <- left_join(book_words_CDU, total_words_CDU)

                book_words_CDU <- book_words_CDU %>%
                  bind_tf_idf(word, book, n) }})}

有没有办法让输出有光泽的打印?

1 个答案:

答案 0 :(得分:2)

由于你没有发布可重复的例子,我认为你没有任何绘图问题,你可以在你的应用程序中看到该情节,请试试这个:

  1. 将您的整个代码放在函数中,例如:

    testplot <- function() {
      if (input$Select1 == "Absolute Worthaufigkeiten") {
        if (input$Select2 == "AFD") {
          AAA %>%
            filter(AFD > 2) %>%
            ggplot(aes(word, AFD, Jahr)) +
            geom_col(colour = "white", fill = "red2")
        }
        else{
          if (input$Select2 == "CDU") {
            AAA %>% ...
          }
        }
      }
    }
    
  2. 渲染函数testplot()

    output$Plot <- renderPlot({testplot()})
    
  3. 使用downloadHandler()

    下载图表
    output$down <- downloadHandler(
      filename = function() {
        paste("AAA", input$var3, sep = ".")
      },
      content = function(file) {
        if (input$var3 == "pdf") {
          pdf(file, width = 30, height = 20)
        }
        else{
          png(file, width = 1200, height = 800, units = "px", pointsize = 12, bg = "white", res = NA)
        }
        print(testplot())
        dev.off()
      }
    )