【问题标题】:R Shiny: Switch between table and plot in renderUIR Shiny:在 renderUI 中的表格和绘图之间切换
【发布时间】:2020-03-22 23:10:23
【问题描述】:

我试图让用户选择他想要的显示类型,但是当我尝试渲染绘图时,它给了我一个错误。

代码如下:

library(shiny)
library(DT)
library(data.table)

runApp(list(
    ui = fluidPage(
        wellPanel(
            radioButtons("visuBtn", NULL, choices = c(Table = "table", Plot = "plot"))
        ),
        wellPanel(
            uiOutput("DataTable")
        )
    ),
    server = function(input, output){

        observeEvent(input$visuBtn,{
            output$DataTable <- renderUI({
                dfconc <- data.table(time = c(1,2,3,4,5), concentration = c(0.1, 0.4, 0.5, 0.7, 0.8))

                if(input$visuBtn == "table"){
                    output$aa <- renderDataTable(dfconc, options = list(paging = FALSE, searching = FALSE))
                    dataTableOutput("aa")
                }
                else { ### NOT WORKING
                    output$aa <- renderPlot({
                        plot(dfconc$time, dfconc$concentration, xlab = "Time", ylab = "Concentration")
                    })
                    fixedRow(
                        plotOutput("aa")
                    )
                }      ###

            })
        })
    }
))


感谢您的帮助

【问题讨论】:

  • 检查 here 是否有此错误

标签: r shiny uioutput


【解决方案1】:

我认为最好在客户端渲染输出,然后根据选择简单地 showhide 元素。这样您就不会在 server 方面浪费资源

library(shiny)
library(shinyjs)
library(DT)
library(data.table)

runApp(list(
  ui = fluidPage(
    useShinyjs(),
    wellPanel(
      radioButtons("visuBtn", NULL, choices = c(Table = "table", Plot = "plot"))
    ),
    wellPanel(
      dataTableOutput("mytable"),
      plotOutput("myplot")
    )
  ),
  server = function(input, output, session){

    dfconc <- data.table(time = c(1,2,3,4,5), concentration = c(0.1, 0.4, 0.5, 0.7, 0.8))

    output$mytable <- renderDataTable(
      dfconc, options = list(paging = FALSE, searching = FALSE)
    )

    output$myplot <- renderPlot({
      plot(dfconc$time, dfconc$concentration, xlab = "Time", ylab = "Concentration")
    })

    observeEvent(input$visuBtn,{
      req(input$visuBtn)
      if(input$visuBtn == "plot"){
        hide("mytable")
        show("myplot")
      }else{
        hide("myplot")
        show("mytable")
      }
    })
  }
))

【讨论】:

  • 谢谢你的回答,我会用这个。但我想知道为什么我的版本不起作用。
  • 您的输出具有相同的 ID output$aa,每个输出只能有 1 个唯一 ID
  • 亲爱的@PorkChop 你能帮忙解决这个问题吗stackoverflow.com/questions/59129562/…
【解决方案2】:

同意@Pork Chop。但是为什么不简单地使用conditionalPanel

library(shiny)
library(DT)
library(data.table)

runApp(list(
  ui = fluidPage(
    wellPanel(
      radioButtons("visuBtn", NULL, choices = c(Table = "table", Plot = "plot"))
    ),

      conditionalPanel(
        condition = "input.visuBtn == 'table'",
        DTOutput('aa')
      ),
      conditionalPanel(
        condition = "input.visuBtn == 'plot'",
        plotOutput('bb')
      )

  ),
  server = function(input, output){
    dfconc <- data.table(time = c(1,2,3,4,5), concentration = c(0.1, 0.4, 0.5, 0.7, 0.8))

          output$aa <- renderDT(dfconc, options = list(paging = FALSE, searching = FALSE))
          output$bb <- renderPlot({
            plot(dfconc$time, dfconc$concentration, xlab = "Time", ylab = "Concentration")
          })
        } 

))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-14
    • 2020-01-13
    • 2017-10-26
    • 2020-08-12
    • 1970-01-01
    • 1970-01-01
    • 2021-03-08
    相关资源
    最近更新 更多