如何将包含多个文件的 Shiny 应用程序转换为易于共享和重现的 Shiny 示例? [英] How to convert a Shiny app consisting of multiple files into an easily shareable and reproducible Shiny example?

查看:16
本文介绍了如何将包含多个文件的 Shiny 应用程序转换为易于共享和重现的 Shiny 示例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Stack Overflow 和 如何制作出色的 R 可重现示例.但是,对于 问题没有类似的指导方针,同时遵守某些标准可以更有可能提供高质量的答案,从而您的问题将得到解决.

There are resources on how to create a Minimal, Complete, and Verifiable example in general on Stack Overflow, and on how to make a great R reproducible example. However, there are no similar guidelines for shiny questions, while adhering to certain standards makes it much more likely that quality answers will be given, and thus that your question will be resolved.

然而,提出一个好的 Shiny 问题可能很困难. 应用程序通常庞大而复杂,使用多个数据源,并且代码通常拆分到多个文件中,因此很难与他人共享易于复制的代码.即使 server.R 中可能出现问题,如果没有 ui.R 的内容(可能还有其他文件,如样式表或 global.R).单独复制粘贴所有这些文件的内容很麻烦,并且需要其他用户重新创建相同的文件结构才能重现问题.

However, asking a good Shiny question can be difficult. shiny apps are often large and complex, use multiple data sources, and the code is often split over multiple files, making it difficult to share easily reproducible code with others. Even though a problem may be caused in server.R, the example is not reproducible without the contents of ui.R (and possibly other files like stylesheets or global.R). Copy-pasting the contents of all these files individually is cumbersome, and requires other users to recreate the same file structure to be able to reproduce the problem.

所以;如何将您的 应用程序转换为一个很好的可复制示例?

So; how to convert your shiny app into a good reproducible example?

推荐答案

示例数据

当然,关于问题的答案中提到的关于样本数据的所有准则如何制作出色的 R 可重现示例" 在创建与 Shiny 相关的问题时也适用.总结一下:确保不需要额外的文件来运行你的代码.使用像 mtcars 这样的示例数据集,或者使用 data.frame() 创建一些示例数据.如果您的数据非常复杂并且确实需要这种复杂性来说明问题,您还可以使用 dput().避免使用像 read.csv() 这样的函数,除非你对像 fileInput 这样的函数有疑问.

Of course, all guidelines regarding sample data mentioned in the answer on the question "how to make a great R reproducible example" also hold when creating questions related to Shiny. To summarize: Make sure no additional files are needed to run your code. Use sample datasets like mtcars, or create some sample data with data.frame(). If your data is very complex and that complexity is really required to illustrate the issue, you could also use dput(). Avoid using functions like read.csv(), unless of course you have questions related to functions like fileInput.

示例代码

始终将您的代码减少到最低限度,以重现您的错误或意外行为.这包括删除对其他 .CSS 文件和 .js 文件的调用,以及删除 uiserver 中不必要的功能>.

Always reduce your code to the bare minimum to reproduce your error or unexpected behavior. This includes removing calls to additional .CSS files and .js files and removing unnecessary functions in the ui and the server.

Shiny 应用程序通常包含两个或三个文件(ui.Rserver.R 和可能的 global.R),例如这个演示应用程序.但是,最好将您的代码作为单个脚本发布,这样其他人就可以轻松地运行它,而无需手动创建这些文件.这可以通过以下方式轻松完成:

Shiny apps often consist of two or three files (ui.R, server.R and possibly global.R), for example this demo application. However, it is preferable to post your code as a single script, so it can easily be run by others without having to manually create those files. This can easily be done by:

  • ui <-fluidPage(…) 包裹你的 ui,
  • 带有 server <- function(input,output, session) {…},
  • 的服务器
  • 然后调用shinyApp(ui, server).
  • wrapping your ui with ui <- fluidPage(…),
  • the server with server <- function(input,output, session) {…},
  • and subsequently calling shinyApp(ui, server).

所以一个简单的骨架开始可能如下所示:

So a simple skeleton to start with could look as follows:

library(shiny)

ui <- fluidPage(

  )

server <- function(input,output,session) {

}

shinyApp(ui, server)

工作示例

因此,考虑到以上所有因素,Shiny 应用程序的一个良好的最小、完整和可验证示例如下所示:

So, taking all the above into account, a good Minimal, Complete, and Verifiable example for a Shiny application could look as follows:

library(shiny)

df <- data.frame(id = letters[1:10], value = seq(1,10))

ui <- fluidPage(
  sliderInput('nrow', 'Number of rows', min = 1, max = 10, value = 5),
  dataTableOutput('my_table')
  )

server <- function(input, output, session) {
  output$my_table <- renderDataTable({
    df[1:input$nrow,]
  })
}

shinyApp(ui, server)

<小时>

添加 CSS

有多种方法可以将自定义 CSS 添加到 Shiny 应用程序,如此处所述.在可重现示例中将 CSS 添加到 Shiny 应用程序的首选方法是在代码中添加 CSS,而不是在单独的文件中.这可以通过在应用程序的 ui 中添加一行来完成,例如如下:

There are multiple ways to add custom CSS to a Shiny application, as explained here. The preferred way to add CSS to a Shiny application in a reproducible example is to add the CSS in the code, rather than in a separate file. This can be done by adding a line in the ui of an application, for example as follows:

tags$head(tags$style(HTML('body {background-color: lightblue;}'))),

这篇关于如何将包含多个文件的 Shiny 应用程序转换为易于共享和重现的 Shiny 示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆