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

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

问题描述

有关如何在堆栈溢出和上创建最小,完整和可验证的示例的资源, href ="https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example">如何制作出色的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?

推荐答案

示例数据

当然,关于问题也适用.总结一下:确保不需要其他文件即可运行代码.使用样本数据集,例如mtcars,或使用data.frame()创建一些样本数据.如果您的数据非常复杂,并且确实需要复杂性来说明问题,则也可以使用dput().除非您对fileInput之类的功能有疑问,否则请避免使用read.csv()之类的功能.

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.

发光的应用程序通常包含两个或三个文件(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(…)包装用户界面,
  • 具有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)

工作示例

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

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,而不是在单独的文件中添加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天全站免登陆