【问题标题】:evaluate function in shiny and print values在闪亮和打印值中评估函数
【发布时间】:2015-05-31 13:11:57
【问题描述】:

假设我有一个函数,它接受一个输入并返回一个具有两个值的数据框。我想呈现以下文本:值 1 是 df1$value1,值 2 是 df1$value2。有没有办法做到这一点?

这是我现在拥有的代码:

## app.R ##
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(title = "Basic dashboard"),
  dashboardSidebar(),
  dashboardBody(
    # Boxes need to be put in a row (or column)
    fluidRow(
      box(renderText(paste('Value 1 is', "df1$value1"))),

      box(
        title = "Controls",
        sliderInput("slider", "X:", 1, 100, 50)
      )
    )
  )
)

my.func <- function(X){
  df1 = data.frame(value1=X*X, value2=sqrt(X))
  return(df1)
}

server <- function(input, output) {

  output$df1 <- renderTable({
    my.func(input$slider)
  })
}

shinyApp(ui, server)

谢谢!

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    您可以像这样在服务器函数中准备输出:

    server <- function(input, output) {
        output$df1 <- renderUI({
            df <- my.func(input$slider)
            lapply(
                1:ncol(df),
                function(i) {
                    force(i)
                    p(paste("Value", i, "is", df[, i]))
                }
            )
        })
    }
    

    然后使用uiOutput('df1')绑定到内部的ui函数中。

    或者,您可以使用observe 块并在循环中为输出分配特定值:

    observe({
        df <- my.func(input$slider)
        for (i in 1:ncol(df)) {
            output[[paste0('value', i)]] <- renderText({ df[, i] }) 
        }
    })
    

    然后你可以像textOutput('value1')这样在ui中简单地使用textOutput

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-04
      • 1970-01-01
      • 1970-01-01
      • 2017-05-18
      • 2015-01-22
      • 2021-05-06
      • 1970-01-01
      相关资源
      最近更新 更多