Unable to reset the textinput and selectinput. I tried to create the action button reset. Also used observeEvent. could you please help. I want to understand why the reset with observeEvent is not working, also when I manually clear the textinput, the app gives error. Any reason
UI part
library(shiny)
library(shinyjs)
library(magrittr)
library(tidyverse)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Package with datasets and functions"),
div(id='form',
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
textInput('pkg','Package Name', value = NULL),
actionButton("update", "Update View"),
actionButton("reset", "Reset inputs"),
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(
tabsetPanel(
id = 'dataset',
tabPanel("List of Datasets in the Package", DTOutput("dataset1")),
tabPanel("Datasets View", DTOutput("dataset2")),
tabPanel("List of Functions with Parameters in the Package", verbatimTextOutput('func'))
)
)
)
)
)
server part
# Define server logic required to draw a histogram
server <- function(input, output, session) {
pkgs <- reactive({input$pkg})
observeEvent(input$reset, {
#pkgs() <- NULL
updateSelectInput(session, 'dat','Datasets', choices = NULL, selected = NULL)
updateTextInput('pkg','Package Name', value = NULL)
})
#
# if (!is.null(pkgs())){
df <- reactive({
# pksis <- require(input$pkg)
# cat(pksis)
# if (input$pkg %in% rownames(installed.packages()) == TRUE) {
data_name1 <- data(package=input$pkg)
data_name2 <- as_tibble(data_name1$results) %>% rename(name=Item, label=Title) %>% select(-LibPath, -Package)
data_name2
# } else {
# install.packages(input$pkg)
# library(input$pkg)
# data_name1 <- data(package=input$pkg)
# data_name2 <- as_tibble(data_name1$results) %>% rename(name=Item, label=Title) %>% select(-LibPath, -Package)
# data_name2
# }
})
# }
obse <- eventReactive(input$update, { df() })
funct <- eventReactive(input$update, { paste0('package:',input$pkg) })
# 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]]
# new <- input$dat
# data(new, package = input$pkg)
# cat(new)
})
output$dataset1 <- renderDataTable({
DT::datatable(obse())
})
output$dataset2 <- renderDataTable({
df2()
})
output$func <- renderPrint({
lsf.str(funct())
})
observeEvent(input$reset,{
output$dataset1 <- renderDataTable({
})
output$dataset2 <- renderDataTable({
})
output$func <- renderPrint({
})
})
}
# Run the application
# undebug(shinyApp)
shinyApp(ui = ui, server = server)