[英]In a shiny app how can I let the user choose the filename and directory to download with write.table
[英]how to let user choose the x_var in Shiny plot?
TD <- thyroid
library(readxl)
library(shiny)
library(ggplot2)
library(shinythemes)
library(DT)
ui <-shinyUI(fluidPage(pageWithSidebar(
headerPanel("Test App"),
sidebarPanel(
selectInput("xaxis", "Choose a x variable", choices = names(TD)),
actionButton("goButton","Update")
),
mainPanel(
tabsetPanel(
tabPanel('Plot', plotOutput("plot1"))
))
)
))
server <- shinyServer(function(input,output, session){
x_var<- eventReactive(input$goButton, {
input$xaxis
})
output$plot1 <- renderPlot({
x <- x_var()
x
p <- ggplot() + geom_bar(aes(TD$x_var, fill = TD$ThryroidClass))
p #+
#theme(plot.title = element_text(hjust = 0.5, size=20))
})
})
shinyApp(ui,server)
問題出在TD$x_var
中。 我想引用從 selectInput 中選擇的變量,但使用這段代碼我得到“Erroe [object object]”。
在renderPlot
內部, x_var()
已經分配給x
。 因此,我們調用x
而不是x_var
。 此外,為了評估 object,我們可以使用[[
而不是$
output$plot1 <- renderPlot({
x <- x_var()
x
p <- ggplot() +
geom_bar(aes(TD[[x]], fill = TD$ThryroidClass))
p #+
#theme(plot.title = element_text(hjust = 0.5, size=20))
})
})
或者不提取列,而是在指定data
后使用不帶引號的列名。 這也將正確命名圖例
output$plot1 <- renderPlot({
x <- x_var()
x
p <- ggplot(TD) +
geom_bar(aes(.data[[x]], fill =ThryroidClass))
p #+
#theme(plot.title = element_text(hjust = 0.5, size=20))
})
})
-使用iris
的可重現示例
TD <- iris
ui <-shinyUI(fluidPage(pageWithSidebar(
headerPanel("Test App"),
sidebarPanel(
selectInput("xaxis", "Choose a x variable", choices = names(TD)[1:4]),
actionButton("goButton","Update")
),
mainPanel(
tabsetPanel(
tabPanel('Plot', plotOutput("plot1"))
))
)
))
server <- shinyServer(function(input,output, session){
x_var<- eventReactive(input$goButton, {
input$xaxis
})
output$plot1 <- renderPlot({
x <- x_var()
p <- ggplot(TD) +
geom_bar(aes(.data[[x]], fill =Species))
p
})
})
shinyApp(ui,server)
-輸出
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.