将参数传递给反应函数
Pass a parameter to reactive function
我的问题如下,我想它有一个简单的解决方案。但是我看了 create reactive function from user input and Reactive Function Parameter,两者都没有回答我的问题。
我有一个图表,其中的 X 轴和 Y 轴可能会根据用户输入而改变。用户将能够单击图表,并且我有一个文本显示,上面写着 'You have selected a (name of x label) value of xxx, and a (name of y label) value of yyy' 的内容。显然,如果 x 或 y 标签的名称以元音开头,我想使用 'an' 而不是 'a'。目前这个函数我已经在服务器模块中写了两次,但是这样不是很优雅。
有没有办法在服务器函数中定义一个函数,我可以将标签名称发送到该函数,并且只需 return 'a' 或 'an'?下面的代码。请注意,Pokemon 数据集随 highcharter 包一起传输,可以从 CRAN 下载。
library(ggplot2)
library(highcharter)
myData <- pokemon
ui <- fluidPage(
# Some custom CSS for a smaller font for preformatted text
tags$head(
tags$style(HTML("
pre, table.table {
font-size: smaller;
}
"))
),
tags$head(tags$style(type = "text/css", ".shiny-text-output, .well{background-color: #EFF8CD;}")),
fluidRow(
column(width = 3
),
column(width = 5,
plotOutput("plot1", height = 450,
# Equivalent to: click = clickOpts(id = "plot_click")
click = "plot_click"
)
),
column(width = 4,
## Text output here.
wellPanel(
h4("Your results"),
htmlOutput("chartDetails")
)
)
)
)
server <- function(input, output) {
output$plot1 <- renderPlot({
ggplot(myData, aes(weight, attack)) + geom_point()
}
)
## Extract some values when plot is clicked
inputX <- reactive({
if (is.null(input$plot_click$x))
{
-999
}
else
{
round(input$plot_click$x, 0)
}
})
inputY <- reactive({
if (is.null(input$plot_click$y))
{
-999
}
else
{
round(input$plot_click$y, 0)
}
})
labelX <- eventReactive(input$plot_click, {
input$plot_click$mapping$x
})
labelY <- eventReactive(input$plot_click,{
input$plot_click$mapping$y
})
## count the number of points that have a higher x and y.
mySubset <- eventReactive(input$plot_click, {
#myFirstSubset <- subset(myData, weight > inputX())
subset(myData, labelX() > inputX() & labelY() > inputY())
})
## Create relevant strings out of the inputX and inputY values.
stringX <- reactive({
if (inputX() > -999)
{
myString <- "You have selected"
if (substr(labelX(), 1,1) %in% c("a", "e", "i", "o", "u"))
{
myString <- paste(myString, "an")
}
else
{
myString <- paste(myString, "a")
}
paste(myString, labelX(), "of", inputX())
}
else
{
""
}
})
stringY <- reactive({
if (inputY() > -999)
{
myString <- "and"
if (substr(labelY(), 1,1) %in% c("a", "e", "i", "o", "u"))
{
myString <- paste(myString, "an")
}
else
{
myString <- paste(myString, "a")
}
paste(myString, labelY(), "of", inputY())
}
else
{
""
}
})
stringCount <- reactive({
if (inputY() > -999 && inputX() > -999)
{
paste("The number of records with higher",labelX(), "and", labelY(), "is", nrow(mySubset()))
}
else
{
""
}
})
## Post the results to the chart details well.
output$chartDetails <- renderUI({
if (inputX() > -999 && inputY() > -999) {
HTML(paste(stringX(), "<br>",
stringY(), "<br>",
stringCount()))
}
else
{
HTML("Click on the chart")
}
})
}
shinyApp(ui, server)
我不知道这是不是你想要的,但是你可以用这两个函数代替元音函数
vowel <- function(myString, label){
if (substr(label, 1,1) %in% c("a", "e", "i", "o", "u"))
{
myString <- paste(myString, "an")
}
else
{
myString <- paste(myString, "a")
}
return(myString)
}
## Create relevant strings out of the inputX and inputY values.
stringX <- reactive({
if (inputX() > -999)
{
myString <- "You have selected"
paste(vowel(myString,labelX()), labelX(), "of", inputX())
}
else
{
""
}
})
stringY <- reactive({
if (inputY() > -999)
{
myString <- "and"
paste(vowel(myString,labelY()), labelY(), "of", inputY())
}
else
{
""
}
})
Dieter Menne 建议的简单解决方案如下:
UI/Server 函数之外:
myConnective <- function(aString) {
if (substr(aString, 1,1) %in% c("a", "e", "i", "o", "u"))
{
myString <- "an"
}
else
{
myString <- "a"
}
return(myString)
}
然后在服务器函数里面:
stringX <- reactive({
if (inputX() > -999)
{
paste("You have selected", myConnective(labelX()), labelX(), "of", inputX() )
}
else
{
""
}
})
我的问题如下,我想它有一个简单的解决方案。但是我看了 create reactive function from user input and Reactive Function Parameter,两者都没有回答我的问题。
我有一个图表,其中的 X 轴和 Y 轴可能会根据用户输入而改变。用户将能够单击图表,并且我有一个文本显示,上面写着 'You have selected a (name of x label) value of xxx, and a (name of y label) value of yyy' 的内容。显然,如果 x 或 y 标签的名称以元音开头,我想使用 'an' 而不是 'a'。目前这个函数我已经在服务器模块中写了两次,但是这样不是很优雅。
有没有办法在服务器函数中定义一个函数,我可以将标签名称发送到该函数,并且只需 return 'a' 或 'an'?下面的代码。请注意,Pokemon 数据集随 highcharter 包一起传输,可以从 CRAN 下载。
library(ggplot2)
library(highcharter)
myData <- pokemon
ui <- fluidPage(
# Some custom CSS for a smaller font for preformatted text
tags$head(
tags$style(HTML("
pre, table.table {
font-size: smaller;
}
"))
),
tags$head(tags$style(type = "text/css", ".shiny-text-output, .well{background-color: #EFF8CD;}")),
fluidRow(
column(width = 3
),
column(width = 5,
plotOutput("plot1", height = 450,
# Equivalent to: click = clickOpts(id = "plot_click")
click = "plot_click"
)
),
column(width = 4,
## Text output here.
wellPanel(
h4("Your results"),
htmlOutput("chartDetails")
)
)
)
)
server <- function(input, output) {
output$plot1 <- renderPlot({
ggplot(myData, aes(weight, attack)) + geom_point()
}
)
## Extract some values when plot is clicked
inputX <- reactive({
if (is.null(input$plot_click$x))
{
-999
}
else
{
round(input$plot_click$x, 0)
}
})
inputY <- reactive({
if (is.null(input$plot_click$y))
{
-999
}
else
{
round(input$plot_click$y, 0)
}
})
labelX <- eventReactive(input$plot_click, {
input$plot_click$mapping$x
})
labelY <- eventReactive(input$plot_click,{
input$plot_click$mapping$y
})
## count the number of points that have a higher x and y.
mySubset <- eventReactive(input$plot_click, {
#myFirstSubset <- subset(myData, weight > inputX())
subset(myData, labelX() > inputX() & labelY() > inputY())
})
## Create relevant strings out of the inputX and inputY values.
stringX <- reactive({
if (inputX() > -999)
{
myString <- "You have selected"
if (substr(labelX(), 1,1) %in% c("a", "e", "i", "o", "u"))
{
myString <- paste(myString, "an")
}
else
{
myString <- paste(myString, "a")
}
paste(myString, labelX(), "of", inputX())
}
else
{
""
}
})
stringY <- reactive({
if (inputY() > -999)
{
myString <- "and"
if (substr(labelY(), 1,1) %in% c("a", "e", "i", "o", "u"))
{
myString <- paste(myString, "an")
}
else
{
myString <- paste(myString, "a")
}
paste(myString, labelY(), "of", inputY())
}
else
{
""
}
})
stringCount <- reactive({
if (inputY() > -999 && inputX() > -999)
{
paste("The number of records with higher",labelX(), "and", labelY(), "is", nrow(mySubset()))
}
else
{
""
}
})
## Post the results to the chart details well.
output$chartDetails <- renderUI({
if (inputX() > -999 && inputY() > -999) {
HTML(paste(stringX(), "<br>",
stringY(), "<br>",
stringCount()))
}
else
{
HTML("Click on the chart")
}
})
}
shinyApp(ui, server)
我不知道这是不是你想要的,但是你可以用这两个函数代替元音函数
vowel <- function(myString, label){
if (substr(label, 1,1) %in% c("a", "e", "i", "o", "u"))
{
myString <- paste(myString, "an")
}
else
{
myString <- paste(myString, "a")
}
return(myString)
}
## Create relevant strings out of the inputX and inputY values.
stringX <- reactive({
if (inputX() > -999)
{
myString <- "You have selected"
paste(vowel(myString,labelX()), labelX(), "of", inputX())
}
else
{
""
}
})
stringY <- reactive({
if (inputY() > -999)
{
myString <- "and"
paste(vowel(myString,labelY()), labelY(), "of", inputY())
}
else
{
""
}
})
Dieter Menne 建议的简单解决方案如下:
UI/Server 函数之外:
myConnective <- function(aString) {
if (substr(aString, 1,1) %in% c("a", "e", "i", "o", "u"))
{
myString <- "an"
}
else
{
myString <- "a"
}
return(myString)
}
然后在服务器函数里面:
stringX <- reactive({
if (inputX() > -999)
{
paste("You have selected", myConnective(labelX()), labelX(), "of", inputX() )
}
else
{
""
}
})