【问题标题】:Why am I unable to plot the graph on RShiny?为什么我无法在 R Shiny 上绘制图表?
【发布时间】:2021-08-28 00:46:40
【问题描述】:

我想创建一个 RShiny 情节。当我启动应用程序时,侧边栏可见但没有绘图。是什么导致了错误?

这是我的代码:

library(shiny)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput('xcol', 'X Variable', dimnames(dat)[[2]], 
                  selected = rownames(dat)),
      selectInput('ycol', 'Y Variable', dimnames(dat)[[2]],
                  selected = colnames(dat)),
      selectInput('color', 'Point Color', c("red", "orange", "yellow", "green", "blue", "violet", "purple"))
    ), 
    mainPanel(
      plotOutput('plot1')
    )
  )
)


server <- function(input, output) {
  
  selectedData <- reactive({
    dat[, c(input$xcol, input$ycol)]
  })
  output$plot<-renderPlot({
    plot(dat$axis(side=1,at=c(1:24),labels=dimnames(dat)[[2]],cex.axis=0.4,las=2), dat$axis(side=2))
  }
  )
}

shinyApp(ui = ui, server = server)

数据快照

dput(dat[1:2,1:2])
structure(list(cdc15_10 = c(-0.16, NA), cdc15_30 = c(0.09, NA
)), row.names = c("YAL001C", "YAL002W"), class = "data.frame")

【问题讨论】:

    标签: r shiny shinyapps shiny-reactivity shinyjs


    【解决方案1】:

    您可以尝试以下代码,该代码从下拉列表中选择包含两列的数据,并使用所选颜色绘制一列与另一列。

    library(shiny)
    
    ui <- fluidPage(
      sidebarLayout(
        sidebarPanel(
          selectInput('xcol', 'X Variable', dimnames(dat)[[2]], 
                      selected = rownames(dat)),
          selectInput('ycol', 'Y Variable', dimnames(dat)[[2]],
                      selected = colnames(dat)),
          selectInput('color', 'Point Color', c("red", "orange", "yellow", "green", "blue", "violet", "purple"))
        ), 
        mainPanel(
          plotOutput('plot')
        )
      )
    )
    
    
    server <- function(input, output) {
      
      selectedData <- reactive({
        dat[, c(input$xcol, input$ycol)]
      })
      output$plot<-renderPlot({
        dat <- selectedData()
        plot(dat[[1]], dat[[2]], col = input$color)
      }
      )
    }
    
    shinyApp(ui = ui, server = server)
    

    【讨论】:

      【解决方案2】:

      您的代码存在一些问题:

      1. 您没有在任何地方使用反应式selectedData 对象。
      2. 您有plotOutput('plot1'),但随后使用output$plot。这可能只是一个简单的错字。
      3. 通用的基础 R 绘图调用看起来很奇怪。我将从基本的plot(selectedData()) 开始,然后从那里开始添加轴标签等。

      这是一个最小的例子

      library(shiny)
      dat <- data.frame(a = rnorm(10), b = rnorm(10), c = rnorm(10))
      
      ui <- fluidPage(
          sidebarLayout(
              sidebarPanel(
                  selectInput(
                      'xcol', 
                      'X Variable', 
                      dimnames(dat)[[2]], 
                      selected = rownames(dat)),
                  selectInput(
                      'ycol', 
                      'Y Variable', 
                      dimnames(dat)[[2]],
                      selected = colnames(dat)),
                  selectInput(
                      'color', 
                      'Point Color', 
                      c("red", "orange", "yellow", "green", "blue", "violet", "purple"))
              ), 
              mainPanel(plotOutput('plot1'))
          )
      )
      
      
      server <- function(input, output) {
          
          selectedData <- reactive({
              dat[, c(input$xcol, input$ycol)]
          })
          
          output$plot1 <- renderPlot({
              plot(selectedData())
          })
      }
      
      shinyApp(ui = ui, server = server)
      

      产生

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-02-19
        • 2019-06-15
        • 2015-09-20
        • 2021-11-12
        • 2018-06-24
        • 2020-02-19
        • 2021-10-20
        • 1970-01-01
        相关资源
        最近更新 更多