【问题标题】:Inputting variable data in shiny在闪亮中输入变量数据
【发布时间】:2020-10-30 12:24:51
【问题描述】:

我正在寻找一个闪亮的应用程序,用户可以在其中上传一个文本文件,并可以输入他们想要过滤的术语(因此具有两个特定单词或短语的句子)。然后他们可以下载结果。到目前为止,我的脚本如下所示:

ui <- fluidPage(
  titlePanel("Download data"),
  sidebarLayout(
    sidebarPanel(
      fileInput("file1", "Choose .txt file",
                multiple = F,
                accept = c(".txt")),
      textInput('energy_co', 'Name of energy company'),
      textInput('asset', 'name of Asset Manager'),
      downloadButton("downloadData", "Download")
    ),
    
    mainPanel(
      tableOutput("table")
    )
    
  )
)




server <- function(input, output, session) {
  
  options(shiny.maxRequestSize=30*1024^2)

  
  output$table <- renderTable({  
  req(input$file1)
  
    data <- read_lines(input$file1$datapath)
    text_df <- as_data_frame(data)
    
    company_data <- text_df %>% 
      filter(str_detect(terms, input$asset)) %>%   
      filter(str_detect(terms, input$energy_co)) %>% 
      distinct(.)
    
    company_data
    
    })
  output$downloadData <- downloadHandler(
    filename = function() {
      paste(company_data, ".csv", sep = "")
    },
    content = function(file) {
      write.csv(company_data, file1, row.names = FALSE)
    }
  )
  
}


shinyApp(ui, server)

我可以上传数据集(一个 .txt 文件),但是当我尝试渲染表格或尝试将结果下载为 csv 时没有任何反应。我认为服务器脚本可能需要反应?任何帮助表示赞赏

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    试试这个

    ui <- fluidPage(
      titlePanel("Download data"),
      sidebarLayout(
        sidebarPanel(
          fileInput("file1", "Choose .txt file",
                    multiple = F,
                    accept = c(".txt")),
          textInput('energy_co', 'Name of energy company'),
          textInput('asset', 'name of Asset Manager'),
          downloadButton("downloadData", "Download")
        ),
        
        mainPanel(
          tableOutput("table")
        )
        
      )
    )
    
    
    options(shiny.maxRequestSize=30*1024^2)
    
    server <- function(input, output, session) {
      
      company_data <- reactive({
        req(input$file1,input$asset,input$energy_co)
        
        data <- read_lines(input$file1$datapath)
        text_df <- as_data_frame(data)
        
        company_data <- text_df %>% 
          filter(str_detect(terms, input$asset)) %>%   
          filter(str_detect(terms, input$energy_co)) %>% 
          distinct(.)
        company_data
      })
      
      
      output$table <- renderTable({  
        
        company_data()
        
      })
      
      output$downloadData <- downloadHandler(
        filename = function() {
          paste(company_data, ".csv", sep = "")
        },
        content = function(file) {
          write.csv(company_data(), file1, row.names = FALSE)
        }
      )
      
    }
    
    
    shinyApp(ui, server)
    

    【讨论】:

    • 当我尝试输入资产或 energy_co 数据时收到错误消息Warning: Error in stri_detect_regex: argument str should be a character vector (or an object coercible to)
    • 您对 str_detect 的使用是这里的问题。正确用法:fruit &lt;- c("apple", "banana", "pear", "pinapple")str_detect(fruit, "a")terms 是向量吗?
    • 啊,所以我已经设法解决了这个问题。但现在它抛出警告:gsub 中的错误:输入字符串 4 无效 UTF-8
    • 这取决于你的 txt 文件。我没有收到那个错误。
    • 嗯,看起来 Shiny 无法解析非 UTF-8 字符。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-19
    • 2016-02-19
    • 2015-06-29
    • 2020-04-14
    • 2014-04-11
    • 2018-06-21
    • 1970-01-01
    相关资源
    最近更新 更多