【问题标题】:Shiny *for* loop to automatically download plots闪亮的 *for* 循环自动下载绘图
【发布时间】:2020-11-16 07:15:41
【问题描述】:

我对 Shiny 很陌生,我正在尝试制作一个 Shiny 应用程序,它允许用户通过一个简单的下载按钮下载特定数据的所有可能组合。

但是,闪亮似乎不允许我在闪亮之外使用的标准for loop

所以基本上我想用这些可重现的数据做的是允许用户下载所有 9 个可能的散点图(1,1),(1,2),(1,3)

我尝试过使用lapply, isolate, local 等等,但很简单,要么制作 3 个相同的图,要么不像当前代码那样工作

我们将非常非常感谢您的帮助。

提前谢谢你

这是我当前的用户界面

#UI
library(shiny)
library(datasets)
library(ggplot2)
Test1<- c(3,5,7)
Test2<- c(21000, 23400, 26800)
Test3<- c(600, 700, 800)
df <- data.frame(Test1, Test2, Test3)
rownames(df)<-colnames(df)
*ui <- shinyUI(fluidPage(
  titlePanel("Column Plot"),
  tabsetPanel(
    tabPanel("First Type",
             pageWithSidebar(
               headerPanel('My First Plot'),
               sidebarPanel(
                 selectInput('xcol', 'X Variable', ""),
                 selectInput('ycol', 'Y Variable', "", selected = ""),
                 actionButton(inputId = "clicks_3",label = "Real"),
               ),
               mainPanel(
                 plotOutput('MyPlot')
               )
             )
    )
    
  )
)
)

这是我的服务器

server <- shinyServer(function(input, output, session) {
  data <- reactive({ 

    updateSelectInput(session, inputId = 'xcol', label = 'X Variable',
                      choices = names(df), selected = names(df))
    updateSelectInput(session, inputId = 'ycol', label = 'Y Variable',
                      choices = names(df), selected = names(df)[2])
    
    return(df)
  })
  
  output$contents <- renderTable({
    data()
  })
  output$MyPlot <- renderPlot({
    x <- data()[, c(input$xcol, input$ycol)]
    plot(x)
    
  })
  observeEvent(input$clicks_3,{
    for (i in 1:3){
      assign(paste("Plotinput_",i,sep = ""),({
        x <- data()[, c(colnames(data())[i], input$ycol)]
        plot(x)
      }))
      lapply(1:3, function(k){local({
        ggsave(plot = eval(parse(text=paste("Plotinput_",k,sep = ""))),filename = paste(k,"barplot.png",collapse = ""))
      })
      })
    }
  })
  
  
})

【问题讨论】:

    标签: r loops for-loop ggplot2 shiny


    【解决方案1】:

    如果你想下载所有图,我会推荐一个降价报告解决方案。在那里,您可以使用 lapply 并遍历所有绘图。见https://shiny.rstudio.com/articles/generating-reports.html

    在您的代码中,括号似乎不正确,请参阅下面的部分以获取解决方案

      observeEvent(input$clicks_3,{
        for (i in 1:3){
          assign(paste("Plotinput_",i,sep = ""),({
            x <- data()[, c(colnames(data())[i], input$ycol)]
            plot(x)
          }))
        }
        lapply(1:3, function(k){local({
          ggsave(plot = eval(parse(text=paste("Plotinput_",k,sep = ""))),filename = paste(k,"barplot.png",collapse = ""))
        })
        })
      })
    

    【讨论】:

    • 非常感谢您的建议,效果很好:),第一次使用markdown,但看起来很方便
    • 不客气。如果能解决您的问题,请接受我的回答
    猜你喜欢
    • 1970-01-01
    • 2017-11-16
    • 2018-04-26
    • 1970-01-01
    • 1970-01-01
    • 2017-11-14
    • 1970-01-01
    • 2022-01-26
    • 2020-08-30
    相关资源
    最近更新 更多