我正在尝试使用Shiny用R编写一个简单的程序。程序读取用户选择的文本文件,然后将其显示为.html对象。我正在使用"扫描"功能读取文本文件(NB目前只试图输出第一行(。程序运行,但输出不会更新。为什么不更新输出?谢谢
library(shiny)
shinyApp(
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
fileInput("text_file", "Choose text file",
multiple = FALSE,
accept = c(".txt")
)
),
mainPanel(htmlOutput("example"))
)
),
server <- function(input, output, session){
text <- reactive({
req(input$text_file)
x <- scan(input$text_file, what = "string", sep = "n")[1]
})
# text output
output$example <- reactive({
renderUI({
HTML(x)
})
})
}
)
shinyApp(ui, server)
您需要进行一些更改:
- 文件读取文件时,必须要求从
input$inputId$datapath
而不是input$inputId
读取文件 renderUI()
应该返回text()
而不是x
,因为text()
是正在渲染的反应对象- 您不需要将
reactive()
添加到shine中的任何render
函数中,因为它们已经是反应性的
将服务器更改为以下服务器:
server <- function(input, output, session){
text <- reactive({
req(input$text_file)
x <- scan(input$text_file$datapath, what = "string", sep = "n")[1]
})
# text output
output$example <- renderUI({
HTML(text())
})
}