为清楚起见而编辑:
我正在以闪亮的方式构建应用程序,以在散点图中浏览数据。简化的代码如下:
library(shiny)
library(ggplot2)
library(DT)
library(tools)
library(dplyr)
library(tidyverse)
#load data
Data <- Electorate_Data
# Define UI for application
ui <- fluidPage(
titlePanel("Scatterplot"),
br(),
# Sidebar layout with a input and output definitions
sidebarLayout(
# Inputs
sidebarPanel(
# Select variable for y-axis
selectInput(inputId = "y", label = "Y-axis:",
choices = colnames(Data[6:32])
),
# Select variable for x-axis
selectInput(inputId = "x", label = "X-axis:",
choices = colnames(Data[6:32])
),
width = 6
),
# Output:
mainPanel(
# Create a container for tab panels
tabsetPanel(
tabPanel(
title = "Explore the Data",
# Show scatterplot
plotOutput(outputId = "scatterplot")
)
),
width = 6
)
)
)
# Define server function required to create the scatterplot
server <- function(input, output) {
# Create scatterplot object the plotOutput function is expecting
output$scatterplot <- renderPlot({
ggplot(data = Data, aes_string(x = input$x, y = input$y)) +
geom_point()
})
}
# Create a Shiny app object
shinyApp(ui = ui, server = server)
我的问题是,当我选择列名(使用函数colnames)作为selectInput函数中的选项,并且colnames中包含空格(即“ Year 12”而不是Year12)时,我的散点图不起作用,并显示错误消息:
错误:: 1:6:意外的“输入” 1:出生于
代码示例
# Select variable for y-axis
selectInput(inputId = "y", label = "Y-axis:",
choices = colnames(Data[6:32])
现在,如果我为每个变量加上别名(例如,使用以下代码),则该应用程序可以正常运行:
# Select variable for y-axis
selectInput(inputId = "y", label = "Y-axis:",
choices = "Year12" = "Year 12",
"Born_Aus" = "Born in Australia",
"NoSchool" = "% of the Population Above 15 Years of Age that Didn't Attend School")
),
我的问题是-有没有一种方法可以更有效地对名称进行别名,以使别名处理过程自动化。我已经尝试过使用names()函数的几种破解方法,但到目前为止,它仅引发了错误。
最终,我可以手动解决此问题,但是肯定有更好的方法。
谢谢
编辑:
如果有帮助,我已经包括了一部分数据。您必须更改以下代码:
# Select variable for x-axis
selectInput(inputId = "x", label = "X-axis:",
choices = colnames(Data[6:32])
),
公正
choices = colnames(Data)
对于X和Y selectInputs
数据:
> head(Electorate_Data[14:18])
Born in Australia LOTE NoSchool Year12 Median_age
1 0.6126271 0.29805068 0.012132744 0.5481394 36
2 0.6419959 0.27278743 0.006160949 0.4610346 39
3 0.8234175 0.05199925 0.002323880 0.3564276 40
4 0.5633673 0.45200442 0.011578501 0.4933828 38
5 0.8186847 0.06066808 0.005270832 0.2701636 44
6 0.4439803 0.59099798 0.017304021 0.5374834 35
答案 0 :(得分:1)
通常,假设您有一个由selectInput()
返回的名称向量,称为x
。您还有第二个同样长的向量,该向量的名称就是您进行选择时用户看到的名称,称为y
。您可以通过首先给x
命名y
来完成所需的工作,如下所示:
names(x)<-y
然后,您可以在x
的{{1}}参数中使用choices=
。用户将看到selectInput()
中的名称作为选择,但是y
将返回selectInput()
中的值。
答案 1 :(得分:0)
我不知道为什么您的光泽不支持包含空格的列名称。下面的代码在我的机器上工作正常。提供可复制的示例始终可以帮助他人帮助您。
library(shiny)
df <- iris[,-5]
colnames(df) <- sub('\\.', ' ', colnames(df))
ui <- fluidPage(
titlePanel("Scatterplot and Regression Calculator"),
br(),
# Sidebar layout with a input and output definitions
sidebarLayout(
# Inputs
sidebarPanel(
# Select variable for y-axis
selectInput(inputId = "y", label = "Y-axis:",
choices = colnames(df)
)
),
mainPanel(
plotOutput(outputId = "plot")
)
)
)
server <-function(input, output) {
output$plot <- renderPlot({
hist(df[[input$y]])
})
}
shinyApp(ui = ui, server = server)