meenaparam
meenaparam

Reputation: 2019

How to use workspace objects in an R Shiny application

I would like a user to be able to type in the name of a dataframe object and have that object rendered as a formatted data table in a Shiny application.

Here is a toy example. There are two dataframe objects available in the workspace: df1 and df2. When the user types in df1, I would like that dataframe to be rendered. Likewise for df2 or for any other dataframe they have in their workspace.

I suspect I have to do something with environments or scoping or evaluation but I am not sure what.

I have commented in the code where I can hardcode in the built-in mtcars dataset and have that rendered correctly. Now I just want to be able to do the same for any ad-hoc dataframe in a user's workspace.

library(shiny)
set.seed(1234)

x <- sample.int(n = 20)
y <- sample(x = LETTERS, size = 20)
a <- rnorm(n = 20)
b <- sample(x = letters, size = 20)

df1 <- data.frame(x = x, y = y)
df2 <- data.frame(a = a, b = b)


# Define UI ----
ui <- fluidPage(
  titlePanel("Using text inputs to select dataframes"),

  sidebarLayout(position = "left",
                sidebarPanel(width = 5,
                            textInput("dfInput", h5("Enter name of dataframe"),
                                                value = "")),

                mainPanel(width = 6,
                          h4("Here's your data"),
                          textOutput("selected_df"),
                          dataTableOutput("view")
                )

  )
)


# Define server logic ----
server <- function(input, output, session) {

  output$selected_df <- renderText({
    paste("You have selected ", input$dfInput)
  })

  output$view <-
    renderDataTable({
      input$dfInput # this should render the selected dataframe. If you replace this with mtcars then that dataset is correctly rendered.
    })

}


# Run the app ----
shinyApp(ui = ui, server = server)

Upvotes: 1

Views: 520

Answers (1)

Pork Chop
Pork Chop

Reputation: 29397

We are going to get all the dataframes within the global enviriment first and then use get in order to access the object. I changed the textInput to selectInput so you dont need to type anything, potentially making a mistake. Moreover I added the data from datasets package however you should build more test cases to check if the data exists

library(shiny)
set.seed(1234)

x <- sample.int(n = 20)
y <- sample(x = LETTERS, size = 20)
a <- rnorm(n = 20)
b <- sample(x = letters, size = 20)

df1 <- data.frame(x = x, y = y)
df2 <- data.frame(a = a, b = b)

mydataframes <- names(which(unlist(eapply(.GlobalEnv,is.data.frame))))
OpenData <- data()$results[,3]

#Define UI ----
ui <- fluidPage(
  titlePanel("Using text inputs to select dataframes"),

  sidebarLayout(position = "left",
                sidebarPanel(width = 5,
                             selectInput("dfInput","Select Dataframe",
                               #choices = mydataframes,

                               list("Your Datasets" = c(mydataframes),
                                    "R Datasets" = c(OpenData),


                               selected=NULL))),

                mainPanel(width = 6,
                          h4("Here's your data"),
                          textOutput("selected_df"),
                          dataTableOutput("view")
                )

  )
)


# Define server logic ----
server <- function(input, output, session) {

  output$selected_df <- renderText({
    paste("You have selected ", input$dfInput)
  })

  output$view <-
    renderDataTable({
      as.data.frame(get(input$dfInput)) # this should render the selected dataframe. If you replace this with mtcars then that dataset is correctly rendered.
    })

}


# Run the app ----
shinyApp(ui = ui, server = server)

enter image description here

Upvotes: 2

Related Questions