I am unable to read the dataset of a package and display the content of it, i am trying to display the dataset content from 'dataset2' output.
I am using the libraries as mentioned in the code. I am able to see the content of the 'dataset1', only issue is with contents of 'dataset2'. Could you please help me how to resolve it
UI Part
library(shiny)
library(magrittr)
library(tidyverse)
library(DT)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Package with datasets and functions"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
textInput('pkg','Package Name', value = NULL),
actionButton("update", "Update View"),
helpText('Please enter the package name for which you want to see the list of datasets and functions (with parameters)'),
br(),
# br(),
selectInput('dat','Datasets', choices = NULL, selected = NULL)
),
# Show a plot of the generated distribution
mainPanel(
DTOutput("dataset1"),
DTOutput("dataset2")
)
)
)
server part
# Define server logic required to draw a histogram
server <- function(input, output, session) {
pkgs <- reactive({input$pkg})
#
# if (!is.null(pkgs())){
df <- reactive({
data_name1 <- data(package=input$pkg)
data_name2 <- as_tibble(data_name1$results) %>% rename(name=Item, label=Title) %>% select(-LibPath)
data_name2
})
# }
obse <- eventReactive(input$update, { df() })
# if (!is.null(pkg1())){
observe({
req(obse())
updateSelectInput(session, inputId = "dat", label = "Datasets", choices = c(df()$name), selected = df()$name[1])
})
# }
df2 <- reactive({
req(obse())
data(package = input$pkg)
library(package = input$pkg)
input$dat
# new <- input$dat
# data(new, package = input$pkg)
# cat(new)
})
output$dataset1 <- renderDataTable({
DT::datatable(obse())
})
output$dataset2 <- renderDataTable({
df2()
})
}
# Run the application
# undebug(shinyApp)
shinyApp(ui = ui, server = server)
CodePudding user response:
In the data
, we may need list
and in library
we can specify character.only = TRUE
ui <- fluidPage(
# Application title
titlePanel("Package with datasets and functions"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
textInput('pkg','Package Name', value = NULL),
actionButton("update", "Update View"),
helpText('Please enter the package name for which you want to see the list of datasets and functions (with parameters)'),
br(),
# br(),
selectInput('dat','Datasets', choices = NULL, selected = NULL)
),
# Show a plot of the generated distribution
mainPanel(
DTOutput("dataset1"),
DTOutput("dataset2")
)
)
)
server <- function(input, output, session) {
pkgs <- reactive({input$pkg})
#
# if (!is.null(pkgs())){
df <- reactive({
data_name1 <- data(package=input$pkg)
data_name2 <- as_tibble(data_name1$results) %>% rename(name=Item, label=Title) %>% select(-LibPath)
data_name2
})
# }
obse <- eventReactive(input$update, { df() })
# if (!is.null(pkg1())){
observe({
req(obse())
updateSelectInput(session, inputId = "dat", label = "Datasets", choices = c(df()$name), selected = df()$name[1])
})
df2 <- reactive({
req(obse())
e <- new.env()
library(package = input$pkg, character.only = TRUE)
out <- data(list = input$dat, package = input$pkg, envir = e)
e[[out]]
})
output$dataset1 <- renderDataTable({
DT::datatable(obse())
})
output$dataset2 <- renderDataTable({
df2()
})
}
}
# Run the application
# undebug(shinyApp)
shinyApp(ui = ui, server = server)
-output