[英]Passing column name via selectInput in shiny application
我有一個簡單的閃亮應用程序,我想將selectInput
的值作為數據框的列名傳遞,並在ggplot中使用它。 我的UI代碼如下所示:
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查詢(在此處剪裁以節省空間)可創建簡單的數據幀:
# 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)
})
})
該查詢將產生一個簡單的數據框,從而重新提取以下摘錄:
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
相關聯input$xaxis
值。 我嘗試了沒有as.character()
函數的代碼,但收到了相同的錯誤消息。
通常,在通常將列名引用為字符串的地方(如input$colName
,請將其替換為get(input$colName)
。 這樣,Shiny便知道獲取input$colName
的值,而不是將其視為字符串。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.