gpt4 book ai didi

r - 根据 Shiny R 中的单选按钮显示 slider 输入

转载 作者:行者123 更新时间:2023-12-04 10:20:31 27 4
gpt4 key购买 nike

如何让 slider (例如命名为“Divide mtcars wt”)弹出“选项 b”单选按钮,并将 x 划分为 aes (这是来自 mtcars 数据集的 wt)“选项 b”与下面可重现示例中的 slider 值?然后,如果选择了“选项 a”单选按钮,请再次隐藏该 slider 。我查看了几个堆栈溢出帖子,但它们没有帮助。我不断收到NULL当我实现它时绘制“选项 b”的输出错误,所以我把它拿出来。

library(shiny)
library(ggplot2)
options(warn=-1)

#ui.r
ui <- pageWithSidebar(
headerPanel("many plots app"),

sidebarPanel(
sliderInput("width", "Plot Width (%)", min = 0, max = 100, value = 100),
sliderInput("height", "Plot Height (px)", min = 0, max = 800, value = 800),
uiOutput("filter_plot")

),

mainPanel(
uiOutput("plot")
)
)
#server.r
server <- function(input, output, session) {

options(warn = -1) #This doesn't stop console warning

output$filter_plot<-renderUI({
radioButtons("rd","Select Option",choices = c("a",
"b",
selected = "a"))
})

output$plot <- renderUI({
if(input$rd=="a"){
output$plot1<- renderPlot({
ggplot2::ggplot(mtcars, aes(mpg, cyl))+ geom_point()
})
plotOutput("plot1", width = paste0(input$width, "%"), height = input$height)
}
else if(input$rd=="b"){
output$plot2<- renderPlot({
ggplot2::ggplot(mtcars, aes(wt, qsec))+ geom_line()

})
plotOutput("plot2", width = paste0(input$width, "%"), height = input$height)
}
})
}
shinyApp(ui = ui, server = server)


先感谢您!

如果可以的话,还有一个好处,即使我隐藏了它们,为什么控制台中还会出现警告。我的应用程序有效,但警告存在:

#Warning: Error in if: argument is of length zero
# 96: renderUI [C:/Users/YOU/Documents/DIRECTORY/xyz.R#443]
# 95: func
# 82: origRenderFunc
# 81: output$plot
# 1: runApp

最佳答案

您可以使用 conditionalPanel显示/隐藏 sliderInput当从您的单选按钮输入中选择“b”时。

为了防止您的警告,您可以要求 req(input$rd)在您的 if 中使用它之前陈述。

让我知道这是否是您想到的行为。

library(shiny)
library(ggplot2)
options(warn=-1)

#ui.r
ui <- pageWithSidebar(
headerPanel("many plots app"),

sidebarPanel(
sliderInput("width", "Plot Width (%)", min = 0, max = 100, value = 100),
sliderInput("height", "Plot Height (px)", min = 0, max = 800, value = 800),
uiOutput("filter_plot"),
conditionalPanel(
"input.rd == 'b'",
sliderInput("sl_wt", "Divide Weight", min = 1, max = 10, value = 5)
)
),

mainPanel(
uiOutput("plot")
)
)
#server.r
server <- function(input, output, session) {

options(warn = -1) #This doesn't stop console warning to stop

output$filter_plot<-renderUI({
radioButtons("rd","Select Option",choices = c("a",
"b",
selected = "a"))
})

output$plot1<- renderPlot({
ggplot2::ggplot(mtcars, aes(mpg, cyl))+ geom_point()
})

output$plot2<- renderPlot({
ggplot2::ggplot(mtcars, aes(wt/input$sl_wt, qsec))+ geom_line()
})

output$plot <- renderUI({
req(input$rd)
if(input$rd=="a"){
plotOutput("plot1", width = paste0(input$width, "%"), height = input$height)
}
else if(input$rd=="b"){
plotOutput("plot2", width = paste0(input$width, "%"), height = input$height)
}
})
}
shinyApp(ui = ui, server = server)

关于r - 根据 Shiny R 中的单选按钮显示 slider 输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60878158/

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