在 Shiny 中,提交按钮不是 运行
In Shiny, the submit button isn't running
我写了一个 R 脚本 (MAIN.R) 将 PDF 表格转换为 CSV。当我 运行 MAIN.R 作为一个单独的文件时,它运行良好。我试过很多次了。
目前,我正在开发一个使用“MAIN.R”作为源并以 pdf 文件作为输入的 R shiny 应用程序。当我按下提交按钮时,输出应该出现在主面板中。不幸的是,提交按钮无法正常工作。
我是 Shiny 的新手,有人可以帮我解决这个问题吗?
UI.R
shinyUI(fluidPage(
titlePanel("DATASET CONVERSION"),
sidebarLayout(
fileInput("filein", label = h2("Select a file to convert.")),
submitButton("Submit")
),
mainPanel(
tableOutput("Dataset")
)
)
)
Server.R
source("MAIN.R")
shinyServer(function(input, output) {
outputdf <- reactive({ input$filein
})
output$Dataset <- renderTable({
outputdf()
})
})
您的“提交”按钮当前未链接到任何内容,因此它不会执行任何操作。如果我没看错代码,你只是获取输入数据集并将其存储为 outputdf 的输出。然后,您的 output$Dataset 将拾取该 outputdf 并按原样显示,无需对其进行任何操作。
您使用这样的操作按钮:
## In UI.R
actionButton("execute", "Execute the Main Function")
## In Server.R
observeEvent(input$execute, {
## Do stuff here
})
请注意,actionButton 有两个参数,inputID(这就是您引用它的方式)和要在顶部显示的文本。例如,对于 input$filein,'filein' 是 inputID。
在 Server.R 中,observeEvent 不会执行任何操作,直到它检测到 input$execute 发生变化,这发生在有人单击按钮时。那就是你放置代码来做事的地方。
现在,在 output$Dataset 中,您需要访问您在该 observeEvent 中执行的任何操作的结果。一种方法是使用 reactiveValue。这就像一个反应式,但它存储的不是函数,而是数据元素。将其初始化为一个空数据帧,然后在 observeEvent 中更新它。像这样:
## In Server.R
treated_output <- reactiveValue(data.frame())
observeEvent(input$execute, {
## Run the function on the file
updated <- main_function(input$filein)
# Update your reactiveValue
treated_output(updated)
})
output$Dataset <- renderTable({
treated_output()
})
这有意义吗?
我写了一个 R 脚本 (MAIN.R) 将 PDF 表格转换为 CSV。当我 运行 MAIN.R 作为一个单独的文件时,它运行良好。我试过很多次了。
目前,我正在开发一个使用“MAIN.R”作为源并以 pdf 文件作为输入的 R shiny 应用程序。当我按下提交按钮时,输出应该出现在主面板中。不幸的是,提交按钮无法正常工作。
我是 Shiny 的新手,有人可以帮我解决这个问题吗?
UI.R
shinyUI(fluidPage(
titlePanel("DATASET CONVERSION"),
sidebarLayout(
fileInput("filein", label = h2("Select a file to convert.")),
submitButton("Submit")
),
mainPanel(
tableOutput("Dataset")
)
)
)
Server.R
source("MAIN.R")
shinyServer(function(input, output) {
outputdf <- reactive({ input$filein
})
output$Dataset <- renderTable({
outputdf()
})
})
您的“提交”按钮当前未链接到任何内容,因此它不会执行任何操作。如果我没看错代码,你只是获取输入数据集并将其存储为 outputdf 的输出。然后,您的 output$Dataset 将拾取该 outputdf 并按原样显示,无需对其进行任何操作。
您使用这样的操作按钮:
## In UI.R
actionButton("execute", "Execute the Main Function")
## In Server.R
observeEvent(input$execute, {
## Do stuff here
})
请注意,actionButton 有两个参数,inputID(这就是您引用它的方式)和要在顶部显示的文本。例如,对于 input$filein,'filein' 是 inputID。
在 Server.R 中,observeEvent 不会执行任何操作,直到它检测到 input$execute 发生变化,这发生在有人单击按钮时。那就是你放置代码来做事的地方。
现在,在 output$Dataset 中,您需要访问您在该 observeEvent 中执行的任何操作的结果。一种方法是使用 reactiveValue。这就像一个反应式,但它存储的不是函数,而是数据元素。将其初始化为一个空数据帧,然后在 observeEvent 中更新它。像这样:
## In Server.R
treated_output <- reactiveValue(data.frame())
observeEvent(input$execute, {
## Run the function on the file
updated <- main_function(input$filein)
# Update your reactiveValue
treated_output(updated)
})
output$Dataset <- renderTable({
treated_output()
})
这有意义吗?