我正在嘗試創建一個應用程式,您可以在其中創建箱線圖并在用戶選擇時添加 Kruskal-Wallis p 值。
該應用程式有 3 個標簽:
- Tab 1 > 它有一個
checkboxInput,如果你點擊它,你將進行 log2 轉換。此外,它還actionButton可以提交您的資料(有或沒有 log2)。如果不單擊該按鈕,將無法繪制繪圖。 - Tab 2 > it has
radioButtonsandcheckboxInput允許您根據用戶的選擇繪制不同的圖。 - 選項卡 3 > 它有一個
numericInput允許您更改繪圖的不透明度。此外,它還有一個checkboxInput允許您將 Kruskal-Wallis p 值添加到繪圖中。
它完美地作業。但是,當我想添加 KW p 值時,該值會在單擊actionButton.
這是當您選擇 2 個組并單擊checkboxInputfrom tab3 以顯示 KW pvalue 時的外觀。
![單擊 actionButton 后,如何將元素添加到繪圖中并查看更新?[閃亮的]](https://img.uj5u.com/2021/12/29/e3142e721afd46c69e5267a6baa72b13.png)
但是,如果您取消選擇組 1,為了僅看到組 3,則在單擊 之前 p 值的位置會發生變化actionButton。
![單擊 actionButton 后,如何將元素添加到繪圖中并查看更新?[閃亮的]](https://img.uj5u.com/2021/12/29/e20f9d8b18f142b59ff8195548e4b4ef.png)
然后,當您單擊該按鈕時,您將獲得您期望的最終輸出。
![單擊 actionButton 后,如何將元素添加到繪圖中并查看更新?[閃亮的]](https://img.uj5u.com/2021/12/29/1a223d197c324751b9e0e32789d5ae1d.png)
另一方面,如果用戶決定更改 p 值的位置(通過numericInput它們在單擊“顯示 Kruskal Wallis p 值”后出現的s),則繪圖更新而無需單擊actionButton.
總而言之,問題是情節在單擊之前更新actionButton,我不知道如何解決。
請注意,如果您更改繪圖的不透明度,除非您單擊actionButton(我想要的所有應用程式),否則繪圖不會更改。
有誰知道如何修理它?
提前致謝
代碼:
library(shiny)
library(dplyr)
library(ggplot2)
ui <- fluidPage(
titlePanel("My app"),
sidebarLayout(
sidebarPanel(
tabsetPanel(
tabPanel("Tab1",
checkboxInput("log2", "Log2 transformation", value = FALSE),
actionButton("submit", "Submit")
),
tabPanel("Tab2",
radioButtons(inputId = "plot_type", label = "I want to see the plot of:",
c("All the samples" = "all_samples",
"Groups" = "samples_group")),
conditionalPanel(
condition = "input.plot_type == 'samples_group'",
style = "margin-left: 20px;",
checkboxGroupInput("group", "Choose the group:",
choices = c("Group1", "Group2", "Group3"))),
actionButton("show_plot", "See the plot")
),
tabPanel("Tab3",
numericInput("alpha", "Opacity of the plot", value=0.2),
checkboxInput(inputId = "Kruskalpval", label = "Show the Kruskal Wallis p-value", value = FALSE),
conditionalPanel(
condition = "input.Kruskalpval == '1'",
style = "margin-left: 20px;",
checkboxInput(inputId = "changeKW", "I want to change the place of the value", value=FALSE),
conditionalPanel(
condition = "input.changeKW == '1'",
numericInput(inputId = "X_axis", "X_axis:", value=2),
numericInput(inputId = "Y_axis", "Y_axis:", value=70)
)
),
actionButton("show_plot_2", "See the plot")
)
)
),
mainPanel(
plotOutput("boxplots")
)
)
)
server <- function(input, output) {
set.seed(1234)
Gene <- floor(runif(25, min=0, max=101))
groups_age <- floor(runif(25, min=18, max=75))
Group <- c("Group1", "Group1", "Group3", "Group2", "Group1", "Group3", "Group2", "Group2", "Group2", "Group1", "Group1", "Group3", "Group1", "Group2", "Group1", "Group2", "Group3", "Group1", "Group3", "Group3", "Group2", "Group1", "Group3", "Group3","Group2")
data <- reactive({
df <- data.frame(Gene, Group, groups_age)
mybreaks <- seq(min(df$groups_age)-1, to=max(df$groups_age) 10, by=10)
df$groups_age <- cut(df$groups_age, breaks = mybreaks, by=10)
if(input$plot_type == "samples_group"){
# if the user selects everything, it will take everything.
if(all(c("Group1", "Group2", "Group3") %in% input$group)){
return(df)
# if the user only selects group1 and group2, it will appear only those columns.
}else if (all(c("Group1", "Group2") %in% input$group)) {
df <- subset(df, (df$Group == "Group1" | df$Group == "Group2"))
return(df)
# if the user only selects group1 and group3, it will appear only those columns.
}else if (all(c("Group1", "Group3") %in% input$group)) {
df <- subset(df, (df$Group == "Group1" | df$Group == "Group3"))
return(df)
# if the user only selects Group2 and Group3, it will appear only those columns.
}else if (all(c("Group2", "Group3") %in% input$group)) {
df <- subset(df, (df$Group == "Group2" | df$Group == "Group3"))
return(df)
# if the user only selects Group1
} else if ("Group1" %in% input$group) {
df <- subset(df, (df$Group == "Group1"))
return(df)
# if the user only selects group2
} else if ("Group2" %in% input$group) {
df <- subset(df, (df$Group == "Group2"))
return(df)
# if the user only selects group3
} else if ("Group3" %in% input$group) {
df <- subset(df, (df$Group == "Group3"))
return(df)
# if the user doesn't select anything.
} else {
return(df)
}
}else{
df$Group <- NULL
return(df)
}
})
mydata <- reactive({
req(input$submit)
if(input$log2 == TRUE){
data <- data()
cols <- sapply(data, is.numeric)
data[cols] <- lapply(data[cols], function(x) log2(x 1))
}
else{
data <- data()
}
return(data)
})
draw_bp <- reactive({
if(ncol(mydata())==2){
bp <- ggplot(mydata(), aes(x=groups_age, y=Gene))
geom_boxplot(aes(fill=groups_age), alpha = input$alpha)
labs(fill = "groups_age")
if((input$Kruskalpval == "TRUE") && (input$changeKW==FALSE)){
pval <- mydata() %>%
summarize(Kruskal_pvalue = kruskal.test(Gene ~ groups_age)$p.value)
bp <- bp geom_text(data=pval, aes(x=2, y=max(mydata()$Gene)-10, label=paste0("Kruskal-Wallis\n p = ",Kruskal_pvalue)))
}
if((input$Kruskalpval == "TRUE") && (input$changeKW==TRUE)){
pval <- mydata() %>%
summarize(Kruskal_pvalue = kruskal.test(Gene ~ groups_age)$p.value)
bp <- bp geom_text(data=pval, aes(x=input$X_axis, y=input$Y_axis, label=paste0("Kruskal-Wallis\n p = ",Kruskal_pvalue)))
}
return(bp)
}
else{
bp <- ggplot(mydata(), aes(x=groups_age, y=Gene))
geom_boxplot(aes(fill=groups_age), alpha=input$alpha)
facet_grid(. ~ Group)
if((input$Kruskalpval == "TRUE") && (input$changeKW==FALSE)){
pval <- mydata() %>%
group_by(Group) %>%
summarize(Kruskal_pvalue = kruskal.test(Gene ~ groups_age)$p.value)
bp <- bp geom_text(data=pval, aes(x=2, y=max(mydata()$Gene)-10, label=paste0("Kruskal-Wallis\n p = ",Kruskal_pvalue)))
}
if((input$Kruskalpval == "TRUE") && (input$changeKW==TRUE)){
pval <- mydata() %>%
group_by(Group) %>%
summarize(Kruskal_pvalue = kruskal.test(Gene ~ groups_age)$p.value)
bp <- bp geom_text(data=pval, aes(x=input$X_axis, y=input$Y_axis, label=paste0("Kruskal-Wallis\n p = ",Kruskal_pvalue)))
}
return(bp)
}
})
v <- reactiveValues()
observeEvent(input$show_plot | input$show_plot_2, {
v$plot <- draw_bp()
})
output$boxplots <- renderPlot({
req(input$submit)
if (is.null(v$plot)) return()
v$plot
})
}
shinyApp(ui = ui, server = server)
uj5u.com熱心網友回復:
您需要isolate()用于數字輸入,以便它們在不單擊 actionButton 的情況下不會更新 KW 的位置。此外,不需要observeEvent(). 試試這個
draw_bp <- eventReactive(c(input$show_plot, input$show_plot_2), {
if(ncol(mydata())==2){
bp <- ggplot(mydata(), aes(x=groups_age, y=Gene))
geom_boxplot(aes(fill=groups_age), alpha = input$alpha)
labs(fill = "groups_age")
if((input$Kruskalpval == "TRUE") && (input$changeKW==FALSE)){
pval <- mydata() %>%
summarize(Kruskal_pvalue = kruskal.test(Gene ~ groups_age)$p.value)
bp <- bp geom_text(data=pval, aes(x=2, y=max(mydata()$Gene)-10, label=paste0("Kruskal-Wallis\n p = ",Kruskal_pvalue)))
}
if((input$Kruskalpval == "TRUE") && (input$changeKW==TRUE)){
pval <- mydata() %>%
summarize(Kruskal_pvalue = kruskal.test(Gene ~ groups_age)$p.value)
bp <- bp geom_text(data=pval, aes(x=isolate(input$X_axis), y=isolate(input$Y_axis), label=paste0("Kruskal-Wallis\n p = ",Kruskal_pvalue)))
}
return(bp)
}
else{
bp <- ggplot(mydata(), aes(x=groups_age, y=Gene))
geom_boxplot(aes(fill=groups_age), alpha=input$alpha)
facet_grid(. ~ Group)
if((input$Kruskalpval == "TRUE") && (input$changeKW==FALSE)){
pval <- mydata() %>%
group_by(Group) %>%
summarize(Kruskal_pvalue = kruskal.test(Gene ~ groups_age)$p.value)
bp <- bp geom_text(data=pval, aes(x=2, y=max(mydata()$Gene)-10, label=paste0("Kruskal-Wallis\n p = ",Kruskal_pvalue)))
}
if((input$Kruskalpval == "TRUE") && (input$changeKW==TRUE)){
pval <- mydata() %>%
group_by(Group) %>%
summarize(Kruskal_pvalue = kruskal.test(Gene ~ groups_age)$p.value)
bp <- bp geom_text(data=pval, aes(x=isolate(input$X_axis), y=isolate(input$Y_axis), label=paste0("Kruskal-Wallis\n p = ",Kruskal_pvalue)))
}
return(bp)
}
})
v <- reactiveValues()
observeEvent(input$show_plot | input$show_plot_2, {
v$plot <- draw_bp()
})
output$boxplots <- renderPlot({
req(input$submit)
# if (is.null(v$plot)) return()
# v$plot
draw_bp()
})
uj5u.com熱心網友回復:
正如@YBS 在評論中建議的那樣,我們可以eventReactive在創建反應式時使用data:
data <- eventReactive(c(input$show_plot, input$show_plot_2), {.....})
完整代碼:
library(shiny)
library(dplyr)
library(ggplot2)
ui <- fluidPage(
titlePanel("My app"),
sidebarLayout(
sidebarPanel(
tabsetPanel(
tabPanel(
"Tab1",
checkboxInput("log2", "Log2 transformation", value = FALSE),
actionButton("submit", "Submit")
),
tabPanel(
"Tab2",
radioButtons(
inputId = "plot_type", label = "I want to see the plot of:",
c(
"All the samples" = "all_samples",
"Groups" = "samples_group"
)
),
conditionalPanel(
condition = "input.plot_type == 'samples_group'",
style = "margin-left: 20px;",
checkboxGroupInput("group", "Choose the group:",
choices = c("Group1", "Group2", "Group3")
)
),
actionButton("show_plot", "See the plot")
),
tabPanel(
"Tab3",
numericInput("alpha", "Opacity of the plot", value = 0.2),
checkboxInput(inputId = "Kruskalpval", label = "Show the Kruskal Wallis p-value", value = FALSE),
conditionalPanel(
condition = "input.Kruskalpval == '1'",
style = "margin-left: 20px;",
checkboxInput(inputId = "changeKW", "I want to change the place of the value", value = FALSE),
conditionalPanel(
condition = "input.changeKW == '1'",
numericInput(inputId = "X_axis", "X_axis:", value = 2),
numericInput(inputId = "Y_axis", "Y_axis:", value = 70)
)
),
actionButton("show_plot_2", "See the plot")
)
)
),
mainPanel(
plotOutput("boxplots")
)
)
)
server <- function(input, output) {
set.seed(1234)
Gene <- floor(runif(25, min=0, max=101))
groups_age <- floor(runif(25, min=18, max=75))
Group <- c("Group1", "Group1", "Group3", "Group2", "Group1", "Group3", "Group2", "Group2", "Group2", "Group1", "Group1", "Group3", "Group1", "Group2", "Group1", "Group2", "Group3", "Group1", "Group3", "Group3", "Group2", "Group1", "Group3", "Group3","Group2")
data <- eventReactive(c(input$show_plot, input$show_plot_2), {
df <- data.frame(Gene, Group, groups_age)
mybreaks <- seq(min(df$groups_age)-1, to=max(df$groups_age) 10, by=10)
df$groups_age <- cut(df$groups_age, breaks = mybreaks, by=10)
if(input$plot_type == "samples_group"){
# if the user selects everything, it will take everything.
if(all(c("Group1", "Group2", "Group3") %in% input$group)){
return(df)
# if the user only selects group1 and group2, it will appear only those columns.
}else if (all(c("Group1", "Group2") %in% input$group)) {
df <- subset(df, (df$Group == "Group1" | df$Group == "Group2"))
return(df)
# if the user only selects group1 and group3, it will appear only those columns.
}else if (all(c("Group1", "Group3") %in% input$group)) {
df <- subset(df, (df$Group == "Group1" | df$Group == "Group3"))
return(df)
# if the user only selects Group2 and Group3, it will appear only those columns.
}else if (all(c("Group2", "Group3") %in% input$group)) {
df <- subset(df, (df$Group == "Group2" | df$Group == "Group3"))
return(df)
# if the user only selects Group1
} else if ("Group1" %in% input$group) {
df <- subset(df, (df$Group == "Group1"))
return(df)
# if the user only selects group2
} else if ("Group2" %in% input$group) {
df <- subset(df, (df$Group == "Group2"))
return(df)
# if the user only selects group3
} else if ("Group3" %in% input$group) {
df <- subset(df, (df$Group == "Group3"))
return(df)
# if the user doesn't select anything.
} else {
return(df)
}
}else{
df$Group <- NULL
return(df)
}
})
mydata <- reactive({
req(input$submit)
if(input$log2 == TRUE){
data <- data()
cols <- sapply(data, is.numeric)
data[cols] <- lapply(data[cols], function(x) log2(x 1))
}
else{
data <- data()
}
return(data)
})
draw_bp <- reactive({
if(ncol(mydata())==2){
bp <- ggplot(mydata(), aes(x=groups_age, y=Gene))
geom_boxplot(aes(fill=groups_age), alpha = input$alpha)
labs(fill = "groups_age")
if((input$Kruskalpval == "TRUE") && (input$changeKW==FALSE)){
pval <- mydata() %>%
summarize(Kruskal_pvalue = kruskal.test(Gene ~ groups_age)$p.value)
bp <- bp geom_text(data=pval, aes(x=2, y=max(mydata()$Gene)-10, label=paste0("Kruskal-Wallis\n p = ",Kruskal_pvalue)))
}
if((input$Kruskalpval == "TRUE") && (input$changeKW==TRUE)){
pval <- mydata() %>%
summarize(Kruskal_pvalue = kruskal.test(Gene ~ groups_age)$p.value)
bp <- bp geom_text(data=pval, aes(x=input$X_axis, y=input$Y_axis, label=paste0("Kruskal-Wallis\n p = ",Kruskal_pvalue)))
}
return(bp)
}
else{
bp <- ggplot(mydata(), aes(x=groups_age, y=Gene))
geom_boxplot(aes(fill=groups_age), alpha=input$alpha)
facet_grid(. ~ Group)
if((input$Kruskalpval == "TRUE") && (input$changeKW==FALSE)){
pval <- mydata() %>%
group_by(Group) %>%
summarize(Kruskal_pvalue = kruskal.test(Gene ~ groups_age)$p.value)
bp <- bp geom_text(data=pval, aes(x=2, y=max(mydata()$Gene)-10, label=paste0("Kruskal-Wallis\n p = ",Kruskal_pvalue)))
}
if((input$Kruskalpval == "TRUE") && (input$changeKW==TRUE)){
pval <- mydata() %>%
group_by(Group) %>%
summarize(Kruskal_pvalue = kruskal.test(Gene ~ groups_age)$p.value)
bp <- bp geom_text(data=pval, aes(x=input$X_axis, y=input$Y_axis, label=paste0("Kruskal-Wallis\n p = ",Kruskal_pvalue)))
}
return(bp)
}
})
v <- reactiveValues()
observeEvent(input$show_plot | input$show_plot_2, {
v$plot <- draw_bp()
})
output$boxplots <- renderPlot({
req(input$submit)
if (is.null(v$plot)) return()
v$plot
})
}
shinyApp(ui = ui, server = server)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/396754.html
