【问题标题】:R Shiny: PlotOutput not updating in Shiny appR Shiny:PlotOutput 未在 Shiny 应用程序中更新
【发布时间】:2021-08-06 02:03:30
【问题描述】:

我正在尝试制作一个具有以下功能的简单闪亮应用:

2 或 3 个输入:

  • input1:一个选择输入
  • input2:一个 selectInput,以 input1 为条件
  • input3:一个数值

输出是一个 ggplot,其中的标签随输入而变化。

我还想要一个默认绘图和一个重置按钮。

我制作了 3 个文件:ui.R、server.R 和 function.R。最后一个是制作情节的那个。

当 input1 取“A”或“B”的值时,我得到了所需的输出。 “重置”按钮似乎也可以正常工作。但是,当我在输入 1 中选择“C”时,它会将我带回到默认绘图:带有“Nothing”标签的绘图,而不是“C1 10”之类的绘图。

我检查了很多次代码,但我无法找到问题所在。

这是我的文件的代码:

# file ui.R

library(shiny)

shinyUI(fluidPage(
    
    plotOutput("printsomething"),
    
    selectInput(
        inputId="input1",
        label="input1",
        choices=c("A","B","C"),
        selected = NULL,
        multiple = FALSE,
        selectize = TRUE,
        width = NULL,
        size = NULL),
    
    conditionalPanel(
        condition = "input.input1 == 'A'",
        selectInput("input2", "input2",
                    list("A1", "A2"))),
    
    conditionalPanel(
        condition = "input.input1 == 'C'",
        selectInput("input2", "input2",
                    list("C1", "C2"))),
    
    numericInput("num","Number",value=0,min=0),
    
    actionButton("Run","Run"),
    actionButton("Reset","Reset")

    )
)
# file server.R

source("function.R")

library(shiny)

shinyServer(function(input, output) {
  
  graph <- reactiveValues(data = NULL)
  
  observeEvent(input$Run, {
    
    graph$data <- printsomething(data,input$input1,input$input2,input$num)

  })
  
  observeEvent(input$Reset, {
    graph$data <- printsomething("Nothing","NA","NA","NA")
  })  
  
  output$printsomething <- renderPlot({
    if (is.null(graph$data)) return(printsomething(data,"NA","NA","NA"))
    graph$data
  })
  
  })
    
# file function.R

library(ggplot2)

data <- "Nothing"

printsomething <- function(data,input1=NA,input2=NA,num=0) {
  
  if(is.na(input1)) {
    
    data <- "Nothing"
    
  } else if(input1=="A") {
    
    if(input2=="A1") {
      data <- paste("A1",num)
    } else if(input2=="A2") {
      data <- paste("A2",num)
    }
    
  } else if(input1=="B") {
    
    data <- paste("B",num)
    
  } else if(input1=="C") {
    
    if(input2=="C1") {
      data <- paste("C1",num)
    } else if(input2=="C2") {
      data <- paste("C2",num)
    }
    
  }
  
  ggplot() +
    geom_label(aes(x=1,y=1,label=data))
}

如果有人可以帮助我,我将不胜感激。我是 Shiny 的新手。

谢谢。

【问题讨论】:

  • 如果将第二个条件的inputId改为selectInput("input3", "input2", list("C1", "C2"))),使其唯一,并在服务器端进行适当的更改,就可以正常工作了。
  • 感谢您的回答!!

标签: r ggplot2 shiny


【解决方案1】:

如评论中所述,唯一的inputId 将使其工作。服务端小幅调整,printsomething功能不变。试试这个

library(shiny)

ui <- shinyUI(fluidPage(

  plotOutput("printsomething"),

  selectInput(
    inputId="input1",
    label="input1",
    choices=c("A","B","C"),
    selected = NULL,
    multiple = FALSE,
    selectize = TRUE,
    width = NULL,
    size = NULL),

  conditionalPanel(
    condition = "input.input1 == 'A'",
    selectInput("input2", "input2", list("A1", "A2"))),

  conditionalPanel(
    condition = "input.input1 == 'C'",
    selectInput("input3", "input2", list("C1", "C2"))),

  numericInput("num","Number",value=0,min=0),

  actionButton("Run","Run"),
  actionButton("Reset","Reset")

))

server <- shinyServer(function(input, output) {

  graph <- reactiveValues(data = NULL)

  observeEvent(input$Run, {

    if (input$input1=="A"){ input2 = input$input2
    }else if (input$input1=="C") input2 = input$input3

    graph$data <- printsomething(data,input$input1,input2,input$num)

  })

  observeEvent(input$Reset, {
    graph$data <- printsomething("Nothing","NA","NA","NA")
  })

  output$printsomething <- renderPlot({
    if (is.null(graph$data)) return(printsomething(data,"NA","NA","NA"))
    graph$data
  })

})

shinyApp(ui, server)

【讨论】:

    猜你喜欢
    • 2022-01-15
    • 2020-06-26
    • 2020-04-23
    • 1970-01-01
    • 1970-01-01
    • 2017-02-22
    • 2019-03-04
    • 2021-10-21
    • 2021-02-05
    相关资源
    最近更新 更多