我正在开发一个应用程序,需要从用户的数据输入。当用户单击“浏览”按钮上传新数据时,我需要应用程序重置所有内容(我认为要求用户通过一个单独的按钮重新设置所有内容,然后浏览新数据是一种错误的做法!)我希望这个代码能起作用,但它没有!“浏览”按钮没有删除任何东西!
library(shiny)
x = 0.1
y = 0.1
df = data.frame(x,y) #Just define a simple dataset
ui <- shinyUI(fluidPage(
fileInput('datafile', 'Select your data CSV file',
accept=c('csv', 'comma-separated-values','.csv')),
tableOutput('table'),
))
server <- shinyServer(function(input, output, session) {
output$table = renderTable({
df
})
#
observeEvent(input$datafile, {
output$table = NULL #This one didn't work
df = NULL #This one didn't work as well
})
})
shiny::shinyApp(ui, server)这只是一个最小的示例代码。有人能帮我删除之前输入的变量通过“浏览”按钮,以便应用程序将是新鲜的新数据进来?
发布于 2020-12-12 22:53:37
df应该是反应性的,以便根据UI输入进行修改。
您可以使用浏览新文件后更新的reactiveVal:
library(shiny)
x = 0.1
y = 0.1
ui <- shinyUI(fluidPage(
fileInput('datafile', 'Select your data CSV file',
accept=c('csv', 'comma-separated-values','.csv')),
tableOutput('table'),
))
server <- shinyServer(function(input, output, session) {
df <- reactiveVal(data.frame(x,y))
observe({
file <- input$datafile
ext <- tools::file_ext(file$datapath)
req(file)
validate(need(ext == "csv", "Please upload a csv file"))
df(read.csv(file$datapath, header = T))
})
output$table = renderTable({
df()
})
})发布于 2020-12-13 11:34:33
使用reactiveVal()解决了这个问题。这正是我真正想要看到的:
library(shiny)
ui <- shinyUI(fluidPage(
fileInput('datafile', 'Select your data CSV file',
accept=c('csv', 'comma-separated-values','.csv')),
textInput("txt", "Enter the text to display below:"),
verbatimTextOutput("default"),
tableOutput('table'), #This is just to check whether the variable will be deleted
))
server <- shinyServer(function(input, output, session) {
X <- reactiveVal(c(1,3))
Y <- reactiveVal(NULL)
observe({
Y(input$txt)
})
output$default <- renderText({Y() })
output$table = renderTable({ #This is just to check whether the variable will be deleted
X()
})
observeEvent(input$datafile,{
Y(NULL)
X(NULL)
})
})
shiny::shinyApp(ui, server)“浏览”按钮用作操作按钮,在单击该按钮之前重置定义的所有内容。
https://stackoverflow.com/questions/65270549
复制相似问题