【问题标题】:How to use workspace objects in an R Shiny application如何在 R Shiny 应用程序中使用工作区对象
【发布时间】:2018-10-03 16:00:09
【问题描述】:

我希望用户能够输入数据框对象的名称,并将该对象呈现为 Shiny 应用程序中的格式化数据表。

这是一个玩具示例。工作区中有两个可用的数据框对象:df1df2。当用户输入df1 时,我希望呈现该数据框。对于df2 或他们在工作区中拥有的任何其他数据框也是如此。

我怀疑我必须对环境、范围界定或评估做一些事情,但我不确定是什么。

我在代码中注释了我可以在内置mtcars 数据集中进行硬编码并正确呈现的代码。现在我只想能够对用户工作区中的任何临时数据框执行相同的操作。

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)

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    我们将首先获取全局环境中的所有数据帧,然后使用get 来访问对象。我将textInput 更改为selectInput,因此您无需输入任何内容,可能会出错。此外,我添加了来自 datasets 包的数据,但是您应该构建更多测试用例来检查数据是否存在

    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)
    

    【讨论】:

    • 这很完美,正是我想要的,谢谢!
    猜你喜欢
    • 1970-01-01
    • 2021-02-19
    • 1970-01-01
    • 2019-09-14
    • 2017-02-22
    • 1970-01-01
    • 2017-08-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多