gpt4 book ai didi

r - 满足条件时显示图

转载 作者:行者123 更新时间:2023-12-04 12:12:07 24 4
gpt4 key购买 nike

是否可以仅在满足条件时显示绘图并在满足条件时隐藏其他绘图?

我尝试与 conditionalPanel() 合作.如果满足条件,它可以工作并显示图,但它也显示其他两个图。我要显示coolPlotcoolPlot_2只有当所选输入是

"X1_P"   "X2_S"   "X3_W"   "X4_S"   "X5_P"   "X6_P"   "X7_P"  
"X8_S" "X9_P" "X10_P" "X11_P" "X12_I"

coolPlot_3只有在 "X13_K" 时才必须显示被选中,其他两个面板应该被隐藏。
我使用了以下代码。
    ui = fluidPage(
titlePanel("Data Visualization"),
sidebarLayout(
sidebarPanel(
uiOutput("variableOutput"),
uiOutput("text1Output")
),
mainPanel(
conditionalPanel(
condition = "input.variableOutput != 'X13_K'",
plotOutput("coolPlot")),
br(),
br(),
conditionalPanel(
condition = "input.variableOutput != 'X13_K'",
plotOutput("coolPlot_2")),
conditionalPanel(
condition = "input.variableOutput == 'X13_K'",
plotOutput("coolPlot_3")),
br(),
br(),
dataTableOutput(
"coolTable"
)
)
)
)

正如 Nice 所建议的那样,我还发布了服务器代码。
server = function(input,output){
output$variableOutput = renderUI({
selectInput("VariableInput",
"Variable auswählen",
choices = colnames(training)[-1] ,
selected = 2)


})

plot_data = reactive({frame = as.data.frame(cbind(training[,"flag"],
training[,input$VariableInput]))

frame[,1] = as.logical(frame[,1])

return(frame)

})

mean_data = reactive({data.frame(flag = c("2","1"),
data = c(weighted.mean(training[,input$VariableInput][training$flag==1],
training$weight[training$flag==1]),
weighted.mean(training[,input$VariableInput][training$flag==0],
training$weight[training$flag==0])))
})
output$coolPlot_3 = renderPlot({

if (is.null(input$VariableInput)) {
return(NULL)
}

numb_classes = length(levels(training[,input$VariableInput]))

row_names = levels(training[,input$VariableInput])

plot_data_2 = data.frame(klassen = character(numb_classes),
index = numeric(numb_classes),
stringsAsFactors = FALSE)

for (j in 1:numb_classes){

count_class_non_goal = count(subset(training[,input$VariableInput],
training[,input$VariableInput] == row_names[j] & training[,"flag"] == FALSE))

count_all_non_goal = count(training[training$flag == FALSE,input$VariableInput])

percent_non_goal = count_class_non_goal[,2]/(sum(count_all_non_goal[,2])/100)

count_class_goal = count(subset(training[,input$VariableInput],
training[,input$VariableInput] == row_names[j] & training[,"flag"] == TRUE))

count_all_goal = count(training[training$flag == TRUE,input$VariableInput])

percent_goal = count_class_goal[,2]/(sum(count_all_goal[,2])/100)

plot_data_2[,1][j] = row_names[j]

plot_data_2[,2][j] = round((percent_goal/percent_non_goal*100)-100,
digits =2)
}

ggplot(data = plot_data_2) +
geom_bar(aes(y = index,
x = klassen),
stat= "identity")+
coord_flip()+
theme(legend.position = "none",
axis.title.x = element_text(size=15,
face = "bold"),
axis.text.y = element_text(size=12),
axis.text.x = element_text(size=12),
axis.title.y = element_text(size=15,
face = "bold"))+
labs(x = paste(input$VariableInput),
y = "Index")
})

output$coolPlot = renderPlot({
if (is.null(input$VariableInput)) {
return(NULL)
}

ggplot(data = plot_data()) +
geom_boxplot(aes(x=V1,
y=V2,
fill=V1),
outlier.shape = NA) +
guides(fill=FALSE)+
scale_x_discrete(labels=c("Nicht-Ziel", "Ziel")) +
coord_cartesian(ylim = c(min(plot_data()[,"V2"]),
quantile(plot_data()[,"V2"])[4] + IQR(plot_data()[,"V2"],
na.rm = TRUE, type = 7)*1.9)) +
stat_boxplot(aes(x=V1,
y=V2,
fill=V1),
geom ='errorbar') +
theme(axis.title.x = element_text(size=15,
face = "bold"),
axis.text.y = element_text(size=12),
axis.text.x = element_text(size=12),
axis.title.y = element_text(size=15,
face = "bold"))+
labs(x = paste(input$VariableInput),
y = "Ausprägung der Variable")
})

output$coolPlot_2 = renderPlot({
if (is.null(input$VariableInput)) {
return(NULL)
}

ggplot(data = plot_data(),
aes(x=V2,
fill=V1)) + geom_density(alpha=.3)+
geom_vline(data=mean_data(),
aes(colour=flag,
xintercept=data),
linetype="dashed", size=1)+
scale_fill_discrete(name = "Gruppen",
labels=c("Nicht-Ziel",
"Ziel"))+
theme(axis.title.x = element_text(size=15,
face = "bold"),
axis.text.y = element_text(size=12),
axis.text.x = element_text(size=12),
axis.title.y = element_text(size=15,
face = "bold"))+
labs(x = paste(input$VariableInput),
y = "Density")


})



output$coolTable = renderDataTable({
training
})
}

最佳答案

问题是 server.r 之间的命名有些不匹配和 ui.r
server.r你声明

output$variableOutput = renderUI({
selectInput("VariableInput",
"Variable auswählen",
choices = colnames(training)[-1] ,
selected = 2)

这导致以下代码
<select id="VariableInput" ...>
<option value="X13_K"></option>
</select>

在 UI 代码中,您然后引用
uiOutput("variableOutput"),

用于输出控制。到目前为止,这是正确的,因为您将控件定义为 output$variableOutput .

然而,当你写 condition = "input.VariableOutput == 'X13_k'" 时,事情出错了。这应该是指定义为 VariableInput 的控件的 id在
 selectInput("VariableInput",
"Variable auswählen",
choices = colnames(training)[-1] ,
selected = 2)

所以一种解决方法是用作条件 condition = "input.VariableInput == 'X13_k'"
但是,我的建议是替换 VariableInputVariableOutput by VariableSelection` 或类似的东西。

具有最少修复的代码以供引用:
library(shiny)
library(ggplot2)
training=iris
names(training)[5] <- c("X13")

server = function(input,output){
output$variableOutput = renderUI({
selectInput("VariableInput",
"Variable auswählen",
choices = colnames(training)[-1] ,
selected = 2)


})

plot_data = reactive({frame = as.data.frame(cbind(training[,1],
training[,input$VariableInput]))

frame[,1] = as.logical(frame[,1])

return(frame)

})

mean_data = reactive({data.frame(flag = c("2","1"),
data = c(weighted.mean(training[,input$VariableInput][training$flag==1],
training$weight[training$flag==1]),
weighted.mean(training[,input$VariableInput][training$flag==0],
training$weight[training$flag==0])))
})
output$coolPlot_3 = renderPlot({

if (is.null(input$VariableInput)) {
return(NULL)
}

numb_classes = length(levels(training[,input$VariableInput]))

row_names = levels(training[,input$VariableInput])

plot_data_2 = data.frame(klassen = character(numb_classes),
index = numeric(numb_classes),
stringsAsFactors = FALSE)

for (j in 1:numb_classes){

count_class_non_goal = count(subset(training[,input$VariableInput],
training[,input$VariableInput] == row_names[j] & training[,"flag"] == FALSE))

count_all_non_goal = count(training[training$flag == FALSE,input$VariableInput])

percent_non_goal = count_class_non_goal[,2]/(sum(count_all_non_goal[,2])/100)

count_class_goal = count(subset(training[,input$VariableInput],
training[,input$VariableInput] == row_names[j] & training[,"flag"] == TRUE))

count_all_goal = count(training[training$flag == TRUE,input$VariableInput])

percent_goal = count_class_goal[,2]/(sum(count_all_goal[,2])/100)

plot_data_2[,1][j] = row_names[j]

plot_data_2[,2][j] = round((percent_goal/percent_non_goal*100)-100,
digits =2)
}

ggplot(data = plot_data_2) +
geom_bar(aes(y = index,
x = klassen),
stat= "identity")+
coord_flip()+
theme(legend.position = "none",
axis.title.x = element_text(size=15,
face = "bold"),
axis.text.y = element_text(size=12),
axis.text.x = element_text(size=12),
axis.title.y = element_text(size=15,
face = "bold"))+
labs(x = paste(input$VariableInput),
y = "Index")
})

output$coolPlot = renderPlot({
if (is.null(input$VariableInput)) {
return(NULL)
}

ggplot(data = plot_data()) +
geom_boxplot(aes(x=V1,
y=V2,
fill=V1),
outlier.shape = NA) +
guides(fill=FALSE)+
scale_x_discrete(labels=c("Nicht-Ziel", "Ziel")) +
coord_cartesian(ylim = c(min(plot_data()[,"V2"]),
quantile(plot_data()[,"V2"])[4] + IQR(plot_data()[,"V2"],
na.rm = TRUE, type = 7)*1.9)) +
stat_boxplot(aes(x=V1,
y=V2,
fill=V1),
geom ='errorbar') +
theme(axis.title.x = element_text(size=15,
face = "bold"),
axis.text.y = element_text(size=12),
axis.text.x = element_text(size=12),
axis.title.y = element_text(size=15,
face = "bold"))+
labs(x = paste(input$VariableInput),
y = "Ausprägung der Variable")
})

output$coolPlot_2 = renderPlot({
if (is.null(input$VariableInput)) {
return(NULL)
}

ggplot(data = plot_data(),
aes(x=V2,
fill=V1)) + geom_density(alpha=.3)+
geom_vline(data=mean_data(),
aes(colour=flag,
xintercept=data),
linetype="dashed", size=1)+
scale_fill_discrete(name = "Gruppen",
labels=c("Nicht-Ziel",
"Ziel"))+
theme(axis.title.x = element_text(size=15,
face = "bold"),
axis.text.y = element_text(size=12),
axis.text.x = element_text(size=12),
axis.title.y = element_text(size=15,
face = "bold"))+
labs(x = paste(input$VariableInput),
y = "Density")


})



output$coolTable = renderDataTable({
training
})
}

library(shiny)
ui = fluidPage(
titlePanel("Data Visualization"),
sidebarLayout(
sidebarPanel(
uiOutput("variableOutput"),
uiOutput("text1Output")
),
mainPanel(
conditionalPanel(
"input.VariableInput != 'X13'",
plotOutput("coolPlot")),
# textInput("bla","blub",input.variableOutput),
br(),
br(),
conditionalPanel(
condition = "input.VariableInput != 'X13'",
plotOutput("coolPlot_2")),
conditionalPanel(
condition = "input.VariableInput == 'X13'",
plotOutput("coolPlot_3")),
br(),
br(),
dataTableOutput(
"coolTable"
)
)
)
)

关于r - 满足条件时显示图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35542854/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com