我在使用有条件语句时遇到了问题。我希望用户选择变量的数量。如果选择1个变量,则选择1个变量的绘图图(ex密度图),如果选择2个变量,则选择2个变量的绘图图(ex散点图)。我试过几种方法,但输出结果与我所期望的不一样。如何在闪亮的服务器中使用if server语句?Tks
用户界面
df <- mtcars
ui <- fluidPage(
h1("My first app",
style = 'color: green;
font-style: italic'),
hr(),
fluidRow(
sidebarPanel(
radioButtons(inputId = "number",
label = "Select number of variable",
choices = c("1 variable" = 1,
"2 variable" = 2)),
selectInput(inputId = "x",
label = "Variable 1",
choices = names(df)),
conditionalPanel(
condition = "input.number == 2",
selectInput(inputId = "y",
label = "Variable 2",
choices = names(df))
)
),
column(8, plotOutput("plot"))
),
hr(),
plotOutput("plot") )服务器
server <- function(input, output, session){
observeEvent(input$x,
{updateSelectInput(session,
inputId = "y",
label = "Variable 2",
choices = names(df)[names(df) != input$x])
})
data <- reactive({
if(input$number == 1){
data <- df %>%
select(input$x)
} else {
data <- df %>%
select(input$x, input$y)
}
})
output$plot <- renderPlot({
if(input$number == 1){
ggplot(data = data(),
x = get(input$x))+
geom_density()
} else {
ggplot(data = data,
x = get(input$x),
y = get(input$y)) +
geom_point()
}
})
}
shinyApp(ui = ui, server = server)发布于 2021-07-12 06:50:30
你可以试试下面的代码-
plotOutput("plot")被提到两次,删除它只包含它一次。reactive中创建数据集时检查条件,在地形码本身中处理它。.data来引用ggplot代码中的列名。library(shiny)
library(ggplot2)
df <- mtcars
ui <- fluidPage(
h1("My first app",
style = 'color: green;
font-style: italic'),
hr(),
fluidRow(
sidebarPanel(
radioButtons(inputId = "number",
label = "Select number of variable",
choices = c("1 variable" = 1,
"2 variable" = 2)),
selectInput(inputId = "x",
label = "Variable 1",
choices = names(df)),
conditionalPanel(
condition = "input.number == 2",
selectInput(inputId = "y",
label = "Variable 2",
choices = names(df))
)
),
column(8, plotOutput("plot"))
)
)
server <- function(input, output, session){
data <- reactive({
df
})
observeEvent(input$x,
{updateSelectInput(session,
inputId = "y",
label = "Variable 2",
choices = names(df)[names(df) != input$x])
})
output$plot <- renderPlot({
if(input$number == 1){
plot <- ggplot(data = data(), aes(x = .data[[input$x]])) + geom_density()
} else {
plot <- ggplot(data = data(),
aes(x = .data[[input$x]], y = .data[[input$y]])) +
geom_point()
}
plot
})
}
shinyApp(ui = ui, server = server)发布于 2021-07-12 06:53:55
你可以用aes_string。
另一个非常重要的问题是 in UI
df <- mtcars
library(ggplot2)
library(dplyr)
ui <- fluidPage(
h1("My first app",
style = 'color: green;
font-style: italic'),
hr(),
fluidRow(
sidebarPanel(
radioButtons(inputId = "number",
label = "Select number of variable",
choices = c("1 variable" = 1,
"2 variable" = 2)),
selectInput(inputId = "x",
label = "Variable 1",
choices = names(df)),
conditionalPanel(
condition = "input.number == 2",
selectInput(inputId = "y",
label = "Variable 2",
choices = names(df))
)
),
column(8, plotOutput("plot"))
),
hr()
# Never use output twice : the UI won't work!
#plotOutput("plot")
)
server <- function(input, output, session){
observeEvent(input$x,
{updateSelectInput(session,
inputId = "y",
label = "Variable 2",
choices = names(df)[names(df) != input$x])
})
data <- reactive({
if(input$number == 1){
data <- df %>%
select(input$x)
} else {
data <- df %>%
select(input$x, input$y)
}
})
output$plot <- renderPlot({
cat(input$x)
if(input$number == 1){
ggplot(data = data())+
geom_density(aes_string(x=input$x))
} else {
ggplot(data = data()) +
geom_point(aes_string(x=input$x,y=input$y))
}
})
}
shinyApp(ui = ui, server = server)https://stackoverflow.com/questions/68342780
复制相似问题