R 在被动语句中使用scan

R 在被动语句中使用scan,r,shiny,shiny-reactivity,R,Shiny,Shiny Reactivity,我正试图用Shiny在R中编写一个简单的程序。程序读取用户选择的文本文件,然后将其显示为.html对象。我正在使用“扫描”功能读取文本文件(NB目前仅尝试输出第一行)。程序运行,但输出未更新。为什么输出没有被更新?谢谢 library(shiny) shinyApp( ui <- fluidPage( sidebarLayout( sidebarPanel( fileInput("text_file", "Choose text fi

我正试图用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)
库(闪亮)
shinyApp(

ui您需要进行一些更改:

  • 文件读取文件时,必须要求从
    input$inputId$datapath
    读取文件,而不是
    input$inputId
  • 您的
    renderUI()
    应该返回
    text()
    而不是
    x
    ,因为
    text()
    是正在渲染的反应对象
  • 您不需要将
    reactive()
    添加到任何
    render
    函数中,因为它们已经是reactive函数了
  • 将服务器更改为以下内容:

    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())
        })
    }
    

    server您需要进行一些更改:

  • 文件读取文件时,必须要求从
    input$inputId$datapath
    读取文件,而不是
    input$inputId
  • 您的
    renderUI()
    应该返回
    text()
    而不是
    x
    ,因为
    text()
    是正在渲染的反应对象
  • 您不需要将
    reactive()
    添加到任何
    render
    函数中,因为它们已经是reactive函数了
  • 将服务器更改为以下内容:

    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())
        })
    }
    

    服务器谢谢。工作正常。感谢您对关键概念的解释。谢谢。工作正常。感谢您对关键概念的解释。