Reputation: 7938
Suppose I have a function that takes an input an returns a data frame with two values. I want to render the following text: Value 1 is df1$value1
, and value 2 is df1$value2
. Is there a way of doing this?
This is the code that I have right now:
## 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)
Thanks!
Upvotes: 0
Views: 978
Reputation: 330093
You can prepare output inside server function like this:
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]))
}
)
})
}
and then bind in inside ui function using uiOutput('df1')
.
Alternatively you can use observe
block and assign specific values to output in the loop:
observe({
df <- my.func(input$slider)
for (i in 1:ncol(df)) {
output[[paste0('value', i)]] <- renderText({ df[, i] })
}
})
Then you can simply use textOutput
in the ui like this textOutput('value1')
.
Upvotes: 1