shiny day 错误
![R Shiny themes and error messages](https://dab1nmslvvntp.cloudfront.net/wp-content/uploads/2016/10/1477467367r-shiny.jpg)
The purpose of Shiny is to provide an intuitive and user-friendly interface to R. R is a highly popular statistical environment for doing heavy data analysis and constructing statistical models, and therefore is highly popular among data scientists. However, for a user with a non-coding background, using R to conduct such analysis can become quite intensive. This is where Shiny Web Apps come in. Essentially, Shiny allows for a more intuitive graphical user interface that is still capable of conducting sophisticated data analysis — without the need for extensive coding on the part of the end user.
Shiny的目的是为R提供一个直观且用户友好的界面。R是用于进行大量数据分析和构建统计模型的非常流行的统计环境,因此在数据科学家中非常受欢迎。 但是,对于具有非编码背景的用户,使用R进行此类分析可能会变得非常密集。 这就是Shiny Web Apps的用武之地。从本质上讲,Shiny允许使用更直观的图形用户界面,该界面仍然能够进行复杂的数据分析-无需最终用户进行大量编码。
In my article on using Shiny with R and HTML, I illustrated how an interactive web application can be created to conduct analysis without the need for direct manipulation of code. In this article, I’ll use a slightly different model to illustrate how the Shiny environment can be customized to work with the end user in a more intuitive fashion. Essentially, the goal of this article is to illustrate how a user can:
在关于将Shiny与R和HTML结合使用的文章中,我说明了如何创建交互式Web应用程序来进行分析而无需直接操作代码。 在本文中,我将使用一个稍有不同的模型来说明如何自定义Shiny环境,以便以更直观的方式与最终用户一起使用。 本质上,本文的目标是说明用户如何:
- Build an application by linking the UI and server side 通过链接UI和服务器端来构建应用程序
- How to customize the themes available in the Shiny Themes library 如何自定义Shiny Themes库中可用的主题
- Implement error messages in order to provide guidance to an end user on how to use a particular program 实施错误消息,以便为最终用户提供有关如何使用特定程序的指导
The program itself that is developed for this tutorial is quite basic: a slider input allows the user to manipulate a variable within the program by means of reactivity, which causes instantaneous changes in the line plot output that is developed by means of reactivity.
本教程开发的程序本身是非常基础的:滑块输入允许用户通过React性来操作程序中的变量,这会导致通过React性开发的折线图输出中的瞬时变化。
This inherent function gives Shiny a significant advantage over using R code as a stand-alone. Traditionally, in order to analyze the change in a particular variable, the code must be manipulated directly (or the data from which the code is reading), and this can ultimately become very inefficient. However, Shiny greatly speeds up this process by allowing the user to manipulate the variables in a highly intuitive manner, and changes are reflected instantly.
与R代码独立使用相比,此固有功能为Shiny提供了显着的优势。 传统上,为了分析特定变量的变化,必须直接操纵代码(或从中读取代码的数据),这最终会变得非常低效。 但是,Shiny通过允许用户以高度直观的方式操纵变量来极大地加快了此过程,并且更改会立即反映出来。
However, the whole purpose of Shiny is to make an R Script as interactive as possible. In this regard, the user will want to be able to add features to the program that go well beyond reactivity. Two such aspects of this that I will discuss in this tutorial are:
但是,Shiny的全部目的是使R脚本尽可能地交互 。 在这方面,用户将希望能够向程序添加远远超出React性的功能。 我将在本教程中讨论的两个方面是:
Using
shinythemes
in order to customize the appearance of our Shiny appearance使用
shinythemes
来定制我们Shiny外观的外观Constructing a
validate()
function in order to display an alert once variables are manipulated in a certain manner构造
validate()
函数以便以某种方式操作变量后显示警报
闪亮主题 (Shiny Themes)
In the UI of our original program, we did not specify a theme, and therefore ended up with quite a basic (default Bootstrap) view of our output:
在原始程序的UI中,我们没有指定主题,因此最终得到了输出的基本视图(默认为Bootstrap):
![default Shiny app view](https://dab1nmslvvntp.cloudfront.net/wp-content/uploads/2016/10/1477467634image1-1024x429.png)
When we defined the shinyUI(fluidPage(…))
function, we didn’t specify a theme. In this instance, we’re going to specify several of the themes available from the Shiny Themes package.
当我们定义shinyUI(fluidPage(…))
函数时,我们没有指定主题。 在这种情况下,我们将指定Shiny Themes包中可用的几个主题 。
After we’ve specified shinyUI(fluidPage(…))
, we’re also specifying our theme as follows:
在指定了shinyUI(fluidPage(…))
,我们还要如下指定主题:
shinyUI(fluidPage(theme = shinytheme("cyborg")
![the cyborg theme](https://dab1nmslvvntp.cloudfront.net/wp-content/uploads/2016/10/1477465881image2-1024x454.png)
From the above, we see that selecting the Cyborg theme gives our web app a dark background. As another example, let’s now select the Spacelab theme and see what we come up with:
从上面可以看到,选择“半机械人”主题使我们的Web应用程序具有深色背景。 作为另一个示例,让我们现在选择Spacelab主题,然后看看我们想到了什么:
![the spacelab theme](https://dab1nmslvvntp.cloudfront.net/wp-content/uploads/2016/10/1477467736image3-1024x389.png)
These themes demonstrate more levels of customization depending on the other attributes in the Shiny code, such as additional buttons, tabs, etc. Feel free to experiment with the other themes at the RStudio GitHub account and see what you come up with!
这些主题展示了更多的自定义级别,具体取决于Shiny代码中的其他属性,例如其他按钮,选项卡等。您可以在RStudio GitHub帐户上随意尝试其他主题,然后看看您的想法!
validate()函数 (validate() function)
The validate function is an indispensable tool when it comes to working with Shiny code. Essentially, validate allows for an alert message to be triggered when a certain part of the Shiny program is manipulated.
在处理Shiny代码时,validate函数是必不可少的工具。 本质上,validate允许在处理Shiny程序的特定部分时触发警报消息。
In this particular instance, I discuss how we can use the validate
and need
functions to trigger an alert message when we manipulate the slider in our program.
在这个特定的例子中,我讨论了当我们在程序中操纵滑块时如何使用validate
和need
功能来触发警报消息。
The program itself, as last time, is quite basic in terms of its function. Essentially, our Shiny program produces a value on the X-axis between 1 and 100 depending on that selected by the slider.
上次,程序本身在功能上是非常基本的。 本质上,我们的Shiny程序会根据滑块选择的值在X轴上生成一个介于1和100之间的值。
We are then using the corresponding y-value to produce an error message whenever the calculated slope falls below 5 — that is, at a value of 29 or greater. Note that the program is not calculating slope directly, but instead the user in this instance is using the value of 29 as the cutoff point for this slope based on manual calculation.
然后,只要计算出的斜率低于5(即29或更大),我们就使用相应的y值生成错误消息 。 请注意,程序不是直接计算斜率,而是在这种情况下,用户根据手动计算使用值29作为该斜率的截止点。
Within the user interface, we specify the validation trigger under our shinyUI as follows:
在用户界面中,我们在ShinyUI下指定验证触发器,如下所示:
shinyUI(fluidPage(theme = shinytheme("spacelab"),
tags$head(
tags$style(HTML("
.shiny-output-error-validation {
color: red;
}
"))
),
Under the server, we specify the validate
and need
commands under the input-output
function:
在服务器下,我们在input-output
功能下指定validate
和need
命令:
function(input, output) {
output$algebra < - renderPlot({
validate(
need(input$lambda <= 28, "A value set at 29 or above produces a slope below 5. Please set a value below 29.")
)
完整程序代码 (Full Program Code)
The UI and Server code as a whole is displayed below:
UI和服务器代码整体如下所示:
用户界面 (UI)
#Load libraries
library(shiny)
library(shinythemes)
#Define Shiny theme and specify shiny-output-error-validation
shinyUI(fluidPage(theme = shinytheme("spacelab"),
tags$head(
tags$style(HTML("
.shiny-output-error-validation {
color: red;
}
"))
),
sidebarLayout(
sidebarPanel(
sliderInput("lambda",
"Number of data points:",
min = 1,
max = 100,
value = 1)
),
mainPanel(
h3("Slope of a line", style = "font-family: 'Jura'; color: blue; font-size: 32px;"),
HTML("<p>Excluding slope below 5 where y = 2x + 3"),
plotOutput("algebra")
)
)
))
服务器 (Server)
#Load libraries
library(shiny)
library(ggplot2)
library(scales)
#Define validate function
function(input, output) {
output$algebra <- renderPlot({
validate(
need(input$lambda <= 28, "A value set at 29 or above produces a slope below 5. Please set a value below 29.")
)
#Define y as 2x+3 (using lambda to represent x variable)
n <- 1:100
lambda <- seq(min(n), max(n), length.out = input$lambda)
plot((2*lambda)+3, type = "o", col = 'blue',
fg = 'blue', xlab= "X (number of data points)", ylab = "Y = 2x+3")
})
}
As a quick recap, we run the program by:
快速回顾一下,我们通过以下方式运行程序:
Opening R Studio and selecting the New Icon → Shiny Web App
打开R Studio并选择New Icon → Shiny Web App
![opening a new Shiny app](https://dab1nmslvvntp.cloudfront.net/wp-content/uploads/2016/10/1477465959image4.png)
We see that we then have an option to choose single file (with both
UI.R
andserver.R
), or a multiple file (whereUI.R
andserver.R
are in separate scripts). We will use multiple file here.我们看到,然后我们可以选择一个文件(同时具有
UI.R
和server.R
)或多个文件(其中UI.R
和server.R
在单独的脚本中)。 我们将在这里使用多个文件 。
![choosing multiple file](https://dab1nmslvvntp.cloudfront.net/wp-content/uploads/2016/10/1477465995image5.png)
Now, we paste the above UI and server codes into their respective sections and run the program by selecting Run App.
现在,我们将上面的UI和服务器代码粘贴到它们各自的部分中,然后通过选择Run App运行该程序。
![running the app](https://dab1nmslvvntp.cloudfront.net/wp-content/uploads/2016/10/1477466030image6-1024x555.png)
We see that our line graph grows in data points on the x-axis depending on how we manipulate the slider. However, you’ll notice that once the slider is moved to a value of 29 or above, the error message is displayed, “A value set at 29 or above produces a slope below 5. Please set a value below 29”.
我们看到折线图在x轴上的数据点中增长,这取决于我们如何操纵滑块。 但是,您会注意到,一旦将滑块移动到29或更高的值,就会显示错误消息, “设置为29或更高的值会产生小于5的斜率。请设置为29以下的值” 。
![error message display](https://dab1nmslvvntp.cloudfront.net/wp-content/uploads/2016/10/1477466075image7-1024x182.png)
Displaying of such error messages can be highly useful when a developer wishes to direct an external user on the use of such a program. For instance, if it was the case that no error messages were to be displayed, then the user has free rein to make inputs into the program that might not necessarily be suitable. In this way, the developer can give further safeguards against inadvertent inputs by an external user, while doing this solely in R code leaves the inputs open to manipulation.
当开发人员希望指导外部用户使用此类程序时,显示此类错误消息非常有用。 例如,如果情况是不显示任何错误消息,则用户可以自由控制输入不一定适合的程序。 这样,开发人员可以提供进一步的保护措施,以防止外部用户的无意输入,而仅用R代码执行此操作会使输入易于操作。
For instance, the Shiny website itself gives an example of how the validate
function can also be applied to a list. The UI and server produce a histogram as output, but the program produces an error message when a particular dataset is selected in the list. In this way, the validate
command does exactly that — validates a particular input, while the need
function is what allows us to personalize the same by customizing our error message — that is, input$lambda <= 28
, “A value set at 29 or above produces a slope below 5. Please set a value below 29.”
例如, Shiny网站本身提供了一个示例,说明如何将validate
函数也应用于列表。 UI和服务器会生成一个直方图作为输出,但是在列表中选择特定的数据集时,程序会生成一条错误消息。 这样, validate
命令可以准确地做到这一点-验证特定的输入,而need
函数正是通过自定义错误消息使我们能够对其进行个性化设置的功能,即input$lambda <= 28
, “将值设置为29或大于5的斜率小于5。请设置小于29的值。
Moreover, while we only specified one error message, we could potentially specify multiple. For instance, let’s now suppose that we want to display an additional error message when the slider goes above a value of 35:
此外,虽然我们仅指定了一条错误消息,但我们可能会指定多条错误消息。 例如,现在让我们假设,当滑块的值超过35时,我们希望显示其他错误消息:
function(input, output) {
output$algebra <- renderPlot({
validate(
need(input$lambda <= 28, "A value set at 29 or above produces a slope below 5. Please set a value below 29."),
need(input$lambda <= 35, "Your slider is now at a value greater than 35!")
)
Using an additional need
function in the server code, manipulating the slider to a value above 35 means that we now display the additional error message, “Your slider is now at a value greater than 35!”
在服务器代码中使用附加的need
功能,将滑块调整为大于35的值意味着我们现在显示附加的错误消息, “您的滑块现在的值大于35!”
![highlighted error message](https://dab1nmslvvntp.cloudfront.net/wp-content/uploads/2016/10/1477466110image8.png)
结论 (Conclusion)
In this article, we’ve seen how to manipulate the design of our Shiny app using Shiny Themes, and learned how to apply filters to the parameters of our code and display error messages when certain conditions have been breached.
在本文中,我们了解了如何使用Shiny Themes来操纵Shiny应用程序的设计,并学习了如何在违反某些条件的情况下将过滤器应用于代码参数并显示错误消息。
Ultimately, the focus of Shiny is in allowing for a far greater user-friendly and intuitive experience in conducting analysis than would be possible using R alone, and also minimizes risk of user input errors by properly specifying the parameters for a particular program.
最终,与单独使用R相比,Shiny的重点在于允许进行分析时提供更多的用户友好和直观的体验,并且通过为特定程序正确指定参数,还可以最大程度地减少用户输入错误的风险。
翻译自: https://www.sitepoint.com/shiny-and-r-how-to-add-themes-and-customize-error-messages/
shiny day 错误