我如何使滑块(例如,将其称为“分割mtcars wt”)弹出以显示选项b单选按钮,然后将x除以选项b的aes
(来自mtcars数据集的wt)以除以该值在下面的可复制示例中?然后,如果选择了选项单选按钮,请再次隐藏该滑块。我看过几个Stack溢出帖子,但它们没有帮助。我收到选项b的NULL输出错误。
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 to stop
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
从单选按钮输入中选择“ b”时,您可以使用conditionalPanel
显示/隐藏sliderInput
。
为了避免警告,您可以在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)