Rshiny基本函数(交互式web应用)

引言

R语言shiny包可以帮助我们轻松的制作交互式的web应用,并且可以搭建为独立的桌面可执行程序,非常方便制作和分享,其可以使用数据分析人员展开工作汇报、报表制作等等!

shiny包的下载

> install.packages('shiny')

shinyApp的构成

shinyApp的构成:
1.用户交互式脚本-ui
2.服务器运行脚本-server

> library(shiny)
> ui <- ...

> server <- ...

> shinyApp(ui, server)

ui的设计函数

HTML类比

shiny函数作用
p段落文本
h1一级标题
h2 二级标题
h3三级标题
h4四级标题
h5五级标题
h6六级标题
a超链接
br换行
div统一风格的文本划分
span统一的样式对文本进行行内分割
pre预定义格式文本
code计算机代码块
img图片
strong加粗
em斜体
HTML直接将字符串作为HTML代码传递
> ui <- fluidPage(
+ titlePanel("My Shiny App"),
+ sidebarLayout(
+ sidebarPanel(),
+ mainPanel(
+ h1("First level title"),
+ h2("Second level title"),
+ h3("Third level title"),
+ h4("Fourth level title"),
+ h5("Fifth level title"),
+ h6("Sixth level title")
+ )
+ )
+ )
> 
> server = function(input, output){ }
> 
> shinyApp(ui, server)

在这里插入图片描述

> ui <- fluidPage(
+ titlePanel("My Shiny App"),
+ sidebarLayout(
+ sidebarPanel(),
+ mainPanel(
+ p("p creates a paragraph of text."),
+ p("A new p() command starts a new paragraph. Supply a style attribute
+ to change the format of the entire paragraph.", style = "font-family:
+ 'times'; font-si16pt"),
+ strong("strong() makes bold text."),
+ em("em() creates italicized (i.e, emphasized) text."),
+ br(),
+ code("code displays your text similar to computer code"),
+ div("div creates segments of text with a similar style. This division
+ of text is all blue because I passed the argument 'style = color:blue' to
+ div", style = "color:blue"),
+ br(),
+ p("span does the same thing as div, but it works with",
+ span("groups of words", style = "color:blue"),
+ "that appear inside a paragraph.")
+ )
+ )
+ )
> 
> server = function(input, output){ }
> 
> shinyApp(ui, server)

在这里插入图片描述

img函数无法加载图片

img函数常用用法为:img(src = ‘.png’,height = ,width = ),经过本人测试,这种方式在RGui中无法使用,所以建议大家在Rstudio中使用.

以下为img函数在Rstudio中的使用方法.

图1 第一步
图2 第二步

在图片第二步选择了根目录E:\shinyApp下建立文件夹know, 需要在know文件夹下必须添加名为www的文件夹,将需要展示的图片放入www文件夹下
在图片第二步中我们选择的是建立app.R文件,将我们需要的ui、server、shinyApp函数写入app.R文件,之后点击保存runApp选项,会提示是否保存app.R文件,点击保存即可.

根据Rstudio分析RGui报错原因,在Rstudio中其运行是:

> runApp('~/know')

成功截图:
在这里插入图片描述

控件函数

函数作用
actionButton操作选项
checkboxInput单一选择
dateInput日期输入
dateRangeInput日期范围
fileInput文件选择
helpText为其他控件增加说明
numericInput数值输入
radioButtons单选按钮
checkboxGroupInput多选按钮
selectInput提供选项的框
sliderInput平滑的点击选择框
submitButton提交按钮
textInput输入文本的字段

每项控件函数的图片展示:
在这里插入图片描述

ui设计的输出对象

在ui中增加R对象.

ui中的输出函数输出类型
dataTableOutput数据表
htmlOutput原生html
imageOutput图片输出
plotOutputR中图片输出
tableOutput表输出:小型矩阵与数据款
textOutput文本输出
uiOutput原生html
verbatimTextOutput文本输出
> ui <- fluidPage(
+         titlePanel("Hello Shiny!"),
+       sidebarLayout(
+         sidebarPanel(
+          sliderInput(inputId = "bins",
+           label = "Number of bins:",
+           min = 1,
+           max = 50,
+           value = 30)
+                    ),
+         mainPanel(
+            plotOutput('plot'))
+                    )
+ )

以上为ui设计对象的输出例子:plotOutput.

server

服务器运行脚本提供R代码支撑ui中的输出对象.
render*函数将R代码产生的对象转为ui中的输出对象.

render*对象
renderDataTable数据表
renderImage图片
renderPlotR中的plot
renderPrint需要打印的输出
renderTable小型的表:数据框、矩阵
renderText文本
renderUIHTML

总结

关于Rshiny的基本简单使用讲到这里就结束了,文中所列举的都是Rshiny开发交互式web的基本函数,大家掌握方能写出自己想要的web.
另外关于shiny有几百个函数,我不可能在一篇博客中尽数到来,但是会在以后的博客中根据自己所需来展示一些函数的使用.
最后由于本人偏好RGui,因此在一直使用其来制作自己的shinyApp,但是某些情况下极为不方便,所以在这里推荐大家使用Rstudio
谢谢大家!!!
晚安!!!

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

卑微的小垃圾

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值