无法重置文本输入和选择输入在闪亮的应用程序



无法重置文本输入和选择输入。我尝试创建动作按钮重置。也使用了observeEvent。你能帮帮我吗?我想了解为什么复位与observeEvent不工作,也当我手动清除文本输入,应用程序给出错误。任何理由

库:

library(shiny)
library(shinyjs)
library(magrittr)
library(tidyverse)

UI部分
# 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'))
)
)
)
)
)

服务器部分
# 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)

下面的代码使updatereset在服务器部分的顺序更加清晰


library(shiny)
library(shinyjs)
library(magrittr)
library(tidyverse)
require(DT)

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 <- function(input, output, session) {

pkgs <- reactive({
req(input$pkg)
})

##### update
observeEvent(input$update, {
updateTextInput(inputId = 'pkg', value = pkgs())

# check if this_package is installed
if(system.file(package = pkgs()) == ""){
updateSelectInput(session, 'dat', choices = NULL , selected = NULL)
}
else{
# data sets in the package
data_pkg <- data(package = pkgs())
# names of data sets in the package
data_names <- data_pkg$results[, "Item"]

updateSelectInput(session, 'dat', choices = data_names, selected = data_names[1])


### dataset1 - data names
output$dataset1 <- renderDataTable({
DT::datatable(data.frame(data_names)) 
})

### dataset3 - function list
funs <- paste0('package:', pkgs()) 
output$func <- renderPrint({
lsf.str(funs)
})
}
})


### dataset2 - selected dataset
data_name <- reactive({
req(input$dat)
})

output$dataset2 <- renderDataTable({
e <- new.env()
library(package = pkgs(), character.only = TRUE)
out <- data(list=data_name(), package = pkgs(), envir = e)
d2 <- e[[out]]

#  some datasets are 3-d, e.g., "ozone" in package "plyr"
if(length(dim(d2)) == 3){
d2 <- d2[, , 1]
}

DT::datatable(d2)
})      


##### reset
observeEvent(input$reset, {
updateTextInput(inputId = 'pkg',value = NULL)

updateSelectInput(session, 'dat', choices = NULL , selected = NULL)

output$dataset1 <- renderDataTable({
DT::datatable(data.frame("No package selected" = NULL))
})

})
}

# Run the application 
shinyApp(ui = ui, server = server)

相关内容

  • 没有找到相关文章

最新更新