使用Shiny Server中的新行(\ n)编写文本文件

时间:2017-04-21 09:49:08

标签: r shiny

我有一个生成文本文件的应用程序。我使用\n将文字放在新行上。如果我在本地运行应用程序,则下载的txt文件具有正确的输出。但是,如果我在Shiny Server上运行应用程序,则文本不会在新行上打印,即\n似乎不起作用。这是一个示例应用程序:

ui <- fluidPage(title = "",
                sidebarLayout(
                  sidebarPanel(
                    numericInput("num", "Number of lines to paste", 3),
                    actionButton("gen", "Generate text with new lines"),
                    downloadButton("downloadWeld", "Download")
                  ),
                  mainPanel(
                    verbatimTextOutput("vtxt")
                  ))

)

server <- function(input, output) {

  values <- reactiveValues()
  values$df <- c()

  observeEvent(input$gen,{
    values$df <- NULL    
    values$df <- paste0("E",input$num,"\n")

    for(i in 1:input$num)
      values$df <- paste0(values$df, "hello\n",i,"\nworld\n")
  })

  output$vtxt <- renderText({
    values$df
  })

  output$downloadWeld <- downloadHandler(
    filename <- function() {
      paste0(input$num,"fname.txt")
    },

    content <- function(file) {
      writeLines(values$df, file)
    },
    contentType = "text/csv"
  )

}    
shinyApp(ui = ui, server = server)

这是我的本地sessionInfo:

R version 3.3.1 (2016-06-21)

Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=English_United Kingdom.1252 
[2] LC_CTYPE=English_United Kingdom.1252   
[3] LC_MONETARY=English_United Kingdom.1252
[4] LC_NUMERIC=C                           
[5] LC_TIME=English_United Kingdom.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] ggplot2_2.2.1 shiny_1.0.2  

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.7      assertthat_0.1   digest_0.6.10    mime_0.5        
 [5] grid_3.3.1       R6_2.1.2         plyr_1.8.4       xtable_1.8-2    
 [9] jsonlite_1.0     gtable_0.2.0     scales_0.4.1     lazyeval_0.2.0  
[13] labeling_0.3     tools_3.3.1      munsell_0.4.3    httpuv_1.3.3    
[17] colorspace_1.2-6 htmltools_0.3.5  tibble_1.2 

和服务器:

R version 3.3.3 (2017-03-06)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.2 LTS

locale:
 [1] LC_CTYPE=en_GB.UTF-8       LC_NUMERIC=C               LC_TIME=en_GB.UTF-8       
 [4] LC_COLLATE=en_GB.UTF-8     LC_MONETARY=en_GB.UTF-8    LC_MESSAGES=en_GB.UTF-8   
 [7] LC_PAPER=en_GB.UTF-8       LC_NAME=C                  LC_ADDRESS=C              
[10] LC_TELEPHONE=C             LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] pool_0.1.0           data.table_1.9.6     sqldf_0.4-10         RSQLite_1.0.0       
 [5] gsubfn_0.6-6         proto_0.3-10         RMySQL_0.10.9        DBI_0.5-1           
 [9] dplyr_0.5.0          ggthemes_2.2.1       scales_0.4.1         plyr_1.8.4          
[13] shinydashboard_0.5.1 shinythemes_1.1.1    markdown_0.7.7       shiny_1.0.2         
[17] ggplot2_2.2.1       

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.8      magrittr_1.5     munsell_0.4.3    colorspace_1.3-2 xtable_1.8-0     R6_2.2.0        
 [7] tcltk_3.3.3      tools_3.3.3      grid_3.3.3       gtable_0.2.0     htmltools_0.3.5  lazyeval_0.2.0  
[13] assertthat_0.1   digest_0.6.11    tibble_1.2       mime_0.4         labeling_0.3     jsonlite_1.1    
[19] chron_2.3-47     httpuv_1.3.3 

为什么我无法在服务器应用上获得正确的输出?

1 个答案:

答案 0 :(得分:2)

我找到了answer的另一个帖子。这是因为在Shiny Server上,应用程序使用Java运行,因此使用以下方法保存新行:

\r\n

我想在本地运行时不需要Java转换。