搜索
查看: 2442|回复: 0

用shiny画图并且提供下载

[复制链接]

633

主题

1182

帖子

4030

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
4030
发表于 2017-6-9 16:24:47 | 显示全部楼层 |阅读模式
server代码是:
[AppleScript] 纯文本查看 复制代码
library(shiny)
shinyServer(function(input,output)({
  # x contains all the observations of the x variable selected by the user. X is a reactive function
  x <- reactive({
    iris[,as.numeric(input$var1)]
  })
  # x contains all the observations of the y variable selected by the user. Y is a reactive function
  y <- reactive({
    iris[,as.numeric(input$var2)]
    
  })
  # xl contains the x variable or column name of the iris dataset selected by the user
  xl <- reactive({
    names(iris[as.numeric(input$var1)])
    })
  # yl contains the y variable or column name of the iris dataset selected by the user
  yl <- reactive({
    names(iris[as.numeric(input$var2)])
  })
  
  # render the plot so could be used to display the plot in the mainPanel
  output$plot <- renderPlot({
    plot(x=x(), y=y(), main = "iris dataset plot", xlab = xl(), ylab = yl())
    
  })
  
  # downloadHandler contains 2 arguments as functions, namely filename, content
  output$down <- downloadHandler(
    filename =  function() {
      paste("iris", input$var3, sep=".")
    },
    # content is a function with argument file. content writes the plot to the device
    content = function(file) {
      if(input$var3 == "png")
        png(file) # open the png device
      else
        pdf(file) # open the pdf device
      plot(x=x(), y=y(), main = "iris dataset plot", xlab = xl(), ylab = yl()) # draw the plot
      dev.off()  # turn the device off
    
    } 
)
  
  
  
}))


UI界面是:
[AppleScript] 纯文本查看 复制代码
library(shiny)
shinyUI(fluidPage(
  titlePanel("Download base plot in Shiny - an example"),
  sidebarLayout(
   sidebarPanel(
      selectInput(inputId = "var1", label = "Select the X variable", choices = c("Sepal.Length" = 1, "Sepal.Width" = 2, "Petal.Length" = 3, "Petal.Width" = 4)),
      selectInput(inputId = "var2", label = "Select the Y variable", choices = c("Sepal.Length" = 1, "Sepal.Width" = 2, "Petal.Length" = 3, "Petal.Width" = 4), selected = 2),
      radioButtons(inputId = "var3", label = "Select the file type", choices = list("png", "pdf"))
),
    mainPanel(
      plotOutput("plot"),
      downloadButton(outputId = "down", label = "Download the plot")
      )
    )
  
  ))


快去试一试吧

如果你不想画两次图,比如上面的例子里面是downloadHandler()和downloadHandler()两次画图。
可以自己创建画图函数,比如下面

[AppleScript] 纯文本查看 复制代码
## a plot function
myPlot <- function(){
    plot(x=x(), y=y(), main = "iris dataset plot", xlab = xl(), ylab = yl())
}

## call the plot function to show on the page
output$plot <- renderPlot({
    myPlot()
})

## call the plot function when downloading the image
output$down <- downloadHandler(
    filename =  function() {
      paste("iris", input$var3, sep=".")
    },
    # content is a function with argument file. content writes the plot to the device
    content = function(file) {
      if(input$var3 == "png")
        png(file) # open the png device
      else
        pdf(file) # open the pdf device
      myPlot() 
      dev.off()  # turn the device off

    } 
)



你这个问题很复杂,需要打赏,请点击 http://www.bio-info-trainee.com/donate 进行打赏,谢谢
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|生信技能树 ( 粤ICP备15016384号  

GMT+8, 2019-12-6 12:23 , Processed in 0.025882 second(s), 23 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.