R包shiny开发网页--7.文字、表格与图像的输出

小洁写于2018.9.23 也不知道哪里跑偏了,我怎么觉得一直在学UI,那server呢,咋整。只有脸蛋米有内涵,那不行不行。
续写于2018.10.5 时间过得太快,中间去学了一下autodock,断断续续就过了这么多天,在家也要好好学习!(虽然懒猪基因就自然的特异性表达了)
今天系统学习一下输出的东西是怎么得到的。

我的学习路线:shiny自带了11个示例,直接输入即可得到示例网页和对应的代码,真是方便的很!然后把代码复制过来贴到脚本窗口,在脚本加上shinyApp(ui,server)即可。然后仔细研究这些代码和网页上的元素对应,找出其中的共性和个性。
这里是官方给出的11个示例。

install.packages("shiny")
library(shiny)
runExample("01_hello")      # a histogram
runExample("02_text")       # tables and data frames
runExample("03_reactivity") # a reactive expression
runExample("04_mpg")        # global variables
runExample("05_sliders")    # slider bars
runExample("06_tabsets")    # tabbed panels
runExample("07_widgets")    # help text and submit buttons
runExample("08_html")       # Shiny app built from HTML
runExample("09_upload")     # file upload wizard
runExample("10_download")   # file download wizard
runExample("11_timer")      # an automated timer

观察了一下这些示例,都是应用了侧边栏布局。而且大多是侧边栏用作输入,主题部分用于输出。

1.文本输出:输入什么就输出什么

R包shiny开发网页--7.文字、表格与图像的输出_第1张图片

(不过我发现木有换行的操作啊。可能是对html不够熟悉吧以后再看)

library(shiny)

library(shinydashboard)

header <- dashboardHeader(title="Reactivity")

sidebar <- dashboardSidebar(
  textInput(inputId = "caption",label = "Caption:")
)
body <- dashboardBody(h3(textOutput("caption")))

ui <- dashboardPage(header, sidebar, body)

server <- function(input, output) {
  output$caption <- renderText({
    input$caption
  })
}
shinyApp(ui, server)

这段代码是从例3中抽出来的。然后我出于对shinydashboard的喜爱和强迫症,对源代码进行了一些不疼不痒的改写,直接runExample("03_reactivity") 可以查看臃肿的源代码奥。

理了一下这里的逻辑:

  • 输入变量和输出变量都叫caption。
  • 输出,在ui中体现为textOutput("caption"),在server中则体现为output$caption。
  • 在ui中规定要显示出某个输出变量,而server则规定要怎么生成这个输出变量。
  • renderText是渲染文本的意思,渲染的意思大概就是改变一下格式,美化一下显示效果,markdown就是渲染用的。

2.数据框输出

可以选择一个数据框显示出来,选项有3个。另外可以通过滑动选择显示的行数。


R包shiny开发网页--7.文字、表格与图像的输出_第2张图片
滑块被挡住了
library(shiny)
library(shinydashboard)
header <- dashboardHeader(title="doodle look at me")
sidebar <- dashboardSidebar(
  selectInput(inputId = "dataset",
              label = "Choose a dataset:",
              choices = c("rock", "pressure", "cars")),
  sliderInput("obs","rows:",1,50,20)
)
body <- dashboardBody(tableOutput("view"))
ui <- dashboardPage(header, sidebar, body,skin = "green")
server <- function(input, output) {
  datasetInput <- reactive({
    switch(input$dataset,
           "rock" = rock,
           "pressure" = pressure,
           "cars" = cars)
  })
  output$view <- renderTable({
    head(datasetInput(), n = input$obs)
  })
}
shinyApp(ui = ui, server = server)

理思路:

  • 输入变量:dataset和obs,分别代表数据框名和行数。
  • 输出变量:view,表示输出的表格。
  • 用到了reactive和swich关联了标签与数据框
  • renderTable渲染表格
  • head选择显示数据框前多少行,行数等于输入变量obs。

3.图像输出

(1)提取数据框中的变量作图(plot)

以mpg数据框中的displ为横坐标,选择cty与hwy中的一列为纵坐标作图。

library(shiny)
library(shinydashboard)
library(ggplot2)
header <- dashboardHeader(title = "doodle,look at me")
sidebar <- dashboardSidebar(
  selectInput("column","col:",c("cty","hwy"))
)
body <- dashboardBody(
  textOutput("caption"),
  plotOutput("x"))
ui <- dashboardPage(header, sidebar, body)

server <- function(input, output){
  columnInput <- reactive({
    switch(input$column,
           "cty" = mpg$cty,
           "hwy" = mpg$hwy)
  })
  output$caption=renderText(paste("displ-",input$column,sep = ""))
  output$x <- renderPlot({
    plot(mpg$displ,columnInput())
  })
  
}

shinyApp(ui, server)

在上面的代码中,关键在于switch实现了标签和列名之间的对应关系,把这个经后台转换过的输入变量又转为一个新的变量作为作图的纵轴变量。

(2)在shiny种实现ggplot作图

作图用的是系统自带的plot,我想实现用ggplot2作图,进行了一系列的探索,发现如果y轴变量是可变的话(也就是照搬上面代码的模式),图片是出不来的!苦苦探索,百改皆错,终于在一个博客中发现了“aes_string”这个神奇的操作!在此启发下写出了以下代码:

①选择指定的列做散点图
library(shiny)
library(shinydashboard)
library(ggplot2)
header <- dashboardHeader(title = textOutput("caption"))
sidebar <- dashboardSidebar(
  selectInput("X","col:",colnames(mpg)),
  selectInput("Y","col:",colnames(mpg))
)
body <- dashboardBody(
  plotOutput("plot"))
ui <- dashboardPage(header, sidebar, body)

server <- function(input, output){
  
  output$caption=renderText(paste(input$X,"-",input$Y,sep = ""))
 
  output$plot <- renderPlot({
    ggplot(mpg,aes_string(x=input$X,y=input$Y))+geom_point()
  })
}

shinyApp(ui, server)
R包shiny开发网页--7.文字、表格与图像的输出_第3张图片

②选择指定的列并指定颜色和是否添加趋势线

library(shiny)
library(ggplot2)
dataset <- mpg
library(shinydashboard)
header <- dashboardHeader(title = "mpg")
sidebar <- dashboardSidebar(
  selectInput('x', 'X', colnames(dataset)),
  selectInput('y', 'Y', colnames(dataset)),
  selectInput('color', 'Color', c('None', names(dataset))),
  checkboxInput('smooth', 'Smooth')
)
body <- dashboardBody(plotOutput('plot'))
ui <- dashboardPage(header, sidebar, body)

server <- function(input, output) {
  output$plot <- reactivePlot(function() {
    p <- ggplot(dataset, aes_string(x=input$x, y=input$y)) + geom_point() 
    if (input$color != 'None')
      p <- p + 
        aes_string(color=input$color) + 
        theme(legend.position="top")
    if (input$smooth)
      p <- p + 
        geom_smooth() + 
        theme(legend.position="top")
    print(p)
  }, height=400)
}
shinyApp(ui,server)
R包shiny开发网页--7.文字、表格与图像的输出_第4张图片

R包shiny开发网页--7.文字、表格与图像的输出_第5张图片
微信公众号生信星球同步更新我的文章

友情链接:
生信技能树公益视频合辑:学习顺序是linux,r,软件安装,geo,小技巧,ngs组学!
B站链接:https://m.bilibili.com/space/338686099
YouTube链接:https://m.youtube.com/channel/UC67sImqK7V8tSWHMG8azIVA/playlists
生信工程师入门最佳指南:https://mp.weixin.qq.com/s/vaX4ttaLIa19MefD86WfUA
学徒培养:https://mp.weixin.qq.com/s/3jw3_PgZXYd7FomxEMxFmw
资料大全:https://mp.weixin.qq.com/s/QcES9u1vYh-l6LMXPgJIlA

你可能感兴趣的:(R包shiny开发网页--7.文字、表格与图像的输出)