我正在尝试使用selectInput为ggplot定义构面,因此facet_grid的输入是字符串或列名。选项之一是“无”(不是列之一) 如果未选择,则下面的示例只要“ none”即可工作。我在如何设置将“ none”选项定义为NULL方面感到困惑。
我尝试了以下操作,但收到以下错误“错误:cols
必须为NULL
或vars()
规范”。
v <- ifelse(rfacet() == "none",NULL,vars(!!rlang::sym(rfacet())))
# Module - facets options-------------------------------------------------------
#UI
plotOpt_rowfacet_UI <- function(id){
ns <- NS(id)
uiOutput(ns("ui_opt_rowfacet"))
}
#SERVER
plotOpt_rowfacet <- function(input,output,session,facet_choices="none"){
output$ui_opt_rowfacet <- renderUI({
ns <- session$ns
selectInput(inputId = ns("opt_rowfacet"),label = "Y-variable:",choices = facet_choices,selected = "none",multiple = FALSE,selectize = FALSE)
})
return(reactive(input$opt_rowfacet)) #returns column name as string
}
library(shiny)
library(dplyr)
library(tidyverse)
ui <- fluidPage(
plotOpt_rowfacet_UI("test1"),h4("facet selected ouput from module"),verbatimTextOutput("y_out"),plotOutput("plot")
)
server <- function(input,session){
rfacet <- callModule(module=plotOpt_rowfacet,id="test1",facet_choices= c("none","mpg","disp"))
output$y_out <- renderPrint({
str(rfacet())
})
#this works except if none is selected
#how can set to NULL if input="none"
output$plot <- renderPlot({
#v <- ifelse(rfacet() == "none",vars(!!rlang::sym(rfacet())))
p <- ggplot(mtcars,aes(x=wt,y=disp))+
geom_point()+
facet_grid(rows=vars(cyl),cols=vars(!!rlang::sym(rfacet())))
print(p)
})
}
shinyApp(ui,server)