在有光泽的应用程序中通过selectInput传递列名 [英] Passing column name via selectInput in shiny application
问题描述
我有一个简单的闪亮应用程序,我想将 selectInput
中的值作为数据框的列名传递,并在ggplot中使用它.我的UI代码如下所示:
I have a simple shiny app, I would like to pass the value from the selectInput
as a column name of data frame and use it in ggplot. My UI code looks like that:
library(shiny)
# Define UI for application that draws a histogram
shinyUI(fluidPage(
# Application title
titlePanel("Title"),
# Sidebar with a slider input for the number of bins
sidebarLayout(
sidebarPanel(
selectInput(inputId = "yaxis",
label = "Y-axis",
choices = list("Overall Rank" = "overall_rank",
"Income Deprivation" = "income_deprivation_rank"),
selected = "income_deprivation_rank"),
selectInput(inputId = "xaxis",
label = "X-axis",
choices = list("Overall Rank" = "overall_rank",
"Income Deprivation" = "income_deprivation_rank"),
selected = "overall_rank")),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot"),
h5("Notes"),
p("notes")
)
)
))
我的服务器端代码非常简单,我有一个SPARQL查询(在此处剪裁以节省空间),它创建了简单的数据框:
My server side code is vary simple, I have a SPARQL query (cut here to save the space) that creates simple data frame:
# Libs
require(shiny); require(SPARQL); require(ggplot2)
# Server function
shinyServer(function(input, output) {
# Source the data
## Define endpoint URL.
endpoint <- "http://data.opendatascotland.org/sparql.csv"
### Create Query
query.simd <- "PREFIX stats: <http://statistics.data.gov.uk/id/statistical-geography/>
(...) cut to save space (...)"
## Make the data
dta.simd<- SPARQL(url = endpoint, query = query.simd, format = "csv")$results
## Make the plot
output$distPlot <- renderPlot({
xaxis <- as.character(input$xaxis)
yaxis <- as.character(input$yaxis)
# draw the the plot
ggplot(data = dta.simd, aes(x = xaxis, y = yaxis)) +
geom_point(shape=1)
})
})
查询会生成一个简单的数据框,从而重新提取以下内容:
The query results in a simple data frame, resambling the extract below:
observation overall_rank income_deprivation_rank
a001 2 6
a002 10 7
a003 11 9
编译应用程序后,我继续收到错误:找不到对象'xaxis'.这使我相信,无论出于何种原因, input $ xaxis
的值都不会传递给 xaxis
对象,因此无法在ggplot中使用.如果我决定用与列名相对应的字符串替换 as.character(input $ yaxis)
,例如 overall_rank
和另外一个 income_deprivation_rank
应用程序可以正常工作,因此问题显然与使用 input $ xaxis
值有关.我尝试了没有 as.character()
函数的代码,但收到了相同的错误消息.
After compiling the app I keep on receiving the Error: object 'xaxis' not found. This leads me to believe that for whatever reason the value from the input$xaxis
is not passes to the xaxis
object and cannot be used in ggplot. If I decide to replace the as.character(input$yaxis)
with a string corresponding to the column name, like for instance overall_rank
and the other one income_deprivation_rank
the app works as it should, so the problem is clearly associated with using the input$xaxis
value. I tried the code with no as.character()
function but got the same error message.
推荐答案
通常您通常将列名引用为类似于 input $ colName
的字符串,将其替换为 get(input $colName)
.这样,Shiny便知道获取 input $ colName
的值,而不是将其视为字符串.
Wherever you would normally reference the column name as a string like input$colName
, replace it with get(input$colName)
. This way Shiny knows to fetch the value of input$colName
instead of treating it as a string.
这篇关于在有光泽的应用程序中通过selectInput传递列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!